java求1到20000000的和_Java数据类型的转换(一)

AAffA0nNPuCLAAAAAElFTkSuQmCC

1. Java基本数据类型:byte(1字节),  short(2字节),  char(2字节),  int(4字节),  long(8字节),  float(4字节),  double(8字节),  boolean

Java基本数据类型的包装类型:Byte,Short,Character,Integer,Long,Float,Double,Boolean

2. 基本数据类型所能表示数的范围(精度)与默认值

byte(8位): 最小值 -128(-2^7),最大值 127(2^7), 默认值为:0

short(16位): 最小值 -32768(-2^15),最大值 32767(2^15 - 1),默认值为:0

char(16位Unicode字符):最小值是 \u0000(即为0),最大值是 \uffff(即为65,535),默认值为:'u0000'

int(32位): 最小值 -2,147,483,648(-2^31),最大值 2,147,483,647(2^31 - 1) ,默认值为:0

long(64位):最小值是 -9,223,372,036,854,775,808(-2^63),最大值是 9,223,372,036,854,775,807(2^63 -1),默认值为:0L

float(32位):最小值:1.4E-45,最大值:3.4028235E38,默认值为:0.0f

double(64位):最小值:4.9E-324,最大值:1.7976931348623157E308,默认值为:0.0d

整数默认是int类型,浮点数默认是double类型

3. 装箱与拆箱

装箱就是自动将基本数据类型转换为包装类型;拆箱就是自动将包装类型转换为基本数据类型。Byte b = 1;  //自动装箱

byte b1 = b;  //自动拆箱(下同)

Short s = 2;

short s1 = s;

Character c = 'F';

char c1 = c;

Integer i = 100;

int i1 = i;

Long l = 100L;   // l和L均可

long l1 = l;

Float f = 3.14F; // f和F均可

float f1 = f;

Double d = 3.14;

double d1 = d;

Boolean bool = true;

boolean bool1 = bool;

4. 数据类型转换必须满足如下规则:

(1) 不能对boolean类型进行类型转换。

(2) 不能把对象类型转换成不相关类的对象。

(3) 在把范围大的类型转换为范围小的类型时必须使用强制类型转换。

(4) 转换过程中可能导致溢出或损失精度,例如:

int i =128;

byte b = (byte)i;

因为 byte 类型是 8 位,最大值为127,所以当 int 强制转换为 byte 类型时,值 128 时候就会导致溢出。

5. 浮点数到整数的转换是通过舍弃小数得到,而不是四舍五入,例如:

(int)33.5678 == 33;

(int)-30.89f == -30

5. 数据类型之间的转换

从低级到高级。

低  ------------------------------------------------->  高

byte,short,char—> int —> long—> float —> double

byte,short,char之间不转换,它们参与运算自动转成int

对于简单类型数据间的转换,有两种方式:自动类型转换和强制类型转换,通常发生在表达式中或方法的参数传递时。

自动类型转换:低精度向高精度转换(低级类型向高级类型转换)

(1)按照数据类型所能表示值的范围的大小(精度),从小到大排序依次为:(byte, short, char)-->int-->long-->float-->double;//低精度向高精度转换

byte b = 100;

short s = 200;

char c = 'B';

int i = b;

System.out.println("i = " + i);

int i1 = s;

System.out.println("i1 = " + i1);

int i2 = c;

System.out.println("i2 = " + i2);

long l = i;

System.out.println("l = " + l);

float f = l;

System.out.println("f = " + f);

double d = f;

System.out.println("d = " + d);

(2)在进行表达式运算时:精度小的数据类型,会向精度高的数据类型进行自动转换(也可以说成低级类型向高级类型转换);

(3)在进行方法调用时:实际参数的精度较小,而被调用的方法的形式参数的精度较大时(若有匹配的方法,则会直接调用匹配的方法),系统也会将精度小的数据类型自动转换为精度较大的数据类型,然后进行方法调用。private static double add(double d1, double d2) {

double sum = d1 + d2;

return sum;

}

private static double add1(double d1, float f1) {

double sum1 = d1 + f1;

return sum1;

}

public static void main(String[] args) {

byte b = 120;

int i = 180;

System.out.println("sum = " + add(b, i));   //300.0

System.out.println("sum1 = " + add1(b, i));  //300.0

}

(4)同一个类中,对于多个重载方法,会转换成最“接近”的精度数据进行调用。//定义多个重载方法(方法名相同,方法的参数类型,个数,返回值不同的方法)

private static double add(double d1, double d2) {

System.out.println("add被调用了");

double sum = d1 + d2;

return sum;

}

private static double add(double d1, float f1) {

System.out.println("add1被调用了");

double sum1 = d1 + f1;

return sum1;

}

private static float add(float f1, float f2) {

System.out.println("add2被调用了");

float sum2 = f1 + f2;

return sum2;

}

private static float add(int i1, float f2) {

System.out.println("add3被调用了");

float sum3 = i1 + f2;

return sum3;

}

public static void main(String[] args) {

byte b = 100;

short s = 300;

int i = 500;

float f = 3.14f;

double d = 314.15926d;

System.out.println("sum = " + add(b, s));

System.out.println("sum1 = " + add(b, i));

System.out.println("sum2 = " + add(s, i));

System.out.println("sum3 = " + add(i, f));

System.out.println("sum4 = " + add(b, d));

System.out.println("sum5 = " + add(f, d));

System.out.println("sum6 = " + add(d, f));

System.out.println("sum7 = " + add(b, b));

System.out.println("sum8 = " + add(i, i));

System.out.println("sum9 = " + add(f, f));

System.out.println("sum10 = " + add(d, d));

}

(5)如果低级类型为char型,它向高级类型(整型)转换时,会转换为对于的ASCII码值。

char c = 'a';

int i = c;

System.out.println("i = " + i);  // i = 97

(6)对于byte,short,char这三种类型,它们是平级的,因此它们不能相互自动转换。

强制类型转换:高精度向低精度转换(高级类型向低级类型转换),存在精度损失问题

将高精度的数据类型转换为低精度的数据类型时,可以使用强制类型转换,这种转换可能导致溢出或精度损失。

强制类型转换的数据类型必须是兼容的,强制转换不会报错,但是会损失精度。

格式:(type)value type是要强制类型转换后的数据类型double d = 314.15926762562627d;

//double强转为float,丢失部分小数

float f = (float)d;

System.out.println("f = " + f);

//double强转为long,只保留整数部分

long l = (long)d;

System.out.println("l = " + l);

//double强转为int,只保留整数部分

int i = (int)d;

System.out.println("i = " + i);

//double强转为short,只保留整数部分

short s = (short)d;

System.out.println("s = " + s);

//double强转为byte,存在溢出(314对应的二进制为:0001 0011 1010,因为byte占8位,取其后8位为58)

byte b = (byte)d;

System.out.println("b = " + b);

6. 表达式运算的数据类型自动提升规则:

(1)所有的byte,short,char型的值将被提升为int型;

(2)如果有一个操作数是long型,计算结果是long型;

(3)如果有一个操作数是float型,计算结果是float型;

(4)如果有一个操作数是double型,计算结果是double型;//获取变量的类型

public static String getType(Object o){

return o.getClass().toString();

}

public static void main(String[] args) {

byte b = 12;

short s = 350;

char c = 90;

int i = 100;

long l = 2000;

float f = 3.14f;

double d = 3.14159265758;

System.out.println("1 --- " + getType(b));

System.out.println("2 --- " + getType(s));

System.out.println("3 --- " + getType(c));

System.out.println("4 --- " + getType(i));

System.out.println("5 --- " + getType(l));

System.out.println("6 --- " + getType(f));

System.out.println("7 --- " + getType(d));

System.out.println("\n");

System.out.println("1 --- " + getType(b + s));

System.out.println("2 --- " + getType(b + i));

System.out.println("3 --- " + getType(s + i));

System.out.println("4 --- " + getType(c + i));

System.out.println("5 --- " + getType(c + s));

System.out.println("6 --- " + getType(c + b));

System.out.println("\n");

System.out.println("1 --- " + getType(i+ l));

System.out.println("2 --- " + getType(l + f));

System.out.println("3 --- " + getType(f + d));

System.out.println("4 --- " + getType(l + d));

System.out.println("5 --- " + getType(f + f));

System.out.println("6 --- " + getType(d + d));

}

未完,待续!请诸君多多支持,喜欢的话,点个赞吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值