java中的Integer类

Integer

Integer对象的创建

public Integer(int value);

public Integer(String str);

可以看到上述都为构造方法

str参数必需是若干数字组成的字符串,当然可以包含负号

public static void main(String[] args) {
//        Test t = new Test();
        /*方法一:使用Integer里的构造器*/
        Integer intob = new Integer("-356");
        Integer integ = new Integer(53);

        /*方法二:Integer是int的兼容包装类,可以使用int常量
        或变量创建一个Integer实例*/
        int a = 5;
        Integer int3 = 3;
        Integer int4 = a;
       
       /*方法三:静态方式获取Integer实例引用,比起new能更高效*/
       Integer.valueOf(3);
 
        System.out.println(int4);

    }

Integer字段常量
public static final int MAX_VALUE
//int 类型能够表示的最大值
public static final int MIN_VALUE
//int类型能够表示的最小值
public static final int SIZE
//二级制补码形式表示int值得比特位数(bit)
public static final int BYTES
//int类型在JVM中占有空间的字节数

compareTo方法

public int compareTo(Integer target)

  • 如果当前Integer对象所包含的int值大于target包含的值则返回1,如果小于返回-1,相等返回0

intValue方法

public int intValue()

  • 以int类型返回该Integer的值。
  • 实现从对象类型转换为值类型。
  • 类似的方法:

byteValue();

shortValue();


parseInt方法

public static int parseInt(String s,int radix)

  • 将字符串 s 转换为10进制 int 值类型返回。
  • 如果radix = 10,s 中除了可以用“ - ”号外,其它字符必须是0~9范围内的数字,否则会出现NumberFormatException异常
  • radix用来表示 s 是几进制。

valueOf方法

public static Integer valueOf(int i)

  • 返回一个表示指定的 int值的 Integer实例。
  • 如果不需要创建新的 Integer 实例(也就是使用new),则通常应优先使用该方法从而提高JVM效率及节省资源。

类似方法:

public static Integer valueOf(String s)

public static Integer valueOf(String s , int radix)


toBinaryString方法

public static String toBinaryString(int i)

  • 将给定的 int类型参数 i 转换为二进制字符串形式返回。
  • 返回的字符串形式省略前导0。

toOctalString 转换为8进制字符串

toHexString 转换为18进制字符串


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值