Java定义的数据类型
一、变量按照数据类型来分:
基本数据类型:
整型:byte\short\int\long
浮点型:float\double
字符型:char
布尔型:boolean
引用数据类型:
类(class)
接口(interface)
数组([])(array)
二、变量在类中声明的位置:
成员变量 vs 局部变量
class Test1{
public static void main(String[] args){
byte b1 = 12;
byte b2 = -128;
b2 = 127;
System.out.println(b1);
System.out.println(b2);
short s1 = 128;
int i1 = 1234;//通常使用int
long l1 = 33452489758327636L;//long型变量必须以“l”或“L”结尾
System.out.println(s1);
System.out.println(i1);
System.out.println(l1);
//浮点型,表示带小数点的数值;float表示数值的范围比long还大
float f1 = 12.3F;//定义float必须以“f”或“F”结尾
double d1 = 123.3;//通常使用double
System.out.println(f1 + 1);
System.out.println(d1 + 1);
//字符型:char(1字符=2字节)。
//1、声明一个字符。2、转义字符。3、直接使用 Unicode 值来表示字符型常量
char c1 = 'a';// char c1 = 97;
char c2 = '\n';
char c3 = '\t';//制表符
char c4 = '\u0043';
System.out.println(c2);
System.out.println(c1);
System.out.println(c4);
//布尔型:boolean 1、只能取两个值true、false。2、常常在条件判断、循环结构中使用
// \n表示换行; \\n表示\n
}
}
基本数据类型之间的运算规则:
前提:不包含Boolean。
1、自动类型提升:
当容量小的数据类型与容量大的数据类型做运算是,结果自动提升为容量大的数据类型。
byte、char、short --> int --> long --> float --> double
**特别的:当byte、char、short三种类型的变量做运算是,结果为int型。
对于整型,默认就为int。
对于浮点型,默认为double。
2、强制类型转换:在Test3中。
class Test2{
public static void main(String[] args){
byte b1 = 2;
int i1 = 12;
//编译不通过 byte b2 = b1 + i1;
int i2 = b1 + i1;
long l1 = b1 + i1;
System.out.println(i2);
System.out.println(l1);
float f1 = b1 + i1;
System.out.println(f1);
//**************特别的*******************
char c1 = 'a';
int i3 = 10;
int i4 = c1 + i3;
System.out.println(i4);
short s1 = 10;
//char c2 = c1 + s1; 编译不通过
byte b2 = 10;
//char c3 = c1 + b2; 编译不通过
//short s2 = b2 + s2; 编译不通过
//short s4 = b1 + b2; 编译不通过
System.out.println(c1 + i3); // 输出107
}
}
强制类型转换:自动类型提升运算的逆运算。
1、需要使用强转符:()
2、注意点:强制类型转换,可能导致精度损失。
class Test3{
public static void main(String[] args){
double d1 = 12.3;
int i1 = (int)d1;//截断操作 12.9999———>12.精度损失
System.out.println(i1);
int i2 = 128;
byte b = (byte)i2;
System.out.println(b);//输出 -128 精度损失
}
}
String类型变量的使用:
1、String属于引用数据类型
2、声明String类型变量时,使用一对""。
3、可以和8种基本数据类型做运算,且运算只能是“连接”运算:+ ,运算的结果还是String类型。
class Test4{
public static void main(String[] args){
String s1 = "Hello World!";
System.out.println(s1);
String s2 = "a";
String s3 = "";
//char c1 = ''; 编译不通过!!
//*********************************
int number = 1001;
String numberStr = "学号:";
boolean b1 = true;
String info = numberStr + number + b1;// +:*连接*运算 输出:学号:1001true
System.out.println(info);
//String str1 = 123; 编译不通过;
//String str1 = (int)123; 编译不通过;
String str1 = 123 + "";// "123"
//int i1 = (int)str1; 编译不通过;
int num1 = Integer.parseInt(str1);
System.out.println(num1);
//练习1
char c = 'a';
int num = 10;
String str = "Hello";
System.out.println(c + num + str);//107Hello
System.out.println(c + str + num);//aHello10
System.out.println(c + (num + str));//a10Hello
System.out.println((c + num) + str);//107Hello
System.out.println(str + num + c);//Hello10a
//练习2
// * *
System.out.println("* *");// * *
System.out.println('*' + '\t' + '*');// 93
System.out.println('*' + "\t" + '*');// * *
System.out.println("*" + "\t" + "*");// * *
System.out.println('*' + '\t' + "*");// 51*
System.out.println('*' + ('\t' + "*"));// * *
}
}

2万+

被折叠的 条评论
为什么被折叠?



