Identifiers and variables (java)

1. Identifier

1. Package name, class name, method name, parameter name, variable name, etc. These symbols are called identifiers.

2. Identifiers can be composed of letters, numbers, underscore_ and dollar sign $

3. The identifier cannot start with a number and cannot be a keyword in java.

4. The first character can be followed by letters (AZ or az), underscore _, dollar sign $ or any number.

5. Java is case-sensitive, so myvar and MyVar are two different identifiers.

6. Keywords and reserved words cannot be used as identifiers, but identifiers can contain keywords and reserved words.

7. The identifier cannot contain spaces.

2. Naming convention

1. All letters in the package name must be lowercase. For example: cn.com.test

2. The first letter of each word in class names and interface names must be capitalized. For example: ArrayList, use big camel case

public class Functions {
    
    }

3. All letters in constant names are capitalized, and words are connected with underscores. For example: DAY_OF_MONTH The first letter of the variable name and method name is lowercase, and starting from the second word, the first letter of each word is uppercase. For example: lineName, getLingNumber, use small camel case .

public class Functions {
    
    
    public void demoTest(){
    
    
        System.out.println("公共的方法");
    }
}

4. In the program, you should try to use meaningful English words to define identifiers to make the program easy to read. For example: use userName to represent the user name and password to represent the password. Try your best to know the meaning of the name

3. Variables

1. The concept of variables

a storage area in memory

The data in this area can keep changing within the same type range

Variables are the most basic storage units in programs. Contains variable type, variable name and stored value

2. The role of variables

Used to save data in memory

3. Things to note
  1. Every variable in Java must be declared before use
  2. Use variable names to access this area of ​​data
  3. The scope of a variable: within the pair of {} where it is defined
  4. Variables are only valid within their scope
  5. Variables with the same name cannot be defined in the same scope.

4. Variable classification-according to data type

For each kind of data, a clear specific data type (strongly typed language) is defined, and different sizes of memory space are allocated in the memory.

Insert image description here

Supplement: Classification of variables-different according to the position of declaration

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

public class User {
    
    
    private String id;
    private String username;
    private String password;

}

Variables declared inside a method body are called local variables.

public class Functions {
    
    
    public void demo(){
    
    
        int a = 10; // a为局部变量
        System.out.println("公共的方法");
        System.out.println(a);
    }
}

Insert image description here

Note: The similarities and differences between the two in terms of initialization values:

  1. Same: both have life cycles
  2. Exception: Local variables, except formal parameters, need to be explicitly initialized.

5. Integer type

byte、short、int、long

  • Each Java integer type has a fixed table number range and field length, which is not affected by the specific OS to ensure the portability of Java programs.
  • Java's integer constants default to int type. When declaring long type constants, you must add 'l' or 'L' after them.
  • Variables in java programs are usually declared as int type. Unless it is not enough to represent a large number, long is used.
    Insert image description here
    500MB 1MB = 1024KB 1KB= 1024B B= byte? bit?

bit: The smallest unit of storage in a computer. byte: The basic storage unit in a computer.

6. Floating point type

float、double

  1. Similar to integer types, Java floating-point types also have fixed table number ranges and field lengths, which are not affected by the specific operating system.
  2. Floating point constants have two representations:
    • Decimal number format: such as: 5.12 512.0f .512 (must have a decimal point)
    • Scientific notation format: such as: 5.12e2 512E2 100E-2
  3. float: single precision, the mantissa can be accurate to 7 significant digits. In many cases, the accuracy is difficult to meet the demand.
  4. double: double precision, the precision is twice that of float. This type is usually used.
    Insert image description here

7. Character type

char

  1. Char type data is used to represent "characters" in the usual sense (2 bytes)
  2. All characters in Java use Unicode encoding, so a character can store a letter, a Chinese character, or a character of other written languages.
  3. Three forms of expression of character variables:
  • A character constant is a single character enclosed in single quotes (' '). For example:

    char c1 = 'a'; 
    char c2  = '中'; 
    char c3 = '9'
    
  • Java also allows the use of the escape character '\' to convert the following characters into special character constants. For example:

char c3 = '\n'; \\ '\n'表示换行符
  • Use Unicode values ​​directly to represent character constants: '\uXXXX'. Among them, XXXX represents a hexadecimal integer. like:

    System.out.println("\u6211\u7231\u004a\u0061\u0076\u0061");
    我爱Java
    

​ 4. The char type can perform operations. Because it all corresponds to Unicode code.

8. Boolean type

boolean

The boolean type is used to determine logical conditions and is generally used for program flow control.

  • if conditional control statement;
  • while loop control statement;
  • do-while loop control statement;
  • for loop control statement;

Boolean type data only allows the values ​​true and false, not null.

  • You cannot use 0 or non-0 integers to replace false and true, which is different from C language.

  • There are no bytecode instructions dedicated to boolean values ​​in the Java virtual machine. The boolean values ​​operated by the Java language are replaced by the int data type in the Java virtual machine after compilation: true is represented by 1, false is represented by 0 . ——"Java Virtual Machine Specification 8th Edition"

9. Automatic type conversion

Small-capacity types are automatically converted to large-capacity data types. Data types are sorted by capacity as:

Insert image description here

  1. When there are multiple types of data mixed operations, the system first automatically converts all data into the data type with the largest capacity, and then performs calculations.

  2. Byte, short, and char are not converted to each other. They are first converted to int type during calculation.

    public class Test2 extends Functions{
          
          
    
        public static void main(String[] args) {
          
          
            byte a = 10;
            char b = 20;
            System.out.println(getType(a+b));
        }
        
        public static String getType(Object obj){
          
          
            return obj.getClass().toString();
        }
    
    }
    ========================
     class java.lang.Integer   
    
  3. The boolean type cannot be operated with other data types.

  4. When concatenating a value of any basic data type with a String (+), the value of the basic data type will be automatically converted to a String type.

public class Test2 extends Functions{
    
    


    public static void main(String[] args) {
    
    
        byte a = 10;
        System.out.println(getType(a+ "hello"));
    }
    public static String getType(Object obj){
    
    
        return obj.getClass().toString();
    }

}
====================
class java.lang.String    

10. String type

  1. String is not a basic data type, it is a reference data type
  2. The usage is consistent with the basic data types. For example: String str = "abcd";
  3. A string can be concatenated with another string, and other types of data can also be concatenated directly. For example:
str = str + “xyz” ; 
int n = 100;
str = str + n;

11. Forced type conversion

The reverse process of automatic type conversion, converting large-capacity data types into small-capacity data types. When using it, you must add the coercion character: (), but it may cause precision reduction or overflow, so be careful.

Usually, strings cannot be directly converted to basic types, but through the wrapper class corresponding to the basic type, the string can be converted into a basic type. like:

String c = "43";
int i = Integer.parseInt(a);

double i = Double.parseDouble(c);
System.out.println(i);

double x = 9.997;
int nx = (int)x;

The boolean type cannot be converted to other data types.

Part of the article is taken from Shang Silicon Valley Java tutorial materials

Guess you like

Origin blog.csdn.net/weixin_45833112/article/details/125359296