Strongly typed and weakly typed languages
- All variables must be defined before they can be used
Java data types fall into two broad categories
- Primitive type
- Reference Type
What is a byte
- Bit: The smallest unit of internal data storage in a computer. 11001100 is an eight-bit binary number.
- 1. The basic unit of data processing in a computer, conventionally represented by a capital B,
- 1B(byte) = 8 bits
- Characters: letters, numbers, words and symbols used in computers
1bit represents 1bit and 1 byte represents a byte 1B= 8B. 1024B=1KB 1024KB=1M 1024M=1G.
Type conversion
- Because Java is a strongly typed language, some operations need to be performed using type conversions.
- Low — — — — — — — — > high
byte.short.char-> int -> long-> float ->double
Copy the code
- In an operation, different types of data are first converted to the same type and then the operation is performed.
Cast casting
Variable name high --> lowCopy the code
System.out.println((int)22.5); / / 23
System.out.println((int) -12.13 f); / / - 12
Copy the code
Automatic type conversion
Low - > highCopy the code
int i = 128;
double a = i;
System.out.println(i); / / 128
System.out.println(a); / / 128.0
Copy the code
Note:
- Boolean values cannot be converted
- Object types cannot be converted to non-1000 types
- Cast when converting high capacity to low capacity
- There may be memory overflow or precision problems during conversion!
Memory overflow cases and solutions
- It is easy to run out of memory when working with large numbers
// new feature in JDK7, numbers can be separated by underscores
int money = 10 _0000_0000;
System.out.println(money); / / 1000000000
int years = 20;
int total = money*years; //-1474836480
long total2 = money*years; // Default is int, overflow problem already existed before conversion
System.out.println(total2); / / - 1474836480
long total3 = money*((long)years); // Convert a number to long
System.out.println(total3); / / 20000000000
Copy the code