一、 八大基本数据类型
整数类型
- byte(1个字节):-128~127
- short(2个字节):-32768~32767
- int (4个字节):-2147483648~2147483647(约21亿)
- long(八个字节):-92233720368545808~92233720368545807
浮点类型
- float(4个字节)
- double(8个字节)
```
//浮点数拓展
float a=0.1f;
double b=1.0/10;
System.out.println(a);
System.out.println(b);
System.out.println(a==b); //输出为false
int c=10_0000_0000;
System.out.println(c);
```
字符类型
- char(2个字节)
```
char a='中';
char b='A';
```
#### 布尔类型
- boolean:占1位,值只有true、false
-
强制类型转换
自动类型转换
- 低—>高
强制类型转换
- (类型)变量名 高 —> 低
注意点:
1.不能对布尔值进行转换
2. 不能把对象类型转换为不相干的类型
3. 转换时可能出现内存溢出,或者精度问题!
4. 把高容量转换为低容量时要用强制转换!
二、变量
- 类变量
- 实例变量
- 局部变量
public class Demo{
//类变量 static
static double salary = 2500;
//实例变量 从属于对象;如果不进行初始化,则有一个默认值
//布尔值默认为false,基本类型默认值为0,0.0,其余默认值为null
String name;
int age;
public static void main(String[] args) {
//局部变量,必须先声明和初始化
int i=10;
System.out.println(i);
//变量类型 变量名字=new 变量类型()
Demo demo=new Demo();
System.out.println(demo.age);
System.out.println(demo.name);
//类变量
System.out.println(salary);
}
}