Java based learning (four) ---- constants, variables, data types
[Note]
another update, interested can look at
First, the constants and variables
1. Constant
1. Overview
refers to the Java program 运行期间固定不变
amounts
2. Classification
Types of | meaning | For example |
---|---|---|
Integer constant | All integer | -1,0, 1 |
Decimal constant | All decimals | -9.9,-0.1,7.62 |
Character constant | Single quotation marks, can only write a character, and必须有内容 |
'C', 'I' |
String constant | Double quotation marks, you can have more than one character, you can not write | "CSDN", "Hello" |
Boolean constants | Only two values | true,false |
Constant air | Only one value | null |
3. Constant Use Case
public class Constant {
public static void main(String[] args) {
System.out.print("整数常量:");
System.out.println(15);
System.out.println("-------------------");
System.out.print("小数常量:");
System.out.println(-5.6);
System.out.println("-------------------");
System.out.print("字符常量:");
System.out.println('C');
System.out.println("-------------------");
System.out.print("字符串常量:");
System.out.println("CSDN");
System.out.println("-------------------");
System.out.print("布尔型常量:");
System.out.println(true);
System.out.println("-------------------");
System.out.print("空常量:");
System.out.println("“null”不能直接打印");
}
}
2. Variable
1. Overview
constant amount is fixed, then in a program 可以变化的量
called variables.
Java, requires a variable can only hold one data must be explicitly saved data type
definitions Format of variables (two kinds)
(1)数据类型 变量名称;//创建了一个变量
int a;
变量名称 = 数据值;//赋值,将右边的数据值,赋值给左边的变量
a=10;
(2)数据类型 变量名称 = 数据值;//在创建一个变量的同时,立刻放入指定的数据值
int a = 10;
3. Notes |
---|
(1) If you create more than one variable, the variable name can not be repeated |
(2) for float and long for letter suffixes L and F must be supporting the use of |
Before you can use (3) If no variable assignment, can not be used directly, after the assignment must be |
(4) using the variable range can not exceed the scope |
(5) You can create multiple variables through a statement, but is not recommended |
Second, the data type
1. The basic data types
type of data | Keyword | length | Ranges |
---|---|---|---|
Byte | byte | 8th | -128~127 |
Short integer | short | # 16 | -32768~32767 |
Integer | int (default) | # 32 | -231~231-1 |
Long integer | long | # 64 | -263~263-1 |
Single-precision floating-point number | float | # 32 | 1.4e-45f ~ 3.4028235e + 38f |
Double-precision floating-point number | double (default) | # 64 | 4.9e-324 ~ 1.797693e + 308 |
Character | char | # 16 | 0~65535 |
Boolean | boolean | 8th | true、false |
2. The reference data types
- String
- Array
- class
- interface
- lambda
3. Notes
Basic string data type is not, but the reference data type |
---|
Float may only be an approximation, not an exact value |
Byte size data does not necessarily correlate with the range of , for example, float (. 4-byte) data range is broader than long (8 bytes) (as the float used scientific notation) |
The default is double floating-point type which, if we must use the float type, you need to add the letter F |
If it is an integer, the default type int, if we must use the long type you need to add a suffix letter L |
Third, the data type conversion
1. Automatic conversion (implicit)
1. Features: code does not require special treatment,自动完成
2. Rules: The value 范围小
of the type 转换
for the value 范围大
of the type
3. Code Example
//======================================================
long n1 = 10;
//左边是long类型,右边是默认的int类型,左右数据类型不一样
//等号代表赋值,将右侧的int,交给左侧的long进行存储
//int→long符合范围小向范围大的数据类型转换,所以发生了自动转换
System.out.print1n(n1); //结果为10
//======================================================
double n2 = 0.5F;
//左边是double类型,右边是float类型,左右数据类型不一样
//等号将左侧赋值给右侧
//float→double符合范围小向范围大的数据类型转换,所以发生了自动转换
System.out.println(n2); //结果为0.5
//======================================================
float n3 = 15L;
//左边是float类型,右边是long类型,左右数据类型不一样
//等号将左侧赋值给右侧
//long→float符合范围小向范围大的数据类型转换,所以发生了自动转换
System.out.print1n(n3); //结果为15.0
//======================================================
2. cast (explicit)
1. Features: Code require special handling , 不能自动完成
2. rules: The value 范围大
type 转换
of the value 范围小
of the type
3 code format
int m = 10;
//范围小的类型 范围小的变量名 = (范围小的类型) 原范围大的数据类型
short n = (short) m;
3. Code Example
//======================================================
int n1 = (int) 66L;
//左边是int类型,右边是long类型,左右数据类型不一样
//等号将左侧赋值给右侧
//long→int,是范围大向范围小的数据类型转换,需要编写代码实现强制类型转换
System.out.println(n1);//结果为66
//======================================================
int n2 = (int) 100000000000L;
//如果右侧数据超过左侧数据范围,会造成数据溢出,结果也将不再准确
System.out.println(n2);//结果为1215752192
//======================================================
int n3 = (int) 9.9;
//double→int,并不是四舍五入,而是整数后的直接丢弃
System.out.println(n3);//结果为9
//======================================================
byte n4 = 4;
byte n5 = 5;
int n6 = n4 + n5;
//byte+byte → int+int → int
System.out.println(n6);//结果为9
//======================================================
short n7 = 7;
byte n8 = 8;
short n9 =(short)(n7 + n8);
//short+byte → int+int → int → short = short
System.out.println(n9);//结果为15
//======================================================
【tips】
1. Force Data type conversion is generally not recommended, because the loss of precision may occur, data overflow
2.byte, short, char three types of mathematical operations can occur, when the lifting operation is an int, then calculates
3.boolean type data type conversion can not occur