Integer类将一个原始类型的值包装在一个对象中。一个Integer类型的对象包含一个类型为int的单一字段。
此外,该类还提供了几种方法,用于将int转换为String,将String转换为int,以及其他在处理int时有用的常量和方法。
int b = 55;
String str= "45";
// Construct two Integer objects
Integer x = new Integer(b);
Integer y = new Integer(str);
###整数类方法
图中显示了Integer类所提供的API/方法的列表。
在这篇文章中,我们将学习Integer类提供的一些重要方法。
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
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
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
。
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
。
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
。
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
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
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);
完整的程序供参考
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
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/08/integer-wrapper-class-in-java.html
内容来源于网络,如有侵权,请联系作者删除!