【Java】Keywords, constants, data types, identifiers, variables of java basic learning

Keywords:

        Keywords refer to words that are given special meanings by the Java language

        Features:

1) The letters of the keyword are all lowercase

2) Commonly used code editors highlight keywords [public, class, static]

constant:

        During the running of the program, the value cannot be changed.

        Classification of Java constants:

1) Integer constants such as: 10, -10, 0, etc.

2) Decimal constants such as: -1.1, 1.1, 2.0, etc.

3) Character constants such as:'ah','a', '4', etc.

4) Boolean constants, Boolean values, indicate true or false, only two values: true and false

5) String constants. Use double quotation marks (" ") to identify one or more characters.

6) The null constant. There is only one value: null.

public class Demo {   
   public static void main(String[] args) {         
     System.out.println(10); // 输出一个整数         
     System.out.println(5.5); // 输出一个小数         
     System.out.println('a'); // 输出一个字符         
     System.out.println(true); // 输出boolean值true       
     System.out.println("欢迎来到黑马程序员"); // 输出字符串    
   } 
}

type of data

Integer:

byte: -2^7 ~ 2^7-1, that is, -128 ~ 127.1 bytes. Byte . Add B at the end

short: -2^15 ~ 2^15-1, that is -32768 ~ 32767. 2 bytes. Short . S at the end

Signed int: -2^31 ~ 2^31-1, that is, -2147483648 ~ 2147483647. 4 bytes. Integer.

Unsigned int: 0~2^32-1.

long: -2^63 ~ 2^63-1, that is, -9223372036854774808 ~ 9223372036854774807. 8 bytes. Long . Add L at the end. (You can also not add L)

Floating point:

float: 4 bytes. Float. Add F at the end. (You can also not add F)

double:8字节。Double。

Character type:

char: 2 bytes. Character.

Boolean:

boolean:Boolean。

      Data type conversion

note

1) Type conversion between boolean type and other types is not allowed ( boolean type cannot be promoted automatically, nor type conversion can be forced), otherwise a compilation error will occur

2) Byte type cannot be automatically promoted to char, and automatic type promotion will not occur directly for char and short (the problem of negative numbers), and byte can be promoted directly to short type

3) When performing operations on data types less than int (byte, char, short), first the variable values ​​of these types will be forced to int for operation, and finally the value of int type will be obtained, so if you combine two short types Add the values ​​of and the final result is of type int. If you need to get the result of type short, you must convert the displayed operation result to type short

short s1 = 1;s1 = (short)(s1+1)

The conversion of char type data to int type is calculated according to the corresponding int value in the code table. For example, in the ASCII code table,'a' corresponds to 97.

 

                      

Automatic type conversion:

        Assign a value or variable that represents a small data range to another variable that represents a large data range

double num=10;//将int类型的10直接赋值给double类型
System.out.println(num);//输出结果为10

Force type conversion:

        Assign a value or variable that represents a large data range to a variable that represents a small data range.

        Conversion format: target data type variable name = (target data type) value or variable;

double num=5.5;
int nu1=(int)num;//将double类型的num转换为int类型

 

Identifier

        Identifiers are names used by users in programming and are used to name classes, methods, variables, constants, etc.

Composition rules:

    1) Consists of letters, numbers, underscore "_", dollar sign "$", the first character cannot be a number

    2) Cannot use keywords in java as identifiers

    3) Identifiers are case sensitive (case sensitive)

Naming rules:

    1) Big hump nomenclature:     the first letter of each word of the class name  must be capitalized

    2) Little hump naming method: method names, variable names        are lowercase, and the first letter of each word is uppercase from the second word

Note: The identifier must be known by the name

variable

        The amount by which the value can change during the execution of the program

数据类型 变量名 = 初始化值; 
// 声明变量并赋值 int age = 18;
 System.out.println(age);

//或者
// 先声明,后赋值(使用前赋值即可) 
数据类型 变量名; 变量名 = 初始化值; 
double money; money = 55.5;
System.out.println(money);


//还可以在同一行定义多个同一种数据类型的变量,中间使用逗号隔开。但不建议使用这种方式,降低程序的可读 性。
int a = 10, b = 20; // 定义int类型的变量a和b,中间使用逗号隔开 System.out.println(a); 
System.out.println(b);
int c,d; // 声明int类型的变量c和d,中间使用逗号隔开 
c = 30; 
d = 40; 
System.out.println(c); System.out.println(d);

Variable considerations:

     1) Variable names cannot be repeated in the same curly braces

      2) Variables must be initialized (assigned) before being used

      3) When defining a variable of type long, you need to add L after the integer (uppercase and lowercase are acceptable, uppercase is recommended). Because integers are of type int by default, an integer that is too large may exceed the range of int. 

      4) When defining variables of the float type, you need to add F after the decimal (upper and lowercase are acceptable, uppercase is recommended). Because the default type of floating-point numbers is double, the value range of double is greater than float, and the types are not compatible. 

Guess you like

Origin blog.csdn.net/weixin_43267344/article/details/107497655