基本数据类型
Java中的数据类型分为基本数据类型和引用数据类型。
基本数据类型:byte(1个字节),short(2字节),char(2字节),int(4字节),long(8字节),float(4字节),double(8字节),boolean(false true)
引用数据类型:类,接口,数组
基本数据类型分为整型,浮点型,字符型和布尔型。
整型:byte,short,int,long(其中默认的数据类型为int类型)
浮点型:float,double(其中默认的是double类型,定义float类型如果有小数点存在,必须在后面加上F/f,float表示的范围比long要大,因为它内存中有一部分用于存放10的幂)
字符型:char(char c ;c中只能放入一个字母或数字)
布尔型:boolean(一般用于循环结构中)
强制类型转换和自动类型转换
自动类型转换:小内存的类型转化为大内存的类型,不用管自动进行转化(byte,short,char在运算时会先转化为int类型)
强制类型转换:大内存的类型转化为小内存的类型
1.必须要有强转符“()”
2.可能会造成精度损失(浮点型转整型会将小数部分全部舍去)
String类型
String类型属于引用类型,声明时需加上“”
String能和8种数据类型做运算,且只能是连接运算,结果仍是String类型
package day01;
public class Test02 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
/*String类型属于引用类型,声明时需加上“”
String能和8种数据类型做运算,且只能是连接运算,结果仍是String类型
*/
String s="姓名";
int age=10;
String s1=s+age;
// System.out.println(s1);
char c='a';
int num=10;
String str="hello";
// String str1=1;
System.out.println(c+num+str);
System.out.println(c+str+num);
System.out.println(c+(num+str));
System.out.println(str+c+num);
char c1=' ';
System.out.println("*"+c1+"*");
}
}
![](https://img-blog.csdnimg.cn/img_convert/1005ef159b3ee6b540d6f33fcacfc8b5.png)