This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

Java identifier

1. What is an identifier

Identifiers: Any place you can name is called an identifier. For example: class name, variable name, method name, interface name, package name, and so on.

2. Naming rules for identifiers :(if you do not comply with the following rules, the compilation fails! Strict compliance is required)

  • Consists of 26 uppercase and lowercase letters, 0-9, _, or $
  • Numbers may not begin.
  • The keyword and reserved word cannot be used, but can contain the keyword and reserved word.
  • Java is case sensitive and has unlimited length.
  • Identifiers cannot contain Spaces.

3. Naming conventions in Java :(if you do not follow the following specifications, the compilation can pass, but we recommend following the following rules)

  • Package name: Lowercase letters when multiple words are used, for example, XXXYYYZZz.
  • Class name, interface name: When multiple words are used, the first letter of each word is uppercase, for example, XxxYyyZzz.
  • Variable name, method name: If there are multiple words, the first word starts with a lowercase letter and the second word starts with a lowercase letter, for example, xxxYyyZzz.

4. Precautions

  • In the name, in order to improve the reading, to try to make sense, “see the name know the meaning”.
  • Java uses the Unicode character set, so identifiers can also be declared using Chinese characters, but this is not recommended.

Data types defined by Java

1. Eight basic data types:

  • Integer types: byte(1 byte =8bit), short(2 bytes), int(4 bytes), long(8 bytes)
  • Float: float(4 bytes), double(8 bytes)
  • Character type: char(2 bytes)
  • Boolean: Boolean
A. the integer

For integers, there are several different computer representations of different bases:

  • Binary: 0,1,1. Start with 0b or 0b.
  • Decimal (decimal) : 0-9, decimal into 1 after 10.
  • Octal: 0-7, 1 in the full 8, starting with the digit 0.
  • Hex: hexadecimal number (0-9 or a-F). 1. The value starts with 0x or 0x. A-f here is case insensitive. 0x21AF +1= 0X21B0

The following code describes the representation of base 4:

public static void main(String[] args) {
        int num1 = 0b110;
        int num2 = 110;
        int num3 = 0110;
        int num4 = 0x110a;

        System.out.println(num1);
        System.out.println(num2);
        System.out.println(num3);
        System.out.println(num4);
    }
Copy the code

Output result:

6
110
72
4362

Process finished with exit code 0

Copy the code

The following code uses four integer data types:

// Int type: byte(1 byte =8bit) \ short(2 bytes) \ int(4 bytes) \ long(8 bytes)
//① Byte range: -128 to 127
//
byte b1 = 12;
byte b2 = -128;
//b2 = 128; // Failed to compile
System.out.println(b1);
System.out.println(b2);
// declare a long variable, which must end with "l" or "l"
// when defining integer variables, we usually use int.
short s1 = 128;
int i1 = 1234;
long l1 = 3414234324L;
Copy the code
B. floating point
// Float: float(4 bytes) \ double(8 bytes)
// float type, representing a number with a decimal point
// float indicates that the range of values is larger than long

double d1 = 123.3;
System.out.println(d1 + 1);
//③ When defining a variable of type float, the variable must end with "f" or "f"
float f1 = 12.3 F;
System.out.println(f1);
// Double is usually used when defining floating-point variables.
Copy the code
C. character
// char (1 character =2 bytes)
// (1) define a char variable, usually with a pair of '', and only one character inside
char c1 = 'a';
// Failed to compile
//c1 = 'AB';
System.out.println(c1);

char c2 = '1';
char c3 = 'in';
char c4 = 'the';
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);

Declare a character. Escape a character. Use Unicode values to represent character constants
char c5 = '\n';/ / a newline character
c5 = '\t';/ / tabs
System.out.print("hello" + c5);
System.out.println("world");

// Use unicode values directly
char c6 = '\u0043';
System.out.println(c6);
Copy the code

Output result:

a
1Hello world CCopy the code
D. the Boolean
// Boolean: Boolean
// The value can be either true or false
// this is the first time that I have been able to do this
boolean bool1 = true;
System.out.println(bool1);
Copy the code

2. Reference data types:

  • Class: omitted here and covered separately in a later article
  • Interface: Omitted here and covered separately in the following article
  • Array: Omitted here and covered separately later in this article

3. Type String

Use of String variables:

  1. String is a reference data type, which translates as a String
  2. When declaring a String variable, use a pair of “”
  3. String can operate with any of the eight basic data type variables, and the operations can only be concatenated: +
  4. The result of the operation is still String

Operation rules between basic data types

Note: This discussion is only about operations between variables of the seven basic data types. Does not contain Boolean type.

1. Automatic type promotion

  • Byte, char, short –> int –> long –> float –> double ps: char, short –> int –> long –> float –> double The size of the volume refers to the size of the range of numbers. For example, float has more capacity than long
  • In particular: byte, char, short results in int

Char, short; int; char; int

byte b0 = 3;
int i1 = 111;
// Failed to compile
//b0 = b0 + i1;
//
Copy the code

The sum of the above two variables is an integer. Instead of assigning the sum directly to the byte variable b0, the sum can be first converted to byte and then assigned to b0, as follows:

b0 = (byte)(b0 + i1);
Copy the code

Alternatively, assign the sum to the integer variable i1;

i1 = b0 + i1 ;
Copy the code

It is also possible to assign a long, float, or double to a long, float, or double with a larger capacity. Since the result is an int, assignment to a larger type automatically increases the result as follows:

long l1 = b0 + i1;
float f1 = b0 + i1 ;
double d1 = b0 + i1 ;

Copy the code

The same is true for short and char variables (char evaluates to their corresponding ASCII values). The same is true for short and char variables. Char and short = char and short = short

char numberChar = 'in';
short numberShort = 333;
int numberInteger = numberChar + numberShort;
System.out.println(numberInteger);
Copy the code

Output result:

20346
Copy the code

2. Cast type

Cast: The reverse of the automatic type promotion operation.

  • A strong character is required :()
  • Note: Casting may result in loss of precision.
public static void main(String[] args) {

        // No precision loss:
        long l1 = 111;
        short s1 = (short)l1;
        System.out.println(s1);

        // Precision loss:
        double d1 = 13.14;
        int i1 = (int)d1;// Truncate
        System.out.println(i1);

        int i2 = 128;
        byte b1 = (byte)i2;
        System.out.println(b1);
    }
Copy the code

Output result:

111
13
-128

Process finished with exit code 0

Copy the code

3. An error occurs when compiling:

public static void main(String[] args) {
        // A large integer
        //long l0 = 123456789123;
        // It should be followed by l or L
        long l0 = 123456789123l;
        // Why can we write this when integers are not large enough
        long l1 = 123456;
        // This is because integer constants, of type int by default, 123456 is int, and are automatically type promoted when assigned to long on the left.


        // Also, floating-point numbers default to double and cannot be assigned directly to float
        / / float f0 = 13.14;
        // A cast is required
        float f0 = (float)13.14;
        
        // In the same way, you need to pay attention to this when doing numerical calculations
        byte b0 = 12;
        //b0 = b0 + 1; // Failed to compile, 1 is an int, and the result will be automatically promoted to an int.
        //float f1 = b0 + 12.3; 12.3 is double and the result will be automatically promoted to double.
        

    }
Copy the code