Java Integer类相关方法调用示例

x33g5p2x  于2021-08-19 转载在 Java  
字(12.1k)|赞(0)|评价(0)|浏览(399)

Integer类将一个原始类型的值包装在一个对象中。一个Integer类型的对象包含一个类型为int的单一字段。

此外,该类还提供了几种方法,用于将int转换为String,将String转换为int,以及其他在处理int时有用的常量和方法。

整数类 构造函数

  • Integer(int value) - 构建一个新分配的Integer对象,代表指定的int值。
  • Integer(String s) - 构建一个新分配的Integer对象,代表String参数所指示的int值。
    例子。
int b = 55;
String str= "45";
// Construct two Integer objects
Integer x = new Integer(b);
Integer y = new Integer(str);

###整数类方法

图中显示了Integer类所提供的API/方法的列表。

在这篇文章中,我们将学习Integer类提供的一些重要方法。

toString()方法

  • String toString() - 返回一个代表此Integer值的String对象。
  • static String toString(int i) - 返回一个代表指定整数的字符串对象。
  • static String toString(int i, int radix) - 返回第一个参数在第二个参数指定的radix中的字符串表示。
    **例子:**例子演示了以上所有toString()方法。
private static void toStringMethods() {
 // toString()
 System.out.println("toString(b) = " + Integer.toString(b));

 // toHexString(),toOctalString(),toBinaryString()
 // converts into hexadecimal, octal and binary forms.
 System.out.println("toHexString(b) =" + Integer.toHexString(b));
 System.out.println("toOctalString(b) =" + Integer.toOctalString(b));
 System.out.println("toBinaryString(b) =" + Integer.toBinaryString(b));

}

输出:

toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111

valueOf()方法

  • static Integer valueOf(int i)- 返回一个代表指定int值的Integer实例。
  • 静态Integer valueOf(String s)- 返回一个持有指定的String值的Integer对象。
  • static Integer valueOf(String s, int radix)- 返回一个Integer对象,该对象持有从指定的String中提取的值,该值是用第二个参数给出的radix进行解析的。
    例子:
private static void valueOfMethods() {
 // valueOf(): return Integer object
 // an overloaded method takes radix as well.
 Integer z = Integer.valueOf(b);
 System.out.println("valueOf(b) = " + z);
 z = Integer.valueOf(bb);
 System.out.println("ValueOf(bb) = " + z);
 z = Integer.valueOf(bb, 6);
 System.out.println("ValueOf(bb,6) = " + z);

}

输出:

valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29

parseInt()方法

  • static int parseInt(String s) - 将字符串参数解析为有符号的十进制整数。
  • static int parseInt(String s, int radix)-将字符串参数解析为第二个参数指定的弧度的有符号整数。
    示例:
private static void parseIntMethods() {
 // parseInt(): return primitive int value
 // an overloaded method takes radix as well
 int zz = Integer.parseInt(bb);
 System.out.println("parseInt(bb) = " + zz);
 zz = Integer.parseInt(bb, 6);
 System.out.println("parseInt(bb,6) = " + zz);

}

输出:

parseInt(bb) = 45
parseInt(bb,6) = 29

rotateLeft()和rotateRight()方法

  • static int rotateLeft(int i, int distance) - 返回指定int值的二进制补码向左旋转指定位数后得到的值。
  • static int rotateRight(int i, int distance) - 返回将指定的int值的二进制补码向右旋转指定位数得到的值。
    举例说明:
private static void rotateMethods() {
 // rotateLeft and rotateRight can be used
 // to rotate bits by specified distance
 int valrot = 2;
 System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" + Integer.rotateLeft(valrot, 2));
 System.out.println("rotateRight(0000 0000 0000 0010,3) =" + Integer.rotateRight(valrot, 3));
}

输出:

rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =1073741824

decode()方法

  • static Integer decode(String nm) - 将一个字符串解码成一个整数。
    例子:
private static void decodeMethods() {

 // decode() : decodes the hex,octal and decimal
 // string to corresponding int values.
 String decimal = "45";
 String octal = "005";
 String hex = "0x0f";
 Integer dec = Integer.decode(decimal);
 System.out.println("decode(45) = " + dec);
 dec = Integer.decode(octal);
 System.out.println("decode(005) = " + dec);
 dec = Integer.decode(hex);
 System.out.println("decode(0x0f) = " + dec);

}

输出:

decode(45) = 45
decode(005) = 5
decode(0x0f) = 15

getInteger()方法

  • static Integer getInteger(String nm) - 确定指定名称的系统属性的整数值。
  • static Integer getInteger(String nm, int val) - 确定具有指定名称的系统属性的整数值。
    *静态Integer getInteger(String nm, Integer val)-返回指定名称的系统属性的整数值。
    例子:
private static void getIntegerMethods() {
 // getInteger(): can be used to retrieve
 // int value of system property
 int prop = Integer.getInteger("sun.arch.data.model");
 System.out.println("getInteger(sun.arch.data.model) = " + prop);
 System.out.println("getInteger(abcd) =" + Integer.getInteger("abcd"));

 // an overloaded getInteger() method
 // which return default value if property not found.
 System.out.println("getInteger(abcd,10) =" + Integer.getInteger("abcd", 10));

}

输出。

getInteger(sun.arch.data.model) = 64
getInteger(abcd) =null
getInteger(abcd,10) =10

int, byte, short, long, double, float value()方法

private static void valueMethods() {
 // Construct two Integer objects
 Integer x = new Integer(b);
 // xxxValue can be used to retrieve
 // xxx type value from int value.
 // xxx can be int,byte,short,long,double,float
 System.out.println("bytevalue(x) = " + x.byteValue());
 System.out.println("shortvalue(x) = " + x.shortValue());
 System.out.println("intvalue(x) = " + x.intValue());
 System.out.println("longvalue(x) = " + x.longValue());
 System.out.println("doublevalue(x) = " + x.doubleValue());
 System.out.println("floatvalue(x) = " + x.floatValue());
}

输出。

bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0

bitcount()

int value = 45;

// bitcount() : can be used to count set bits
// in twos complement form of the number
System.out.println("Integer.bitcount(value)=" + Integer.bitCount(value));

numberOfTrailingZeros() 和 numberOfLeadingZeros() 方法

// numberOfTrailingZeroes and numberOfLeaadingZeroes
// can be used to count prefix and postfix sequence of 0
System.out.println("Integer.numberOfTrailingZeros(value)=" + Integer.numberOfTrailingZeros(value));
System.out.println("Integer.numberOfLeadingZeros(value)=" + Integer.numberOfLeadingZeros(value));

highestOneBit()方法

// highestOneBit returns a value with one on highest
// set bit position
System.out.println("Integer.highestOneBit(value)=" + Integer.highestOneBit(value));

lowestOneBit()方法

// highestOneBit returns a value with one on lowest
// set bit position
System.out.println("Integer.lowestOneBit(value)=" + Integer.lowestOneBit(value));

reverse()和reverseytes()方法

// reverse() can be used to reverse order of bits
// reverseytes() can be used to reverse order of bytes
System.out.println("Integer.reverse(value)=" + Integer.reverse(value));
System.out.println("Integer.reverseBytes(value)=" + Integer.reverseBytes(value));

signum()方法

// signum() returns -1,0,1 for negative,0 and positive
// values
System.out.println("Integer.signum(value)=" + Integer.signum(value));

hashcode(), equals(), compare()和compareTo()方法

// hashcode() returns hashcode of the object
int hash = x.hashCode();
System.out.println("hashcode(x) = " + hash);

// equals returns boolean value representing equality
boolean eq = x.equals(y);
System.out.println("x.equals(y) = " + eq);

// compare() used for comparing two int values
int e = Integer.compare(x, y);
System.out.println("compare(x,y) = " + e);

// compareTo() used for comparing this value with some
// other value
int f = x.compareTo(y);
System.out.println("x.compareTo(y) = " + f);

完整的程序供参考

package com.javaguides.corejava.wrapperclasses;

//*/*
/* 
/* @author javaguides.net
/*
/*/
public class IntegerClassExample {

 static int b = 55;
 static String bb = "45";

 private static void toStringMethods() {
  // toString()
  System.out.println("toString(b) = " + Integer.toString(b));

  // toHexString(),toOctalString(),toBinaryString()
  // converts into hexadecimal, octal and binary forms.
  System.out.println("toHexString(b) =" + Integer.toHexString(b));
  System.out.println("toOctalString(b) =" + Integer.toOctalString(b));
  System.out.println("toBinaryString(b) =" + Integer.toBinaryString(b));

 }

 private static void valueOfMethods() {
  // valueOf(): return Integer object
  // an overloaded method takes radix as well.
  Integer z = Integer.valueOf(b);
  System.out.println("valueOf(b) = " + z);
  z = Integer.valueOf(bb);
  System.out.println("ValueOf(bb) = " + z);
  z = Integer.valueOf(bb, 6);
  System.out.println("ValueOf(bb,6) = " + z);

 }

 private static void parseIntMethods() {
  // parseInt(): return primitive int value
  // an overloaded method takes radix as well
  int zz = Integer.parseInt(bb);
  System.out.println("parseInt(bb) = " + zz);
  zz = Integer.parseInt(bb, 6);
  System.out.println("parseInt(bb,6) = " + zz);

 }

 private static void getIntegerMethods() {
  // getInteger(): can be used to retrieve
  // int value of system property
  int prop = Integer.getInteger("sun.arch.data.model");
  System.out.println("getInteger(sun.arch.data.model) = " + prop);
  System.out.println("getInteger(abcd) =" + Integer.getInteger("abcd"));

  // an overloaded getInteger() method
  // which return default value if property not found.
  System.out.println("getInteger(abcd,10) =" + Integer.getInteger("abcd", 10));

 }

 private static void decodeMethods() {

  // decode() : decodes the hex,octal and decimal
  // string to corresponding int values.
  String decimal = "45";
  String octal = "005";
  String hex = "0x0f";
  Integer dec = Integer.decode(decimal);
  System.out.println("decode(45) = " + dec);
  dec = Integer.decode(octal);
  System.out.println("decode(005) = " + dec);
  dec = Integer.decode(hex);
  System.out.println("decode(0x0f) = " + dec);

 }

 private static void rotateMethods() {
  // rotateLeft and rotateRight can be used
  // to rotate bits by specified distance
  int valrot = 2;
  System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" + Integer.rotateLeft(valrot, 2));
  System.out.println("rotateRight(0000 0000 0000 0010,3) =" + Integer.rotateRight(valrot, 3));
 }

 private static void valueMethods() {
  // Construct two Integer objects
  Integer x = new Integer(b);
  // xxxValue can be used to retrieve
  // xxx type value from int value.
  // xxx can be int,byte,short,long,double,float
  System.out.println("bytevalue(x) = " + x.byteValue());
  System.out.println("shortvalue(x) = " + x.shortValue());
  System.out.println("intvalue(x) = " + x.intValue());
  System.out.println("longvalue(x) = " + x.longValue());
  System.out.println("doublevalue(x) = " + x.doubleValue());
  System.out.println("floatvalue(x) = " + x.floatValue());
 }

 public static void main(String[] args) {

  // Construct two Integer objects
  Integer x = new Integer(b);
  Integer y = new Integer(bb);

  toStringMethods();
  parseIntMethods();
  rotateMethods();
  toStringMethods();
  valueOfMethods();
  decodeMethods();
  getIntegerMethods();
  valueMethods();
  
  
  int value = 45;

  // bitcount() : can be used to count set bits
  // in twos complement form of the number
  System.out.println("Integer.bitcount(value)=" + Integer.bitCount(value));

  // numberOfTrailingZeroes and numberOfLeaadingZeroes
  // can be used to count prefix and postfix sequence of 0
  System.out.println("Integer.numberOfTrailingZeros(value)=" + Integer.numberOfTrailingZeros(value));
  System.out.println("Integer.numberOfLeadingZeros(value)=" + Integer.numberOfLeadingZeros(value));

  // highestOneBit returns a value with one on highest
  // set bit position
  System.out.println("Integer.highestOneBit(value)=" + Integer.highestOneBit(value));

  // highestOneBit returns a value with one on lowest
  // set bit position
  System.out.println("Integer.lowestOneBit(value)=" + Integer.lowestOneBit(value));

  // reverse() can be used to reverse order of bits
  // reverseytes() can be used to reverse order of bytes
  System.out.println("Integer.reverse(value)=" + Integer.reverse(value));
  System.out.println("Integer.reverseBytes(value)=" + Integer.reverseBytes(value));

  // signum() returns -1,0,1 for negative,0 and positive
  // values
  System.out.println("Integer.signum(value)=" + Integer.signum(value));

  // hashcode() returns hashcode of the object
  int hash = x.hashCode();
  System.out.println("hashcode(x) = " + hash);

  // equals returns boolean value representing equality
  boolean eq = x.equals(y);
  System.out.println("x.equals(y) = " + eq);

  // compare() used for comparing two int values
  int e = Integer.compare(x, y);
  System.out.println("compare(x,y) = " + e);

  // compareTo() used for comparing this value with some
  // other value
  int f = x.compareTo(y);
  System.out.println("x.compareTo(y) = " + f);
 }
}

输出。

toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
parseInt(bb) = 45
parseInt(bb,6) = 29
rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =1073741824
toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
getInteger(sun.arch.data.model) = 64
getInteger(abcd) =null
getInteger(abcd,10) =10
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0
Integer.bitcount(value)=4
Integer.numberOfTrailingZeros(value)=0
Integer.numberOfLeadingZeros(value)=26
Integer.highestOneBit(value)=32
Integer.lowestOneBit(value)=1
Integer.reverse(value)=-1275068416
Integer.reverseBytes(value)=754974720
Integer.signum(value)=1
hashcode(x) = 55
x.equals(y) = false
compare(x,y) = 1
x.compareTo(y) = 1

###参考资料

https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html

相关文章