目 录
2.int类型的封装类是Integer!其中常用的构造方法有:
4.封装类的常用操作: 用于基本数据类型与字符串之间的转换.
String类型转换为int类型 (需要注意的是该字符串的字面上是有效的数字)
为了对基本数据类型进行更多方便的操作,java就针对每一种基本数据类型提供了对应的类类型,也就是基本数据类型相对应的封装类.
封装类就是把基本数据类型包装成引用数据类型,之后便可以调用一些方法.
1.基本数据类型的封装类
基本数据类型的封装类
基本数据类型 | byte | short | int | long | float | double | char | boolean |
对应的封装类 | Byte | Short | Integer | Long | Float | Double | Character | Boolean |
那么该如何使用呢?又该如何调用方法呢?来看看具体的实例!所有的包装类都是差不多的!以int的封装类为例看看吧!
2.int类型的封装类是Integer!其中常用的构造方法有:
- Integer( int value) 构造一个新分配的 Integer 对象,它表示指定的 int 值
- Integer(String s) 构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值
public class Test {
public static void main(String[] args) {
//Integer( int value)
int num = 200;
Integer integer = new Integer(num);
//Integer(String s)
String str = "100"; //这个字符串的字面上是一个有效的数字。
Integer integer2 = new Integer(str);
}
}
3.Integer封装类中常用的方法:
- Integer.toBinaryString(num); 将num转换为二进制输出
- Integer.toHexString(num); 将num转换为十六进制输出
- Integer.toOctalString(num); 将num转换为八进制输出
- Integer.MAX_VALUE; 静态常量,int类型的最大范围
- Integer.MIN_VALUE; 静态常量,int类型的最小范围
public class Test {
public static void main(String[] args) {
int num = 10;
String s = Integer.toBinaryString(num);
String s1 = Integer.toHexString(num);
String s2 = Integer.toOctalString(num);
System.out.println(s); //1010
System.out.println(s1); //a
System.out.println(s2); //12
//int 4个字节 整型范围
int maxValue = Integer.MAX_VALUE;
int minValue = Integer.MIN_VALUE;
System.out.println(minValue); //-2147483648
System.out.println(maxValue); //2147483647
}
}
4.封装类的常用操作: 用于基本数据类型与字符串之间的转换.
int类型转换为String类型
- 给int类型的数据拼接一个空字符串""
- String.valueOf(int类型数据); 该方法可以将任意类型的转换为String类型
- Integer.toString(int类型数据);
String类型转换为int类型 (需要注意的是该字符串的字面上是有效的数字)
- Integer integer=new Integer(字符串); int a=integer.intValue();
- Integer.parseInt(字符串); (注意:char类型的封装类中没有这个方法的哈!)
public class ClassFengZhuangTest {
public static void main(String[] args) {
//int------String
int a=200;
//拼接一个空字符串""
String s3=a+"";
System.out.println(s3); //200
//String.valueOf();
String s4=String.valueOf(a);
System.out.println(s4); //200
//toString();
String s5=Integer.toString(a);
System.out.println(s5); //200
//String------int
String s6="500";
//intValue()
Integer i=new Integer(s6);
int b=i.intValue();
//parseInt()
int c=Integer.parseInt(s6);
int d=b+c;
System.out.println(d); //1000
}
}
(小编也在努力学习更多哟!以后会多多分享哒!)
希望对友友们有所帮助!!!!