java语言基础(变量和运算符)

java八大基本数据类型;

整型{   int(整型)    short(短整型)   long(长整型)}

浮点型{ float(浮点型)   double(双精度)}

布尔{boolean}            字符{char(单个字符,用单引号)}       字节{byte(字节)}

注意:String不是基本类型,是系统类

java三种引用数据类型{ 数组、 类、 接口}

数据类型间的转换

1、自动类型转换

byte ----> short----->int------>long---->float---->double

char----->int------>long---->float---->double

2、其余均为强制转换类型(注意:布尔类型与其他类型不兼容,不能转换)

例:

byte bt = 12;
char ch = (char) bt;//byte与char之间相互强制类型转换
byte b = (byte) ch;
//System.out.println(ch);
short sh = bt;//自动类型转化
short s = (short) ch;//char转short
char c = (char) s;
int i = bt;//byte转int
double d = bt;
long l = (long) d;//double强转long

不同数据类型间的加法,

//byte char short各自做加法时均先自动转换成int加,然后再强转换成结果的类型
byte b_byte = 3;
b_byte = (byte) (b_byte+3);//3为int,so 要强转为byte
System.out.println(b_byte);//6
byte b1 = 3,b2 = 4,b3,b4;
b3 = (byte) (b1+b2);
System.out.println(b3);//7
b4 = 3+4;
System.out.println(b4);//7
int a = 2147483647;
byte re = (byte) a;
System.out.println("强转化后的byte:"+re);//-1

long l1 = 3,l2 = 4,l3;
l3 = l1+l2;
System.out.println(l3);

char c1 = 'a',c2 = 'b',c3;
c3 = (char) (c1+c2);
char c4 = (char) (c1+2);
System.out.println(c4);//c
System.out.println(c3);//?

short s1 = 3,s2 = 4,s3;
s3 = (short) (s1+s2);
System.out.println(s3);//7

 

原文地址:https://www.cnblogs.com/nn369/p/7366263.html