-
1, Variable
-
1. What are variables
-
2. Define the format of the variable
-
3. Precautions when defining variables
-
-
2, Data type
-
1. Basic data type
-
2. Reference data type
-
3. Notes on data types
catalogue
2. Define the format of the variable
-
1, Variable
1. What are variables.
Variable refers to the quantity whose value can change during the execution of the program, which is called variable
2. Defines the format of the variable.
The first is data type variable name = initialization value;
The second is the data type variable name;
Variable name = initialization value;
3. Considerations when defining variables.
① scope: variables are valid within the same brace, but not outside the brace.
② two variables with the same name cannot be defined in the same scope.
③. It cannot be used directly without initialization value. It can be assigned before use
④. It is not recommended to define multiple variables on one line. It is mandatory to define one variable as one line.
2, Data type
1. Basic data type
The integer type occupies a range that can be represented by the number of bytes byte 1 -2^7 ~ 2^7-1 short 2 -2^15 ~ 2^15-1 int 4 -2^31 ~ 2^31-1 long 8 -2^63 ~ 2^63-1 Floating point type float 4 -3.403*10^38 ~ 3.403*10^38 double 8 -1.798*10^308 ~ 1.798*10^308 character char 2 Boolean boolean 1
2. Reference data type (supplementary)
3. Notes on data types
① the value of Boolean type cannot be assigned to other data types; The default conversions are byte, short, char int -- long float double; When byte, short and char are converted to each other, they participate in the operation and are first converted to int.
Cast type: target data type = (target data type) target variable name = (target data type) variable to be assigned. [ps: extensive use of cast is not recommended, and precision may be lost.]
② character type operation: when characters participate in arithmetic operation, they will be converted into values in ascll code table to participate in the operation. It is recommended to remember several ASCL codes, '0' = 48 'a' = 65 'a' = 97.
package com.study.gss.day03; /* System.out.println('a'); System.out.println('a'+1); Shortcut keys for code formatting: ctrl+alt+L ASCII The values corresponding to the three characters that must be remembered in the code table: '0' 48 'A' 65 'a' 97 */ public class DataTypeDemo6 { public static void main(String[] args) { System.out.println('a'); //When characters participate in arithmetic operation, they will be converted into the values in the corresponding ASCII code table to participate in the operation System.out.println('c' + 1); // 100 } }
③ the usage of the + sign.
package com.study.gss.day03; /* System.out.println("hello"+'a'+1); System.out.println('a'+1+"hello"); System.out.println("5+5="+5+5); System.out.println(5+5+"=5+5"); +Usage of number: 1,When both sides of the plus sign are numbers or numbers and characters, the + sign does addition 2,When there is a string on both sides of the plus sign, it is string splicing. After splicing, it is a new string 3,Adding a string with any numeric value is string splicing, and a new string is spliced */ public class DataTypeDemo7 { public static void main(String[] args) { System.out.println("hello"+'a'+1); // helloa1 System.out.println('a'+1+"hello"); // 98hello System.out.println("5+5="+5+5); // 5+5=55 System.out.println(5+5+"=5+5"); // 10=5+5 //System.out.println("5+5="+5+5); // 5+5=55. I want to calculate the following 5 + 5 first. What should I do? //Parentheses can change the order and priority of operations System.out.println("5+5="+(5+5)); } }
question1. (byte, short and char will be automatically converted to int type during operation)
/* byte b1=3,b2=4,b; b=b1+b2; b=3+4; Which sentence failed to compile? Why? Variable addition is different from constant addition: 1,Adding variables will first promote the data type, and then add and assign values. If the received data type range is less than the data type range after the calculation result, an error is reported and cannot be assigned 2,Constant addition will be calculated first. After calculation, it will be checked that it is not within the range of received data types. If it is, it will be assigned directly. If it is not, an error will be reported. */ public class DataTypeDemo4 { public static void main(String[] args) { // int a = 3; // byte b = 4; // int c1 = a + b; // // byte c2 = (byte)(a + b); // System.out.println(c2); byte b1=3,b2=4,b; // b=b1+b2; b=(byte)(b1+b2); b=3+4; } }