Basic data types
1. Bits and bytes
1. Bit bit: The smallest unit of data storage inside a computer
2. Byte: The basic unit of data processing in a computer
3. Word: When computer data processing, the length of data access, processing and transmission is called word
The processor | byte | word | Word length |
---|---|---|---|
16 | 2 | Consists of two bytes | 16 |
32 – | 4 | It consists of four bytes | 32 |
A 64 – bit | 16 | It consists of 16 bytes | 64 |
4. 1byte = 8bit
2. Base and conversion
1. Binary: 0,1; Full 2 into 1; It starts with an 0B or an 0B
2. Decimal: 0~9; With 10 in 1
Octal: 0~7; If 8, enter 1; 0 at the beginning
4. Hexadecimal: 0-9 and A-E(case insensitive); Full 16 to 1; It starts with 0X or 0X
5. Base conversion: octal <–> binary <–> hexadecimal
3, the original code complement code inverse
The bottom of the computer is to use complement code to save data
The highest bit of a binary number in a computer represents a sign bit
- The source, inverse and complement of positive numbers are the same
- The source code for negative numbers takes the absolute value, which is expressed as binary, and the sign bit is 1
- Reverse code: divide the sign bit to the original code
- Complement: Add 1 to the inverse
binary | Said the number | instructions |
---|---|---|
0000, 0000, | 0 | The highest bit represents the symbol |
0111, 1111, | 127 | The highest bit represents the symbol |
1000, 0001, | – 1 | The highest bit represents the symbol |
1111, 1111, | – 127. | The highest bit represents the symbol |
1000, 0000, | – 128. | A negative number is minus 0 for minus 128 |
- Range of 8-bit binary numbers in a computer: [1000 0000, 0111 1111]
- Because the computer saves the data by inverse code, 1000 0000 is the complement form, minus 1 to get the inverse code 0111 1111, inverse to get the original code 1000 0000, converted to decimal is 128, plus the sign, namely -128
4. Identifier
The character Java uses to name variables, methods, classes, interfaces, packages, etc
Any place with its own name is called an identifier
1. Naming rules
- The value consists of letters, digits, underscores (_), and $(.)
- You can’t start with a number
- Cannot conflict with the keyword or reserved word
- Strictly case sensitive
2. Naming conventions
- Package name: xxxyyyzzz
- Class name, interface name: XxxYyyZzz
- Variable name, method name: xxxYyyZzz
- Constant name: XXX or XXX_YYY_ZZZ
3. Name is name
5, variables,
1. The concept
- A storage area in memory
- The data in this region can vary over time within the same type range
- Variables are the most basic storage unit in a program
2. Declaration and assignment
- Data type variable name = value;
3. The scope
- Defined in a pair of {}
- Only valid within its scope
- You cannot declare two variables with the same name in the same scope
4. Where variables are declared in a class
6. Basic data types
type | Occupying Storage space | Table number range |
---|---|---|
byte |
1 byte | Can be said The number: ~ = > 128 ~ 127 |
short |
2 – | : – 32768 ~ 32767 |
int |
4 bytes | : ~ |
long |
8 bytes | : ~ |
float |
4 bytes | 3.403 e38 ~ 3.403 e38 |
double |
8 bytes | 1.798 e308 ~ 1.798 e308 |
char |
2 – | Represents a single character |
boolean |
4 bytes (will be compiled toint Type) |
false/true |
- define
char
It can be a character or a number, but the output is the character corresponding to the number - Boolean
false
andtrue
It doesn’t represent 0 and 1, it’s the outputtrue
andfalse
, will not output 1 or 0 float
andint
It’s also 4 bytes. Why is the range greater thanlong
The range is larger because it uses one part to represent specific single or double values, and the rest to represent the number of powers of 10
6.1 Automatic Type Promotion
- When a variable of a small data type is evaluated with a variable of a large data type, the result is automatically promoted to a large data type.
- Byte, char, short –> int –> long –> float –> double
- In particular: byte, char, short results in int
- The size of the volume refers to the size of the range of numbers. For example, float has more capacity than long
6.2 Cast Type
- Integer constants default is
int
type- Floating-point constants default is
double
type
- Reverse operation of automatic type promotion, using strong characters
- There is a loss of precision, such as float from decimal to int (just take the integer part)
- Definition of long without an L but within the range of int does not generate an error due to automatic type promotion