Home > Back-end >  Java basic data types
Java basic data types

Time:02-17

A, the basic data type

The Java language provides eight basic types, six types of digital (four integer, floating point), a kind of character types, there is also a Boolean



Integer, in does not specify any type of cases, the default is an int type;

Byte:
Example: byte a=100, byte b=- 50,

Short:
Example: short s=100, short r=200,

Int:
Example: int a=1000, int b=2000,

Long:
Example: long a=100000 l, long l=b - 200000,
This type is mainly used in need of large integer system;
Note: "L" in theory, case insensitive, but if written as "L" easily confused with the number "1", it is not easy to determine, so it's best to uppercase,

Integer hexadecimal notation:

1, hex (0 x)
Byte b=0 xf. Short s=0 xf. Int I=0 xf. Long L=0 XFL;

2, octal (0)
Byte b=077; Short s=077; Int I=077; Long L=077 L;

3, the binary (0 b)
Byte b=0 b1; Short s=0 b1; Int I=0 b1; Long L=0 b1l;

Floating point, in does not specify any type of cases, the default is the type double;

Float:
Floating point Numbers cannot be used to indicate the precise values, such as currency;
Example: float f1=234.5 f,//f or f

Double:
Type double the same cannot be said precise values, such as currency;
Example: double d1=123.4,//[optional] d or d

Boolean:
Said a Boolean data type information;
There are only two values: true and false;
This type is only as a symbol to record the true/false information;
The default value is false.
Example: a Boolean one=true,

Char:
The char type is a single 16-bit Unicode characters;
Char data type can store any character;
Example: char letter='A'; ,

Floating point:
Float (32) : 1 - (the sign bit) 8 bits (index) of 23 bits (tail)
Double (64) : 1 - (the sign bit) 11 bits (index) 52 bits (tail)

Two, the basic data type wrapper class

Each basic types in Java. Java lang in the package there is a corresponding wrapper class

And how does wrapper class
1, provide a series of practical methods
2, the collection is not allowed to deposit the basic data types, deposit number, want to use the packing type



Conclusion: in addition to int and the char type wrapper class is full, the rest of the wrapper classes are capitalize the first letter

The eight kinds of wrapper classes inherited the parent class is not all the same,
1, Integer, Float, Double, Short, Byte, Long belong to a subclass of Number class, Number class itself provides a series of, return the above six basic data types of operation,
2, the Character belongs to the Object subclass
3, belong to a Boolean Object subclass,

Packing and unpacking
1, the basic data type packing into a class called,
2, the wrapper class type into basic data type is called split open a case,
3, after JDK1.5, provides the function of automatic packing and unpacking,

Three, the data type conversion

1, automatic type conversion (implicit type conversion)

Integer, character data can be mixed operation,

Operation, the different types of data into the same type first, and then carry out calculations,

Conversion from junior to senior,


Automatic conversion has the following pattern:

Small type automatically translate into large type

Integer types can be automatically converted to floating point types, may produce rounding error

Characters can be automatically promoted to integer

2, casts (explicit type conversion)

Format: (type) value

Type is casts after the data type of the

Note:

Casting precision overflow or damage may result in

In the large capacity type conversion for casting must be used in capacity of small type

Transformation of floating point Numbers to integers is by abandoning the decimal, rather than rounded

Example:
Byte byte1=127;//this value is the maximum value at present: 2 ^ 7-1
Short short1=32767;//this value is the maximum value at present: 2 ^ 15-1
Int int1=2147483647;//this value is the maximum value at present: 2 ^ 31-1
Long long1=9223372036854775807 l;//this value is the maximum value at present: 2 ^ 63-1
Float float1 e38 F=3.4028235;//this value is the maximum value at present: 2 ^ 128 valuation
Double double1 e308=1.7976931348623157;//this value is the maximum value at present: 2 ^ 1024 valuation

//implicit conversion

//from byte implicit conversion to short
Short s1=byte1;
//from a short implicit conversion to int
Int i1=short1;
//int implicit conversion to long
Long l1=int1;
//from long implicit conversion to float (floating point Numbers: single precision)
Float f1=long1;
//from the float (floating point Numbers: single precision) implicit conversion to a double (floating point Numbers: double)
Double d2=float1;

//display conversion

//from double (floating point Numbers: double) display switch to float (floating point Numbers: single precision)
Float f2=(float) double1;
//from the float (floating point Numbers: single precision) shows into long
Long l2=(long) float1;
//from the long show to int
Int i2=(int) long1;
//from the int shows to short
Short s2=(short) int1;
//from the short display to byte
Byte b2=(byte) short1;