Memory Area allocation:
1. Code segment: Storing Codes
2. Data segment: Static variable
3. Stack: Local Variables
4. Heap:new out of something
8 Basic data types:
Boolean Type: Boolean
Character type: char:1 bytes
Integer type:
Byte:1 bytes-127-128
Short:2 bytes-2 15--2 15 square-1
Int:4 bytes
Long:8 bytes
floating-point type:float (4 bytes, 10 38-square), double (8 bytes, 10 300-time Square)
Reference data types: classes, interfaces, arrays
Double into the float variable no, long put in int will not go wrong
Data type conversions:
1. The Boolean type cannot be converted to another type
2. Shaping int, char, float float data can be converted to each other
3. The small size of the type is automatically converted to a large type of capacity: The volume refers to the number of numbers represented, the sizes are sorted as:
Byte,short,char-->int-->long-->float-->double (Byte,short,char operation automatically converted to Int,int+long=long ...)
Byte, short, char cannot be converted to each other, the three of them are automatically converted to int type when calculating
4. Large capacity conversion to a small size data type to be added with a cast character, but resulting in reduced precision or overflow.
5. When multiple types of data are mixed, the system automatically converts all data into the type of data with the largest capacity and then calculates it.
6. The real number constant is double by default, and the shaping constant is int by default
7. Double variables cannot be cast to float because floating-point types do not exist the same way
8. Float type to indicate F
byte=1; Yes, as long as not more than 127byte b3=b1+b2; No, you want to cast (byte), change to the following byte b3= (byte) b1+b2; If it exceeds 127, it will automatically cut off the front byte float f1=1.23f; Must add Ffloat f2=3.2f;float f3=f1+f2; No, you want to cast, because it becomes a double.
public class Testconvert {public static void Main (String arg[]) { int i1 = 123; int i2 = 456; Double D1 = (I1+I2) *1.2;//system will be converted to double operation float f1 = (float) ((I1+I2) *1.2);//need to strengthen the conversion character byte B1 =; byte b2 =; byte B3 = (byte) (B1+B2);//The system will be converted to an int type operation, and/ or to force the conversion character System.out.println (b3); Double d2 = 1e200; float F2 = (float) d2;//will generate overflow System.out.println (F2); Float F3 = 1.23f;//must be plus f long l1 = 123; Long L2 = 30000000000l;//must be added L float f = l1+l2+f3;//system will be converted to float type calculation long L = (long) f;//cast will shed fractional parts (not rounded) }}
public class TestConvert2 {public static void main (string[] args) { int i=1,j=12; float f1= (float) 0.1; 0.1f float f2=123; Long L1 = 12345678,l2=8888888888l; Double D1 = 2e20,d2=124; BYTE B1 = 1,b2 = 2,B3 = 127; j = j+10; i = I/10; i = (int) (i*0.1); Char c1= ' a ', c2=125; byte B = (byte) (B1-B2); char C = (char) (c1+c2-1); float F3 = f1+f2; float F4 = (float) (f1+f2*0.1); Double d = d1*i+j; float f = (float) (D1*5+D2); }}
The Char type addition automatically converts int, which needs to be cast back:
public class Test {public static void main (string[] args) {char c1= ' c '; char C2=1;char c= (char) (C1+C2); System.out.println ("c=" +c);//system.out.println ("c2=" +C2);}}
The program that adds factorial:
public class Test{public static void Main (string[] args) {long result = 0; Long f = 1; for (int i=1;i<=10;i++) { f=f*i; result=result+f; } SYSTEM.OUT.PRINTLN (result);}}
1+3+5+...+99:
public class Test {public static void main (string[] args) {int sum = 0;for (int i=1; i<=99;i=i+2) {sum=sum+i;} SYSTEM.OUT.PRINTLN (sum);}}
Output 0~9:
public class Testwhile{public static void Main (String args[]) {int I=0;while (i<10) {System.out.println (i); i++;}}}
While break, continue:
public class Test{public static void Main (String args[]) {int stop=4;for (int. i=1;i<10;i++) {if (i==stop) break; System.out.println (i);}}}
1-100 the first 5 bits divisible by 3:
public class Test{public static void Main (String args[]) {int num=0;for (int i=1;i<=100;i++) {if (i%3==0) { System.out.println (i); num++;} if (num==5) break;}}}
Prime numbers between 101-200: numbers that can be divisible only by themselves and 1:
public class Test{public static void Main (String args[]) {for (int i=101;i<200;i+=2) {boolean f=true;for (int j=2;j<i ; j + +) {if (i%j==0) {f=false;break;}} if (!f) continue; System.out.println (i);}}}
Switch statement: Case can be merged, careful case penetration, to use break, switch inside can only detect the int type, so the use can be converted to int char,byte,short type
public class Test{public static void Main (String args[]) {int I=8;switch (i) {case (2): Case (3): Case (5): Case (8): System.out.println ("AAAA"); Break;case (9): System.out.println ("bbbb"); Break;default:system.out.println ("bbbb");}}
Java Basic syntax