📄 example04_convert.java
字号:
package example;
//该程序实现有关类型之间的转换
public class Example04_Convert
{
public static void main(String[] args)
{
//byte[] to String
{
byte[] bytes={'H','e','l','l','o','!'};
String string=new String(bytes);
System.out.println(string);
}
//String to byte[]
{
String string="Hello!";
byte[] bytes=string.getBytes();
for (int i=0;i<bytes.length;i++)
{
System.out.print((char)bytes[i]);
}
System.out.println();
}
//char[] to String
{
char[] chars={'H','o','w',' ','a','r','e',' ','y','o','u','?'};
String string=new String(chars);
System.out.println(string);
}
//String to char[]
{
//method 1
String string="How are you?";
char[] chars=string.toCharArray();
System.out.println(chars);
//method 2
chars=new char[3];
string.getChars(0,3,chars,0);
System.out.println(chars);
}
//String to StringBuffer
{
String string=new String("World");
StringBuffer strbuff=new StringBuffer(string);
System.out.println(strbuff);
}
//StringBuffer to String
{
StringBuffer strbuff=new StringBuffer("World");
//method 1
String string=strbuff.toString();
System.out.println(string);
//method 2
string=new String(strbuff);
System.out.println(string);
}
//基本数据类型, String and 包装类
{
//boolean
boolean boolb=true;
Boolean boolp=new Boolean(boolb);
boolp=new Boolean("true");
boolp=Boolean.valueOf(boolb);
boolp=Boolean.valueOf("true");
boolb=boolp.booleanValue();
System.out.println(boolp.toString());
System.out.println(Boolean.toString(boolb));
//byte
byte byteb=10;
Byte bytep=new Byte(byteb);
bytep=new Byte("10");
bytep=Byte.valueOf("10");
byteb=bytep.byteValue();//shortValue();intValue();longValue();floatValue();doubleValue();
byteb=Byte.parseByte("10");
System.out.println(bytep.toString());
System.out.println(Byte.toString(byteb));
//char
char charb='字';
Character charp=new Character(charb);
charb=charp.charValue();
// int intn=Character.getNumericValue('字');
System.out.println(charp.toString());
System.out.println(Character.toString(charb));
//short
short shortb=100;
Short shortp=new Short(shortb);
shortp=new Short("100");
shortp=Short.valueOf("100");
shortb=shortp.shortValue();//byteValue();intValue();longValue();floatValue();doubleValue();
shortb=Short.parseShort("100");
System.out.println(shortp.toString());
System.out.println(Short.toString(shortb));
//int
int intb=10000;
Integer intp=new Integer(intb);
intp=new Integer("10000");
intp=Integer.valueOf("10000");
intb=intp.intValue();//byteValue();shortValue();longValue();floatValue();doubleValue();
intb=Integer.parseInt("10000");
System.out.println(intp.toString());
System.out.println(Integer.toString(intb));//toBinaryString(int);toOctalString(int);toHexString(int);
System.out.println(Integer.toString(intb,16));
//long
long longb=1000000;
Long longp=new Long(longb);
longp=new Long("1000000");
longp=Long.valueOf("1000000");
longb=longp.longValue();//byteValue();shortValue();intValue();floatValue();doubleValue();
longb=Long.parseLong("1000000");
System.out.println(longp.toString());
System.out.println(Long.toString(longb));//toBinaryString(long);toOctalString(long);toHexString(long);
System.out.println(Long.toString(longb,16));
//float
float floatb=10.05f;
Float floatp=new Float(floatb);
floatp=new Float("10.05f");
floatp=Float.valueOf("10.05f");
floatb=floatp.floatValue();//byteValue();shortValue();intValue();longValue();doubleValue();
floatb=Float.parseFloat("10.05f");
System.out.println(floatp.toString());
System.out.println(Float.toString(floatb));
//double
double doubleb=1234.56789;
Double doublep=new Double(doubleb);
doublep=new Double("1234.56789");
doublep=Double.valueOf("1234.56789");
doubleb=doublep.doubleValue();//byteValue();shortValue();intValue();longValue();floatValue();
doubleb=Double.parseDouble("10.05f");
System.out.println(doublep.toString());
System.out.println(Double.toString(doubleb));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -