Integer类
Integer类的作用:
1.integer类对基本类型int的进行包装,它包含一个int值
2.integer类提供了多个方法,能在int类型和String类型之间互相转换,还提供了处理int类型时非常有用的其他一些常量和方法
3.定义类成员属性经常使用integer替代int从而可以应用更多integer的功能
compare To方法
public int compare To(Integer target)
如果当前Integer对象所包含的int值大于target包含的值则返回1,如果小于返回-1,如果相等则返回0;
package com.domain;
public final class Employee {
private Integer id;//
public Employee(Integer id) {
this.id = id;
}
public Employee() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
package com.Test;
import com.domain.Employee;
public class Test1 {
public static void main(String[] args) {
Employee employee = new Employee();
Employee employee1 = new Employee();
Integer intObj = new Integer(24);
employee.setId(intObj);
System.out.println(employee.getId());//24
employee1.setId(new Integer("45"));
System.out.println(employee1.getId());//45
//integer常量属性
System.out.println(Integer.MAX_VALUE);//2147483647
System.out.println(Integer.MIN_VALUE);//-2147483648
System.out.println(Integer.BYTES);//4
System.out.println(Integer.SIZE);//32
//比较大小的方法
System.out.println(employee.getId().compareTo(employee1.getId()));//24<45返回-1
System.out.println(employee1.getId().compareTo(employee.getId()));//45>24返回1
System.out.println(new Integer(100).compareTo(new Integer(100)));//相等返回0
}
}
intValue方法
public int intValue()
以int类型返回该Integer的值,实现从对象类型转换为值类型,其他类似方法:
byteValue();shortValue;longValue();
floatValue();doubleValue();
parselnt方法
public static int parseInt(String s)
将给定的字符串s转化为十进制int值类型返回,s字符串除了第一个字符可以使用-表示有符号的负数以外,其他字符必须保证是0-9的数字,否则转换出现NumberFormatException异常
public static int parseInt(String s,int radix)
将给定的字符串s转换为以参数radix标识,10进制int值类型返回,radix的取值通常为2,8,10,16,27
任何不符合格式的s都将出现NumberFormatException异常
测试类:
package com.Test;
public class Test2 {
public static void main(String[] args) {
//parseInt静态转换方法,将字符串转化为值类型,所有包装类都具有此类似的方法
String pwd = "123450";
int pwdInt = Integer.parseInt(pwd);
System.out.println(pwdInt+9);//12345+9=123459而不是字符串拼接为1234509
//parseInt重载方法
System.out.println("二进制:"+Integer.parseInt("10111",2));
}
}
toBinaryString方法
public static String toBinaryString(int i)
将给定的int类型参数i转化为二进制字符串形式返回,返回的字符串形式省略前导0,类似的方法:
toOctalString 转换为8进制字符串
toHexString 转换为16进制字符串
工具类:
package com.tools;
public class IntegerParseManagement {
public static String fromIntegerToStringByArg(int target,int tag){
if (tag == 2)
return Integer.toBinaryString(target);
else if (tag == 8)
return Integer.toOctalString(target);
else if (tag == 16)
return Integer.toHexString(target);
else
return Integer.toBinaryString(target);
}
}
测试类:
package com.Test;
import com.tools.IntegerParseManagement;
public class Test3 {
public static void main(String[] args) {
int num = 2390;
System.out.println(IntegerParseManagement.fromIntegerToStringByArg(num,2));//100101010110
System.out.println(IntegerParseManagement.fromIntegerToStringByArg(num,8));//4526
System.out.println(IntegerParseManagement.fromIntegerToStringByArg(num,16));//956
}
}
valueOf方法
public static Integer valueOf(int i)
返回一个表示指定的int值的Integer实例,如果不需要新的Integer实例,则通常应优先使用该方法从而提供JVM效率及节省资源。类似方法:
public static Integer valueOf(String s)
public static Integer valueOf(String s,int radix)
package com.Test;
public class Test4 {
public static void main(String[] args) {
int num = 1234;
Integer intObj0 = Integer.valueOf(num);
System.out.println(intObj0.intValue());//1234
System.out.println(Integer.valueOf("345").intValue());//345
String str = "1011010";
Integer intObj1 = Integer.valueOf(str,2);
System.out.println("以二进制处理:"+intObj1);//以二进制处理:90
Integer intObj2 = Integer.valueOf(str,8);
System.out.println("以八进制处理:"+intObj2);//以八进制处理:266760
Integer intObj3 = Integer.valueOf(str,16);
System.out.println("以十六进制处理:"+intObj3);//十六进制处理:16846864
}
}