Daily learning java series – learning notes on zero basics of Java (V) Java identifier | keyword | variable

Time:2021-10-22

1、 Identifier

1. What can an identifier identify? What is an identifier? How to understand this concept!

2. Naming rules for identifiers

3. Naming conventions for identifiers

The final requirement of this summary is to give a word at will and judge whether it is a legal identifier.

What is an identifier?

In short, any word that can be named by the programmer is an identifier.

What can identifiers be used to identify?

Can identify:

Class name, method name, variable name, interface name, constant name

Identifier naming rules

The naming rules belong to the syntax mechanism and must be observed. Failure to observe the naming rules means that the syntax is not complied with, and the compiler will report an error.

Naming rules:

1. The identifier can only be composed of numbers, letters (including Chinese), underscores () and dollar symbols ($), and cannot contain other symbols.

2. The identifier cannot start with a number.

3. Keyword cannot be used as identifier. For example: public, static, class

4. Identifiers are strictly case sensitive.

5. The identifier has no length limit in theory.

Identifier naming convention

The naming rule is syntax. If it is not observed, an error will be reported during compilation. Naming conventions are designed to improve code readability.

Naming conventions:

  1. See the name and know the meaning (when naming, it’s best to know what it means by looking at this word).
  2. Follow the hump naming method. (hump: one high and one low, one high and one low…)
    For example: identifiertest, username
  3. Class name and interface name are capitalized, and each subsequent word is capitalized.
    For example: Userlogin, sorttest
  4. Variable and method names start with lowercase letters, followed by each word with uppercase letters.
    For example: username, GetPassword
  5. All constant names are capitalized and underlined between words.
    For example: Max_ VALUE、MATH_ PI…

Every day you will write many programs. You will encounter many compilation errors and many running errors. Do you need to prepare a separate file to record this information, and record how this information is caused, what is the reason, how to solve it, and what is the solution????

It’s very necessary. If you want to be a master of wrong adjustment, it’s necessary to do it.

2、 Keywords

1. What are keywords?

When Sun company developed the Java language, it defined some words with special meaning in advance. These words are all lowercase and have special meaning and cannot be used as identifiers.

2. All existing in blue font in EDITPLUS are keywords with special meaning.

3. Remember:

All keywords in the Java language are all lowercase.

Note: the Java language is strictly case sensitive. Public is different from public.

Class is different from class. Static is different from static.

4. So what are the keywords? Do we need to recite them? Need to remember alone?

keyword:

  • public
  • static
  • void
  • class
  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char
  • true
  • false
  • if
  • while
  • for
  • private
  • protected
  • ……..

For these keywords, you don’t need to spend time memorizing them alone. With the accumulation of later programs, you will be exposed to all keywords.

3、 Variable

1. Literal quantity

Literal quantity is data

Data is literal

It’s a thing.

  • 10 100 123: integer
  • 34 3.14 2.0: floating point
  • True false: Boolean
  • ‘a’, ‘country’: character type
  • “A” “ABC” “country” “China”: string type
  • 10: An integer is a number
  • “10”: it is not a number, but a string, or it belongs to the “text class”.

The properties are completely different, and the corresponding binary codes in the computer are also completely different.

2. Variable

Understanding of variables

What are variables?

A variable is a data box. (who decides the size of the box? Data type) the most basic storage unit in memory.

It is used to store data, and the data is variable, so it is called variable.

Use of variables

Three elements of variables?

Data type, variable name, value (value is data, that is literal quantity.)

int i = 100;

Variables in Java must be declared first and then assigned before they can be accessed (they must be assigned manually.)

int k; System.out.println(k);That won’t work.

You can declare multiple variables on one line:

int a, b, c = 100;

The C variable is assigned a value of 100, and the A and B variables only declare that there is no assignment.

int a = 10, b = 20, c = 100;

You can assign values to each.

Declaration and assignment can be done separately or together!!!

int i;

i = 100; // Declaration before assignment

int k = 200; //Simultaneous assignment of declarations

In the same field, the variable name cannot be the same!!!!!!

But it can be reassigned!!!!!!

{
        int i = 100;
        //double i = 2.0; //  If the name is duplicated, the compiler will report an error, which is not allowed.
        i = 300; //  Can be reassigned.
    }

What exactly is the same domain?????

This is not easy to explain at present. Remember: a brace represents a field.

{a domain
    {B domain
        {C domain
        }
    }
}

Domain a includes domain B, and domain B includes domain C.

Classification of variables

Classify by location: just remember

Variables declared in the method body are called local variables.

public static void m1(){
    //Local variable, memory is released after method execution.
    int k = 100;
    int i = 200;
}

Variables declared outside the method and inside the class are called member variables.

public class T{
    public static void x(){
                    
    }
    //Member variable
    int i = 200;
}

Scope of variable

We don’t know each other without braces. Leave everything else alone.

{
    int i = 100;
    {
        I can be accessed here
    }
}

{
    The I variable cannot be accessed here.
}

last

I recommend you a more detailed Java zero foundation tutorial. The following one I have seen is very good and worth watching and collecting.

To share with you, click here

https://www.bilibili.com/vide…

After reading, if it helps you, thank you for your praise and support~

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]