目录
1.字符型char
- char占用2个字节。
- char的取值范围:[0-65535]
- char采用unicode编码方式。
- char类型的字面量使用单引号括起来。
- char可以存储一个汉字。
public class CharTest01{
public static void main(String[] args){
char c1 = '中';// char可以存储1个汉字,汉字占用2个字节,java中的char类型占用2个字节
System.out.println(c1);
char c2 = 'a';
System.out.println(c2);
char c3 = '0';// 0如果加上单引号的话,0就不是数字0了,就是文字0,它是1个字符。
System.out.println(c3);
//char c4 = "a";// 错误: 不兼容的类型: String无法转换为char
//char c5 = 'ab';// 错误: 未结束的字符文字
//char c6 = '1.08';// 错误: 未结束的字符文字
}
}
2.转义字符
java语言中“\”负责转义。\t 表示制表符tab
public class CharTest02{
public static void main(String[] args){
// 普通的't'字符
char c1 = 't';
System.out.println(c1);//t
char c2 = '\t'; //相当于键盘上的tab键
System.out.println("abcdef");//abcdef
System.out.println("abctdef");//abcdef
System.out.println("abc\tdef");//abc def
System.out.print("HelloWorld");//不换行 HelloWorld123abcdef
System.out.println("123abcdef");//换行
char c3 = '\n'; // 换行符
//System.out.println(''');// 错误: 空字符文字
System.out.println('\'');//正确,输出 ' 字符
//System.out.println('\');//错误: 未结束的字符文字
System.out.println('\\');//输出 \ 字符
//System.out.println(""test"");// 错误: 需要')'
System.out.println("“test”"); //输出“test” 内部的双引号可以用中文
//System.out.println(""");// 编译报错。
System.out.println("\"");//输出"
System.out.println("\"test\"");//"test"
System.out.println("'你好'");// '你好'
System.out.println("'");// 输出' 这个不需要专门进行转义。这个 ' 在这里只是一个普通的字符,不具备特殊含义。
//System.out.println(''');//不行
//System.out.println(""");//不行
//char x = '4e2d';// 错误: 未结束的字符文字
char x = '\u4e2d';// 反斜杠u表示后面的是一个字符的unicode编码。unicode编码是十六进制的。
System.out.println(x); // '中'
}
}
3.整数型byte short int long
整数型在java语言中共4种类型:
- byte:1个字节,最大值127
- short:2个字节 ,最大值32767
- int (最常用的):4个字节,2147483647是int最大值,超了这个范围可以使用long类型。
- long:8个字节
注:1个字节 = 8个二进制位(1byte = 8bit)
在java语言中整数型字面量有4种表示形式:
- 十进制:最常用的,不需要前缀。
- 二进制:数据以0b/0B打头
- 八进制:数据以0打头
- 十六进制:数据以0x打头
代码 IntTest01:
public class IntTest01{
public static void main(String[] args){
// 十进制
int a = 10;
System.out.println(a); // 10
// 八进制
int b = 010;
System.out.println(b); // 8
// 十六进制
int c = 0x10;
System.out.println(c); // 16
int x = 16; //十进制方式
System.out.println(x);//16
// 二进制(JDK8的新特性,低版本不支持。)
int d = 0b10;
System.out.println(d); // 2
}
}
3.1自动类型转换
小容量可以自动转换成大容量,这种操作被称为:自动类型转换。
在任何情况下,整数型的“字面量/数据”默认被当做int类型处理。
如果希望该“整数型字面量”被当做long类型来处理,需要在“字面量”后面添加L/l,建议使用大写L 。
b变量是long类型,int类型占4个字节,long类型占8个字节。
//int类型赋值给int类型,不存在类型转换
int a = 100;
System.out.println(a);//100
//int类型赋值给long类型,存在类型转换
long b = 200;
System.out.println(b);
//不存在类型转换
long c = 300L;
System.out.println(c);
long e = 2147483648;//报错,整数太大
在java中,整数型字面量一上来编译器就会将它看做int类型。
2147483648已经超出了int的范围,所以在没有赋值之前就出错了。不是e放不下2147483648,e是long类型,完全可以容纳2147483648。
解决:
long e = 2147483648L;
System.out.println(e);
3.2强制类型转换
大容量转向小容量会报错有损失,想要编译通过需要加强制类型转换符,虽然编译通过但运行时可能会损失精度。
例子:long 转 int
long x = 100L;
int y = (int)x;//(int)强制转换
long类型100L转int的实现逻辑:
00000000 00000000 00000000 00000000 00000000 00000000 00000000 01100100
强转为int类型会自动将“前面”的4个字节砍掉:00000000 00000000 00000000 01100100
例子:int 转 byte
byte b = (byte)300;
System.out.println(b); // 44
300的int类型对应的二进制:00000000 00000000 00000001 00101100
byte占用1个字节,砍掉前3个字节,结果是:00101100 (44)
3.3不同类型赋值
java中有一个语法规则:
当这个整数型字面量没有超出byte/short/char的取值范围,则可以直接赋值。
例子:int 赋值给 byte/short
byte x = 1; //正常运行
byte y = 127;//正常运行
byte z = 128;// 错误: 不兼容的类型: 从int转换到byte可能会有损失
short s = 1;//正常运行
short s1 = 32767;//正常运行
short s2 = 32768;//错误: 不兼容的类型: 从int转换到short可能会有损失
例子:int赋值给char
char c2 = 97;
System.out.println(c2); // 'a'
// char类型取值范围:[0~65535]
char c3 = 65535; // 实际上最终是一个“看不懂”的字符。
System.out.println(c3); //不会报错,输出?
char类型取值范围:[0~65535],超过范围需要加强制转换:char c4 = (char)65536;
4.浮点型float double
float - 4个字节 - 单精度
double - 8个字节 - 双精度
10.0 / 3 采用float来存储的话结果可能是:3.33333
10.0 / 3 采用double来存储的话结果可能是:3.3333333333333
补充1:long类型占用8个字节。float类型占用4个字节。哪个容量大?
任意一个浮点型都比整数型空间大。
float容量 > long容量。
补充2:java中规定,任何一个浮点型数据默认被当做double来处理。如果想让这个浮点型字面量被当做float类型来处理,那么请在字面量后面添加F/f。如3.14f
补充3:如果用在银行方面或者说使用在财务方面,double也是远远不够的,在java中提供了一种精度更高的类型,这种类型专门使用在财务软件方面:java.math.BigDecimal (不是基本数据类型,属于引用数据类型。)
public class FloatTest01{
public static void main(String[] args){
double pi = 3.1415926;
System.out.println(pi);//默认double类型
//float f = 3.14;//错误: 不兼容的类型: 从double转换到float可能会有损失
//修改方式1:在字面量后面添加F/f
//float f = 3.14f;
//float f = 3.14F;
//修改方式2:强制类型转换,但可能损失精度。谨慎使用。
float f = (float)3.14;
System.out.println(f);
//int i = 10.0 / 5; //错误: 不兼容的类型: 从double转换到int可能会有损失
//修改方法1:
int i = (int)10.0 / 5;
System.out.println(i); // 2
//修改方法2:
int x = (int)(10.0 / 5);
System.out.println(x); // 2
}
}
5.布尔型boolean
在java语言中boolean类型只有两个值true和false,没有其他值。
C语言中1和0也可以表示布尔类型。
boolean xingBie = 1;//错误: 不兼容的类型: int无法转换为boolean
boolean类型在实际开发中通常使用在逻辑判断当中,通常放到条件的位置上(充当条件)。
// 需求规定:如果为true则表示男,为false则表示女。
boolean sex = false;
if(sex){
System.out.println("男");
}else{
System.out.println("女");
}
6.byte short char 的混合运算
byte、char、short做混合运算的时候,各自先转换成int再做运算。
public class IntTest06{
public static void main(String[] args){
char c1 = 'a';
byte b = 1;
System.out.println(c1 + b); // 98 注意:这里的"+"是负责求和的
//下面在这样写不可以
//short s = c1 + b;//错误: 不兼容的类型: 从int转换到short可能会有损失
//short s = (short)c1 + b;//错误: 不兼容的类型: 从int转换到short可能会有损失
//下面这样写可以
short s = (short)(c1 + b);
}
}
7.多种数据类型混合运算
多种数据类型做混合运算的时候,最终的结果类型是“最大容量”对应的类型。
long a = 10L;
char c = 'a';
short s = 100;
int i = 30;
最大容量是long
public class IntTest07{
public static void main(String[] args){
long a = 10L;
char c = 'a';
short s = 100;
int i = 30;
System.out.println(a + c + s + i); //237 求和
//int x = a + c + s + i;错误: 不兼容的类型: 从long转换到int可能会有损失。a + c + s + i计算结果是long类型
int x = (int)(a + c + s + i);
System.out.println(x);//237
int temp = 10 / 3;
System.out.println(temp); // 结果是:3 最终取整
int temp2 = 1 / 2;
System.out.println(temp2); // 0
}
}
8.类型转换的规则总结
1、八种基本数据类型中,除 boolean 类型不能转换,剩下七种类型之间都可以进行转换;
2、如果整数型字面量没有超出 byte,short,char 的取值范围,可以直接将其赋值给byte,short,char 类型的变量;
3、自动类型转换:小容量向大容量转换,容量从小到大的排序为:byte < short(char) < int < long < float < double,其中 short和 char都占用两个字节,但是char 可以表示更大的正整数;
4、强制类型转换:大容量转换成小容量,编写时必须添加“强制类型转换符”,但运行时可能出现精度损失,谨慎使用;
5、byte,short,char 类型混合运算时,先各自转换成 int 类型再做运算;
6、多种数据类型混合运算,各自先转换成容量最大的那一种再做运算。