数据类型
Java数据类型
基本数据类型(primitive type)
- 整数类型
- byte占一字节范围:-128-127
- short占2个字节范围:-32768-32767
- int占4个字节范围:-2147483648-2147483647
- long占8个字节范围:-9223372036854775808-9223372036854775807
- 浮点类型
- 字符型:char占2个字节
- boolean类型:占1位其值只有true和false两个
public class Demo00{
public static void main(String[] args){
int num1 = 10;
byte num2 = 20;
short num3 = 30;
long num4 = 30L;
float num5 = 50.1F;
double num6 = 3.141592653589793238462643;
char name = '国';
String namea = "龙渊";
boolean flag = true;
}
}
引用数据类型(reference type)
数据类型拓展
- 整数拓展:进制 二进制0b 十进制 八进制0 十六进制0x
public class Demo01 {
public static void main(String[] args){
int i = 10;
int i2 = 010;
int i3 = 0x10;
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
}
}
- 浮点数拓展:float / double 有限、离散、舍入误差 最好完全避免使用浮点数进行比较。
- 银行业务如何表示钱? 数学工具类 BigDecimal
public class Demo01 {
public static void main(String[] args){
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d);
float d1 = 2312323232153221313f;
float d2 = d1 + 1;
System.out.println(d1==d2);
}
}
public class Demo01 {
public static void main(String[] args){
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);
System.out.println(c2);
System.out.println((int)c2);
char c3 = '\u0061';
System.out.println(c3);
System.out.println("hello\nworld");
}
}
数据类型转换
public class Demo02 {
public static void main(String[] args){
int i = 128;
byte b = (byte)i;
System.out.println(i);
System.out.println(b);
int a = 128;
float c = a;
System.out.println(a);
System.out.println(c);
System.out.println("===========================");
System.out.println((int)23.7);
System.out.println((int)-45.89f);
System.out.println("============================");
char e = 'a';
int f = e + 1;
System.out.println(e);
System.out.println((char)f);
int money = 10_0000_0000;
int years = 20;
int total = money*years;
System.out.println(total);
long total3 = money*((long)years);
System.out.println(total3);
}
}