码迷,mamicode.com
首页 > 编程语言 > 详细

Java中的包装类

时间:2020-02-16 20:34:14      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:ati   intvalue   基本数据   基本数据类型   new   字符   value   intval   ring   

为了针对基本类型数据进行更多操作,更方便的操作,Java就针对每一种基本数据类型提供了对应
的类类型。包装类类型。
(基本类型) (引用类型)
byte Byte
short Short
int Interger
long Long
float Float
double Double
char Character
boolean Boolean

/**Interger的构造方法*/
public Integer(int value)
public Interger(String s)
注意:这个字符串必须是有数字字符组成
public static void main(String[] args) {
//方式1
int i = 100;
Integer ii = new Integer(i);
System.out.println("ii:" + ii);

//方式2
String s = "100";
//String s = "abc"; 会报错 NumberFormatException
Integer iii = new Integer(s);
System.out.println("iii:" + iii);
}
/**String和int类型的相互转换*/
public static void main(String[] args){
/**int -- String*/
int number = 100;
//方式1
String s1 = "" + number;
System.out.println("s1:" + s1);
//方式2
String s2 = String.valueOf(number);
System.out.println("s2:" + s2);
//方法3
//int -- Integer -- String
Integer i = new Integer(number);
String s3 = i.toString();
System.out.println("s3:" + s3);
//方式4
//public static String toString(int i)
String s4 = Integer.toString(number);
System.out.println("s4:" + s4);

/**String -- int*/
String s = "100";
//方式1
//String -- Integer -- int
Integer ii = new Integer(s);
//public int intValue()
int x = ii.intValue();
Systme.out.println("x:" + x);
//方式2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println("y" + y);

/**String -- float*/
//public static float parseFloat(String s)
String s = "100";
float f1 = Float.parseFloat(s);
System.out.println(f1);//100.0
}

Java中的包装类

标签:ati   intvalue   基本数据   基本数据类型   new   字符   value   intval   ring   

原文地址:https://www.cnblogs.com/lszbk/p/12318406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!