Java中基本数据类型与包装类,字符串转换

存储范围大小:

byte-short-char-int-long-float-double

低级到高级自动类型转换:
 

int i = 5;
float f = i;
double d = i;


高级到低级强制类型转换:

int a = 20;
byte b = (byte) a;

double d = 2.99;
int in = (int) d;


基本类型转包装类:
 

int n=10;
Integer n1=new Integer(n);


包装类过渡类型转换:
 

//double转换为int
double d2=200.03;
Double d3=new Double(d2);
int n=d3.intValue();


字符串转基本数据类型
 

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

String str="25";

byte b=Byte.parseByte(str);

short s=Short.parseShort(str);

int i=Integer.parseInt(str);

long log=Long.parseLong(str);

float f=Float.parseFloat(str);

double d=Float.parseFloat(str);

 

String->char

//String转换为char时,需要char类型的数组接收
String str="hello";
char[] c=str.toCharArray();
for (int i = 0; i < c.length; i++) {
    System.out.println(c[i]+"\t");
}

基本数据类型转换为字符串

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

String num1 = Integer.toString(int n);
String num2 = Long.toString(long n);
String num3 = Short.toString(Short n);
String num4 = Float.toString(Float n);
String num5 = Double.toString(Double n);

String str=String.valueOf(int n);

char->String

char c='a';
String str1=String.valueOf(c);
        
char[] c1={'a','b','c'};
String str2=String.valueOf(c1, 0, 3);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值