Some sorting and classification of data types
table of Contents
5. Single-precision floating-point type
6. Double-precision floating-point type
1. Commonly used reference classes
2. How to use the reference class
1. Implicit conversion (automatic type conversion)
2. Explicit conversion (forced type conversion)
One, 8 basic data types
1. Byte type
Byte type, unit: byte, length: 1, value range: -128~127
Byte is composed of 8-bit binary
Byte s1=00000001;
System.out.println(s1);//1
Base conversion
Binary to decimal
Integer binary numbers are multiplied by the power of 2 and added sequentially
10010111
Decimal = 151
1*2^7+0*2^6+0*2^5+1*2^4+0*2^3+1*2^2+1*2+1*2^0
PS: The last bit is 2 to the power of zero, so it is 1.
Decimal to binary
Taking 54 as an example, the steps are as follows:
54/2=27.......0
27/2=13.........1
13/2=6............1
6/2=3............0
3/2=1............1
1/2=0............1
Then 54 (decimal) = 110110 (binary)
2. Short integer
Short integer, unit: short, length: 2, value range: -32768~32767
The maximum value of short is 32767 (5 bits)
short a1=32767;
System.out.println(a1);//32767
3. Integer
Integer, unit: int, length: 4, value range: -2147483648~2147483647
//int shaping is up to 2147483647 (10 bits)
int a2=2147483647;
System.out.println(a2);//2147483647
4. Long integer
Long integer, unit: long, length: 8, value range: -9223372036854775807L
Long long integer is up to 9223372036854775807L (16 bits)
long a3=922337203685477580L;
System.out.println(a3);//922337203685477580L
5. Single-precision floating-point type
Single-precision floating-point type, unit: float, length: 4, value range: +/-3.4E+38F (6~7 effective digits)
float single-precision floating-point type, the maximum decimal point is 8 digits
float a4=0.12345678F;
System.out.println(a4);//0.12345678
6. Double-precision floating-point type
Double-precision floating-point type, unit: double, length: 8, value range: +/-1.8E+308 (15 effective digits)
//double double-precision floating-point type with a maximum decimal point of 16 digits
double a5=0.1234567890123456;
System.out.println(a5);//0.1234567890123456
7. Character type
Character type, unit: char, length: 2, value range ISO single character set
char occupies 2 digits and can be directly assigned a letter, number, Chinese, or a number in the range of 0~65565
When assigning a range of numbers, each number represents a character
char a6='陈';
char a7='c';
char a8='1';
char a9=6556;
char a10=99;
System.out.println(a6);//陈
System.out.println(a7);//c
System.out.println(a8);//1
System.out.println(a9);//ᦜ
System.out.println(a10);// c
System.out.println(a7==a10);// true
8. Boolean
boolean Boolean type size 1 byte true/false
Two, reference class
1. Commonly used reference classes
Packaging (Byte, Short, Integer, Long, Float, Double, Boolean)
Consistent with the eight basic data types, one-to-one corresponding packaging categories
Byte b=new Byte("001");
System.out.println(b);//1
Short s=new Short("32767");
System.out.println(s);//32767
Integer s=new Integer("2147483647");
System.out.println(s);//2147483647
Long s=new Long("9223372036854775807");
System.out.println(s);//9223372036854775807
Float s=new Float("0.12345678");
System.out.println(s);//0.12345678
Double s=new Double("0.1234567890123456");
System.out.println(s);//0.12345678
Boolean s=new Boolean("true");
System.out.println(s);//true
Character s=new Character('a');
System.out.println(s);//a
, Enumeration class, String class, Object. . . .
2. How to use the reference class
1. Strong Reference (StrongReference)
Strong citations are the most commonly used citations. If an object has a strong reference, the garbage collector will never reclaim it.
When the memory space is insufficient, the Java virtual machine would rather throw an OutOfMemoryError error to make the program terminate abnormally.
Nor will it solve the problem of insufficient memory by reclaiming objects with strong references at will.
ps: Strong quotation actually means that we usually mean A a = new A().
2. Soft Reference (SoftReference)
If an object has only soft references, the memory space is enough, the garbage collector will not reclaim it;
If the memory space is insufficient, the memory of these objects will be reclaimed.
As long as the garbage collector does not reclaim it, the object can be used by the program.
Soft references can be used to implement memory-sensitive caches
3. Weak Reference (WeakReference)
The difference between weak references and soft references is that only objects with weak references have a shorter life cycle.
In the process of the garbage collector thread scanning the memory area under its jurisdiction, once an object with only weak references is found, its memory will be reclaimed regardless of whether the current memory space is sufficient.
However, because the garbage collector is a low-priority thread, objects that only have weak references may not be found quickly.
4. Phantom Reference (PhantomReference)
As the name implies, "virtual reference" is nothing but a virtual reference. Unlike other types of references, a virtual reference does not determine the life cycle of an object.
If an object holds only phantom references, then it is the same as without any references, and may be collected by the garbage collector at any time.
Phantom references are mainly used to track the activities of objects being recycled by the garbage collector.
Three, data type conversion
1. Implicit conversion (automatic type conversion)
The following types are converted directly from left to right, and there will be no error prompt
Conversion of numeric data: byte→short→int→long→float→double.
The character type is converted to an integer type: char→int.
Note:
The addition of float and double will increase the decimal point, and the conversion of float into double will increase the value (this is not recommended)
The char type is special. Char is automatically converted to int, long, float and double.
But byte and short cannot be automatically converted to char, and char cannot be automatically converted to byte or short.
float a1=10.1f;
int a2=2;
double a3=10.2;
int a4=2;
System.out.println(a1*a2);//20.2
System.out.println(a3*a4);//20.4
System.out.println(a1*a2+a3*a4);//40.60000076293945
2. Explicit conversion (forced type conversion)
This conversion will not be performed automatically, because the range of change of the double type is smaller than that of the int type.
This kind of conversion can be called a "shrinking conversion", because you must make the value of the source data type smaller to fit the target data type.
Therefore, when the two data types are incompatible, or the value range of the target type is smaller than the source type, automatic conversion will not be possible, and then forced type conversion is required
Conversion method: (type)variableName
int a=3;
double b=5.0;
a=(int)b;
Changing from large to small will cause loss of accuracy (this is not recommended)
reference
http://c.biancheng.net/view/796.html