First, the constant
1, constant: the program is running, a fixed amount of unchanged
2, constant classification
- String constants: partially double quotation marks, 'abc'
- Integer constants: integers, no decimal point, 100
- Float constants: decimal points, 2.5
- Character Constants: single quotation marks single character, 'a'
- Boolean constants: true, false
- Empty constants: null. No data on behalf of
3 Notes
- The middle two single quotes must have one and only one character
- Empty constants can not be printed out directly
Second, the data type
1, the data type classification
- The basic data types: integers, floats, characters, boolean
- Reference data types: type, an array, an interface
2, the basic data types (four eight kinds)
- Integer: byte, short, int, long
- Float: float, double
- Character: char
- Boolean: boolean
type of data | Keyword | Memory footprint | Ranges |
---|---|---|---|
Byte | byte | 1 byte | -128~127 |
Short integer | short | 2 bytes | -32768~32767 |
Integer | int | 4 bytes | -231~1 |
Long integer | long | 8 bytes | -263~263-1 |
Single-precision floating-point number | float | 4 bytes | 1.4013E~3.4028E+38 |
Double-precision floating-point number | double | 8 bytes | 4.9E-324 ~ 1.7977E + 308 |
Character | char | 2 bytes | 0~65535 |
Boolean | boolean | 1 byte | true false |
3 Notes
- Basic string data type is not, but the type of reference
- Float may be an approximation, not a precise value
- Range data does not necessarily correlate with the number of bytes, such as a wider range than the data float long, but float is 4 bytes, long 8 bytes
- Float Default is double. To use the float, you need to add the suffix f. To
- The default integer int, to use long, need to add the suffix l.
Third, variable
1, variable
- 程序运行期间,内容可以发生变化
2, create a variable format
数据类型 变量名称;//创建了一个变量
变量名称 数据值;//赋值,将右边的数据值,赋值交给左边
数据类型 变量名称=数据值;//创建一个变量同时赋值
4 Notes
- When you create multiple variables, names between variables can not be repeated.
- with long float type suffix f and l can not lose
- When using the short type variable byte data value can not exceed the range of the left side of the right type
- No variable assignment, it can not be used directly.
- Using Variables can not exceed the scope of scope
- [Scope] start by defining variables, braces belongs to the end
Fourth, data type conversion
- When the data type is not the same, data type conversion occurs
- Automatic type conversion (implicit type conversions)
- Casts (explicit)
1, automatic type conversions
- Features: Code does not require special handling, automatically
- Rules: data range from small to large
2, cast
Features: Code need for special formatting can not be done automatically
Format: Small small range of types of variable name = (small type) had a large range of data
public class demo2 { public static void main(String[] args) { int num = (int)100l; System.out.println(num); } }
3 Notes
- Cast is not recommended, loss of precision may occur, data overflow
- byte, short, char when these three types of data operation occurs, it will first lift an int, then the calculation.