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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LittleMangoYX/article/details/83342731

存储范围大小:

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);

猜你喜欢

转载自blog.csdn.net/LittleMangoYX/article/details/83342731
今日推荐