? Java variables:
? Java variables are the most basic storage units in a program, and their features include variable names, variable types, and scopes.
? Each variable in a Java program belongs to a specific data type and must be declared before use:
eg
int i=100;
float f=12.4f;
Double d=0.12;
String s= "Helloword";
In essence, a variable is actually a small chunk of memory, using the variable name to access the area, so each variable must be requested (declared) before it is used, and then it must be assigned before it can be used.
Java variable classification:
? divided by the declared location:
A local variable: A variable defined inside a method or statement block "A variable (including a formal parameter) declared inside a method body becomes a local variable"
A member variable: A variable inside a method outside a class that is internally defined "in the method body, a variable declared within a class becomes a member variable"
Note: Outside the class (outside the curly braces that correspond to the class) cannot have the declaration of a variable
Public class Test { int j = 9; the parameter of the // method is a local variable Eg:argspublic static void Main (string[] args) {// method body int i = +; local variable }}
? By the data type that belongs to:
? basic data type variables
? reference data type variables
The Boolean logical operator has only true and false values of two
Public class Test { publicstaticvoid main (string[] args) { boolean flag= true ; if (flag) { //dosomething } }}
Basic Data type conversions:
? The Boolean type cannot be converted to another data type, and no other data type can be converted to a Boolean type
? Small-capacity types are automatically converted to large-capacity data types, and data types are sorted by capacity size
Byte,short,char->int->long->float->double
Byte,short,char do not convert to each other, they are first converted to int type when calculating
When a large data type is converted to a data type with small capacity data, a cast character is added, but the precision can be reduced or overflowed; be careful when using
When there are multiple types of data blending operations, the system first automatically translates all the data into large-capacity data types before it is calculated. Public class Test {
Public Static voidMain (string[] args) {//method Body intA=123; intb=456; DoubleD= (a+b) *1.6;//the system is automatically converted to double type floatF= (float) ((a+b) *1.6);//need to strengthen the conversion of float's data capacity is larger than double byteb1=67; byteB2=90; byteB3= (byte) (B1+B2);//The system is converted to an int operation and requires a cast characterSYSTEM.OUT.PRINTLN (B3);//The print result is:-99 reason: The range of byte tables is -128~127 floatF3 = 1.23f;//must add f LongL1 = 123; LongL2 = 30000000000L;//must be plus l floatF4 = l1+l2+f3;//system will be converted to float type calculation LongL = (Long) F;//The cast will shed the fractional part (not rounding)System.out.println (F4); System.out.println (l);
} }Self-adding decrement operator:
? + + (-) first operation and then value
To take a value before the operation
Public classTest { Public Static voidMain (string[] args) {intI1 = 10; intI2 = 20; inti = (i2++); System.out.print ("I=" +i); System.out.println ("I2=" +i2); I= (++i2); System.out.print ("I=" +i); System.out.println ("I2=" +i2); I= (--i1); System.out.print ("I=" +i); System.out.println ("I1=" +i1); I= (i1--); System.out.print ("I=" +i); System.out.println ("I1=" +i1); } }
Printing results:
I=20 i2=21i=22 i2=22i=9 i1=9i=9 i1=8
Logical operators:
Public classTest { Public Static voidMain (string[] args) {BooleanA,b,c; A=true; b=false; C= A &b; System.out.println (c);//falsec = A |b; System.out.println (c);//truec = a ^b; System.out.println (c);//truec =!A; System.out.println (c);//falsec = a &&b; System.out.println (c);//falsec = A | |b; System.out.println (c);//true } }
Public classTest { Public Static voidMain (string[] args) {inti = 1,j = 2; //The second operation is no longer calculated BooleanFlag1 = (i<3) && ((i+j) >5); BooleanFlag2 = (i<3) && ((i+j) <5); BooleanFlag3 = (i==1) && ((i+j) <5); System.out.println (FLAG1);//falseSystem.out.println (FLAG2);//trueSystem.out.println (FLAG3);//true//The second operation is no longer calculated BooleanFlag4 = (i<2) | | ((I+J) <6); BooleanFlag5 = (i>2) | | ((I+J) <6); BooleanFlag6 = (i<2) | | ((I+J) <6); System.out.println (FLAG1);//falseSystem.out.println (FLAG2);//trueSystem.out.println (FLAG3);//true } }
Switch statement:
Public classTest { Public Static voidMain (string[] args) {intScore = 69; if(score>100| | Score<0) {System.out.println ("Input score is wrong"); }Else{ Switch(SCORE/10){ Case10: System.out.println ("Very good"); Break; Case9: System.out.println ("Great"); Break; Case8: System.out.println (Excellent); Break; Case7: System.out.println (Good); Break; Case6: System.out.println (Failed); Break; Case5: Case4: Case3: Case2: Case1: System.out.println ("Failed"); Break; default: System.out.println ("Error"); } } } }
Recursive invocation:
Public classTest { Public Static voidMain (string[] args) {System.out.println (3)); } Public Static intMethodintN) { if(n = = 1){ return1; }Else{ returnN*method (n-1); } }}
Program execution Process:
Stack: stack holds local variables
Heap: Stack dynamic request memory for new out of something
Java Trivia points