4.各种数据类型之间的转换
java中存在八种基本数据类型
- byte:8位,最大存储数据量是255,存放的数据范围是-128~127之间。
- short:16位,最大数据存储量是65536,数据范围是-32768~32767之间。
- int:32位,最大数据存储容量是2的32次方减1,数据范围是负的2的31次方到正的2的31次方减1。
- long:64位,最大数据存储容量是2的64次方减1,数据范围为负的2的63次方到正的2的63次方减1。
- float:32位,数据范围在3.4e-45~1.4e38,直接赋值时必须在数字后加上f或F。
- double:64位,数据范围在4.9e-324~1.8e308,赋值时可以加d或D也可以不加。
由于小数常量的默认类型是double型,所以float类型的后面一定要加f(F)。 - boolean:只有true和false两个取值。
- char:16位,存储Unicode码,用单引号赋值。
注意:
String类并不是基本数据类,而是一个类(class),是C++、java等编程语言中的字符串。
抽象类Number是BigDecimal、BigInteger、Byte、Double、Float、Integer、Long、Short类的超类
JDk1.6-API
例:Byte 类将基本类型 byte 的值包装在一个对象中。一个 Byte 类型的对象只包含一个类型为 byte 的字段。 此外,该类还为 byte 和 String 的相互转换提供了几种方法,并提供了处理 byte 时非常有用的其他一些常量和方法。当然其他类型也类似。
Boolean类将基本类型为boolean的值包装在一个对象中。
以byte转其他类型为例(包装类过渡类型转化,其他几种类似):
public class gao {
static byte byt=0x01;
static Byte Byt=new Byte(byt);//Byte、Double、Float、Integer、Long、Short
static byte b=Byt.byteValue();
static short s=Byt.shortValue();
static int i=Byt.intValue();
static long l=Byt.longValue();
static float f=Byt.floatValue();
static double d=Byt.doubleValue();
public static void main(String args[]) {
System.out.println(byt);
System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
}
}
结果:
1
1
1
1
1
1.0
1.0
char与其他类型之间的转化:
public class gao {
public static void main(String args[]) {
int a=97;
char c=(char)a; //整形->char加(char)
int b=c; //char->整形可以加(int)也可不加
System.out.println(c);
System.out.println(b);
}
}
结果:
a
97
整形、整形数组、字符串、字符串数组、字符数组之间的转化:
- 整形-字符串
- 字符串-整形
- 字符串-字符串数组
- 字符串数组-字符串
- 字符串-字符数组
- 字符数组-字符串
- 整形数组-字符串
- 字符串-整形数组
整形数组转化为字符数组(整形数组先转为字符串、字符串转为字符数组)
字符数组转化为整形数组(字符数组转为字符串、字符串转化为整形数组)
1.其他基本类型向字符串转化:
- 将基本数据类型与空字符串(" ")连接(+)即可获得其所对应的字符串
- 调用String类中的valueOf()方法返回相应的字符串
- 使用包装类的toString()方法
public class gao {
public static void main(String args[]) {
int i=123;
String str1=i+"";
String str2=String.valueOf(i);
String str3=Integer.toString(i);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
结果:
123
123
123
2.字符串向其他基本类型转化:
- 调用基本数据类型对应的包装类中的方法ParseXXX(String)
- 对应包装类型的valueOf(String)
public class gao {
public static void main(String args[]) {
String str="123";
int a=Integer.parseInt(str);
int b=Integer.valueOf(str);
System.out.println(a);
System.out.println(b);
}
}
结果:
123
123
如果需要将一个字符串转换成为一个数值类型数据,那么需要注意的是,字符串中得有符合数值类型的数字,否则将无法转换。
如:
public class gao {
public static void main(String args[]) {
String str="123abc";
int a=Integer.parseInt(str);
System.out.println(a);
}
}//则会报错
5.字符串转字符数组
3.字符串转字符串数组
public class gao {
public static void main(String args[]) {
String str = "abcbcddef";
char[] ch=str.toCharArray();
for(int i=0;i<ch.length;i++) {
System.out.println(ch[i]);
}
String[] arr=str.split("");
for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
}
}
//当然 也可以用循环依次放入,
//取得字符串的某个元素用 String.charAt(i); 返回char
char[] cha= new char[str.length()];
for(int i =0;i<str.length();i++) {
cha[i] = str.charAt(i);
}
4.字符串数组转字符串
public class gao {
public static void main(String args[]) {
String[] str={"abc","bcd","def"};
StringBuffer SB=new StringBuffer();
for(int i=0;i<str.length;i++){
SB.append(str[i]);
}
String s=SB.toString();
System.out.println(s);
}
}//输出abcbcddef
6.字符数组转字符串
(1).
public class gao {
public static void main(String args[]) {
char[] data={'a','b','c'};
String s=new String(data);
}
}//输出abc
(2).
public class gao {
public static void main(String args[]) {
char[] data={'a','b','c'};
String string =String.copyValueOf(data);
}
}//输出abc
7.整形数组转化为字符串
public class gao {
public static void main(String args[]) {
int[] a= {1,2,3,4};
StringBuffer sb=new StringBuffer();
for(int i=0;i<a.length;i++) {
sb.append(a[i]);
}
System.out.println(sb);
}
}//输出1234
8.字符串转化为整形数组(先将字符串转为字符串数组)
public class gao {
public static void main(String args[]) {
String str="100200300";
String[] temp=str.split("");
int[] result=new int[temp.length];
for(int i=0;i<temp.length;i++) {
result[i]=Integer.parseInt(temp[i]);
}
for(int i=0;i<result.length;i++) {
System.out.print(result[i]);
}
}
}//输出100200300