Summarized some small knowledge points, not comprehensive, in order to facilitate the review and review, it is regarded as a note~
table of Contents
1. There are four basic types and eight types:
2. How many bytes does each type occupy?
5. How to compare two floating-point numbers for equality?
1. There are four basic types and eight types:
①Integer: byte short int long
②Floating point number: float double
③Character: char
④Boolean: boolean
2. How many bytes does each type occupy?
3. Packaging
The wrapper class of int is Integer
The packaging class of long is Long
The packaging class of short is Short
The wrapper class of double is Double
Example:
public class Pra0104 {
public static void main(String[] args) {
//int类型所表示范围的最大值和最小值
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
}
4. Literal value
10 represents an int type literal
10L represents a literal of type long
long num=100_0000_1000L; //为了方便读数,也可以通过加下划线的方式
10.0 Represents a literal of type double
10.0f represents a literal of type float
'a' represents the literal of the char type
char num='中'; //由于java中char类型占两个字节,所以也可以是中文字符
System.out.println(num);
The value of boolean type is only true and false
5. How to compare two floating-point numbers for equality?
You cannot use == to compare whether two floating-point numbers are equal, and there will be errors.
The correct way is to subtract the two floating-point numbers to be compared , and compare whether the difference is less than a certain range (the allowable range of error)
6. Type conversion
① Assigning values between variables of different numeric types means that a type with a smaller range can be implicitly converted to a type with a larger range .
②If you need to assign a type with a large range to a type with a small range, you need to force type conversion , but the precision may be lost.
③When assigning a literal constant, Java will automatically check the number range
7. Numerical increase
Byte and byte operation example
As you can see, a wavy line appears in the third row. This is because in the process of a+b, instead of byte+byte, the byte is first converted to int, that is, int+int, the result is still int, and then assigning to c is problematic.
The revised version is as follows:
Summary :
① For mixed operations of different types of data, a small range will be upgraded to a large range.
②For short, byte, which is smaller than 4 bytes, it will be promoted to 4-byte int before calculation
8. Other (About String)
String is not a basic data type, so I will briefly summarize it here.
①Integer to string
int num=10;
String str=""+num;
System.out.println(str);
int num=10;
String str=String.valueOf(num);
System.out.println(str);
②String to integer
String str="100";
int num=Integer.parseInt(str);
//int num=Integer.valueOf(str);
System.out.println(num);