I. Introduction
Java data types are divided into two kinds, namely, basic data types and reference data types; java data type is strong data type, meaning that each declare a variable, it must have the corresponding data types; interview frequently asked java8 large basic data type is actually the basic data types; it is important to lay the foundation of data types you want to shut;
Two basic data types
Basic data types into an integer 4, a boolean, a character, floating point type 2; the size of each data type is different, the size of the data type in bytes, the byte is a kind of computer It is equal to eight, i.e. the method 8;
2.1 Integer
Integer intended name suggests, is an integer type field declaration, the time field is assigned only integer data type; if it is a long integer (long) tape behind writing or L represents a long data l ;
Writing exemplary type int:
public class Type {
// int 型 java默认为整型
private int zszxz1 = 1327;
}
Writing exemplary type long:
public class Type {
// long 型 Java中称呼为长整型,后面跟 L 或者 l,由于l同 1很像,通常使用L
private long zszxz2 = 1327L;
}
Example shor type writing:
public class Type {
// shor 型 java中称呼为短整型
private short zxzxz3 = 1327;
}
Writing a byte Example:
public class Type {
// byte 型 java中称呼为字节
private byte zszxz4 = 127;
}
2.2 floating-point type
Single-precision floating-point data types into type (float) and double precision (double); double-type, or D to bring back the digital d represent double writing;
writing float type, after the number or F represents a single band f -precision; IEEE 745 specification which follow, the following three cases, if an overflow occurs:
- Positive infinity
- Negative infinity
- Nan represent non-numeric
Writing example:
public class Type {
// float 型
private float zszxz5 = 3.17f;
// double 型
private double zszxz6 = 3.17d;
}
2.3 Character
Char (char) stores a single character, using special character unicode values (0 to 65535) for storing; 2 bytes in size;
public static void main(String[] args) {
// 字符 A 在 unicode中的值是65也就是 u0041;
char zszxz7 = 'A';
char zszxz8 = '\u0041';
char zszxz9 = 65;
// true
System.out.println(zszxz7==zszxz8);
// true
System.out.println(zszxz7==zszxz9);
}
Common escape signals:
2.4 Boolean
Boolean (boolean), which is only two values of true and false; generally configured to determine non;
// 正确
private boolean zszxz10 = true;
// 错误
private boolean zszxz11 = false;
Ternary numerical representation
3.1 binary number
0b binary number used at the beginning or 0B, are each represented by 0 to 1;
Example:
// 二进制数
private int zszxz12 = 0b1111;
3.2 octal
Octal number to use at the beginning of 0, 0 to 7 each represent;
Example:
public static void main(String[] args) {
int zszxz13 = 017;
// 15
System.out.println(zszxz13);
}
3.3 hexadecimal number
Using a hexadecimal number or a leading 0x 0X, are used per 0 ~ 9, A ~ F represents;
Example:
public static void main(String[] args) {
int zszxz14 = 0xff;
// 255
System.out.println(zszxz14);
}
Four index values indicate
Using e represents 10 - power in java index type, such as shown as 10 ^ 5 E5, note the use of floating-point data type or other types of large numerical declaration;
Example:
public static void main(String[] args) {
double zszxz15 = 0.3e2;
// 30.0
System.out.println(zszxz15);
}
Five casts
5.1 automatic conversion
Automatic type conversions are small turn big data type data type java virtual machine automatically help you convert, do not need to do extra work;
Accuracy is not lost:
Accuracy is lost:
5.2 cast
Cast refers to turning big data type small data type needs to be cast, but there is the risk of loss of precision may occur, the use of () represents the cast; the rules at the time of loss of precision floating-point integer not turn rounding but the interception;
Example:
public static void main(String[] args) {
double zszxz16 = 55.9;
int opq = 0 ;
opq = (int)zszxz16;
// 55
System.out.println(opq);
}
Six reference data types
Data types include references cited type essentially identical to the reference c is a pointer, but not the argument pointer in java, but retained a pointer classes, interfaces, arrays, the java; special data type is a reference to the data type String ; detailed reference data type in a subsequent study will have a good understanding, for starters, say more at present no meaning;