JAVAJava中的整型类

整数类是一个 包装类 对于原语类型int,它包含几种有效处理int值的方法,比如将其转换为字符串表示,反之亦然。Integer类的对象可以保存单个int值。

null

施工人员:

  • 整数(整数b): 创建用提供的值初始化的整数对象。

语法:

public Integer(int b)

参数:

b : value with which to initialize
  • 整数(字符串s): 创建一个整数对象,用字符串表示法提供的int值初始化。默认基数为10。

语法:

public Integer(String s) throws NumberFormatException

参数:

s : string representation of the int value 

抛出:

NumberFormatException : If the string provided does not represent any int value.

方法:

1.toString() : 返回与int值对应的字符串。

语法:

public String toString(int b)

参数:

b : int value for which string representation required.

2.tohextstring() : 以十六进制形式返回与int值对应的字符串,也就是说,它返回一个以十六进制字符表示int值的字符串-[0-9][a-f]

语法:

public String toHexString(int b)

参数:

b : int value for which hex string representation required.

3. toOctalString() : 以八进制形式返回与int值对应的字符串,也就是说,它返回一个以八进制字符表示int值的字符串-[0-7]

语法:

public String toOctalString(int b)

参数:

b : int value for which octal string representation required.

4. Tobinarysting() : 以二进制数字返回与int值对应的字符串,也就是说,它返回一个以十六进制字符表示int值的字符串-[0/1]

语法:

public String toBinaryString(int b)

参数:

b : int value for which binary string representation required.

5.价值 : 返回用提供的值初始化的整数对象。

语法:

public static Integer valueOf(int b)

参数:

b : a int value

语法:

public static Integer valueOf(String val, int radix)throws NumberFormatException

参数:

val : String to be parsed into int valueradix : radix to be used while parsing

抛出:

NumberFormatException : if String cannot be parsed to a int value in given radix.

语法:

public static Integer valueOf(String s)throws NumberFormatException

参数:

s : a String object to be parsed as int

抛出:

NumberFormatException : if String cannot be parsed to a int value in given radix.

6.parseInt() : 通过分析以基数提供的字符串返回int值。与valueOf()不同,因为它返回一个基本的int值,valueOf()返回整数对象。

语法:

public static int parseInt(String val, int radix)throws NumberFormatException

参数:

val : String representation of int radix : radix to be used while parsing

抛出:

NumberFormatException : if String cannot be parsed to a int value in given radix.
  • 另一个重载方法只包含字符串作为参数,基数默认设置为10。

语法:

public static int parseInt(String val)throws NumberFormatException

参数:

val : String representation of int 

抛出:

NumberFormatException : if String cannot be parsed to a int value in given radix.

7.getInteger(): 返回表示与给定系统属性关联的值的整数对象,如果不存在,则返回null。

语法:

public static Integer getInteger(String prop)

参数:

prop : System property
  • 另一个重载方法,如果属性不存在,则返回第二个参数,即它不返回null,而是返回用户提供的默认值。

语法:

public static Integer getInteger(String prop, int val)

参数:

prop : System propertyval : value to return if property does not exist.
  • 另一个重载方法根据返回的值解析值,即如果返回的值以“#”开头,则解析为十六进制;如果以“0”开头,则解析为八进制,否则为十进制。

语法:

public static Integer getInteger(String prop, Integer val)

参数:

prop : System propertyval : value to return if property does not exist.

8.解码 : 返回一个整数对象,该对象包含所提供字符串的解码值。提供的字符串必须为以下形式,否则将抛出NumberFormatException- 十进制(符号)十进制数 十六进制-(符号)“0x”十六进制数字 十六进制-(符号)“0X”十六进制数字 八进制-(符号)“0”八进制数字

语法:

public static Integer decode(String s)throws NumberFormatException

参数:

s : encoded string to be parsed into int val

抛出:

NumberFormatException : If the string cannot be decoded into a int value

9.rotateLeft() : 以给定值的补码形式将位向左旋转给定距离,返回一个基本整数。向左旋转时,最高有效位移到右侧,或最低有效位位置,即位发生循环移动。负距离表示向右旋转。

语法:

public static int rotateLeft(int val, int dist)

参数:

val : int value to be rotateddist : distance to rotate

10.rotateRight() : 通过以给定值的二补形式将位向右旋转给定距离,返回一个基本整数。当向右旋转时,最低有效位移到左侧,或最高有效位发生循环移动。负距离表示向左旋转。

语法:

public static int rotateRight(int val, int dist)

参数:

val : int value to be rotateddist : distance to rotate

JAVA

// Java program to illustrate
// various Integer methods
public class Integer_test {
public static void main(String args[])
{
int b = 55 ;
String bb = "45" ;
// Construct two Integer objects
Integer x = new Integer(b);
Integer y = new Integer(bb);
// 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));
// 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);
// 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);
// 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 ));
// 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);
// 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 ));
}
}


输出:

toString(b) = 55toHexString(b) =37toOctalString(b) =67toBinaryString(b) =110111valueOf(b) = 55ValueOf(bb) = 45ValueOf(bb,6) = 29parseInt(bb) = 45parseInt(bb,6) = 29getInteger(sun.arch.data.model) = 64getInteger(abcd) =nullgetInteger(abcd,10) =10decode(45) = 45decode(005) = 5decode(0x0f) = 15rotateLeft(0000 0000 0000 0010 , 2) =8rotateRight(0000 0000 0000 0010,3) =1073741824

11.字节值() : 返回与此整数对象对应的字节值。

语法:

public byte byteValue()

12.shortValue() : 返回与此整数对象对应的短值。

语法:

public short shortValue()

13.intValue() : 返回与此整数对象对应的int值。

语法:

public int intValue()

13.longValue(): 返回与此整数对象对应的长值。

语法:

public long longValue()

14.双重价值() : 返回与此整数对象对应的双精度值。

语法:

public double doubleValue()

15.浮动值() : 返回与此整数对象对应的浮点值。

语法:

public float floatValue()

16.hashCode() : 返回与此整数对象对应的哈希代码。

语法:

public int hashCode()

17.比特数() : 返回给定整数的两个补码中的设置位数。

语法:

public static int bitCount(int i)

参数:

i : int value whose set bits to count

18.numberOfLeadingZeroes() : 返回值的两补形式中最高1位之前的0位数,即如果两补形式中的数字为0000 1010 0000 0000,则此函数将返回4。

语法:

public static int numberofLeadingZeroes(int i)

参数:

i : int value whose leading zeroes to count in twos complement form

19.numberOfTrailingZeroes() : 返回值的两补形式的最后1位之后的0位数,即如果两补形式的数字为0000 1010 0000 0000,则此函数将返回9。

语法:

public static int numberofTrailingZeroes(int i)

参数:

i : int value whose trailing zeroes to count in twos complement form

20.highestOneBit() : 返回一个最多只有一位的值,该值位于给定值中最高一位的位置。如果给定的值为0,即数字为0000 0000 1111,则返回0,然后此函数返回0000 0000 1000(给定数字中最高一位的一个)

语法:

public static int highestOneBit(int i)

参数:

i : int value 

21.Lowstonebit() : 返回一个最多只有一位的值,该值位于给定值的最低一位位置。如果给定值为0,即数字为0000 1111,则返回0,然后此函数返回0000 0001(给定数字中最高一位的一个)

语法:

public static int LowestOneBit(int i)

参数:

i : int value 

22.等于: 用于比较两个整数对象的相等性。如果两个对象包含相同的int值,则此方法返回true。仅当检查是否相等时才应使用。在所有其他情况下,应首选比较法。

语法:

public boolean equals(Object obj)

参数:

obj : object to compare with

23.比较 : 用于比较两个整数对象的数值相等性。在比较两个整数值是否相等时,应使用此选项,因为它会区分较小值和较大值。返回小于0,0的值,对于小于、等于和大于,返回大于0的值。

语法:

public int compareTo(Integer b)

参数:

b : Integer object to compare with

24.比较 : 用于比较两个基本int值的数值相等性。由于它是一个静态方法,因此可以在不创建任何整数对象的情况下使用它。

语法:

public static int compare(int x,int y)

参数:

x : int valuey : another int value

25.符号() : 对于负值返回-1,对于0返回0,对于大于0的值返回+1。

语法:

public static int signum(int val)

参数:

val : int value for which signum is required.

26.反向 : 返回一个原语int值,该原语int值与给定int值的补位形式的位顺序相反。

语法:

public static int reverseBytes(int val)

参数:

val : int value whose bits to reverse in order.

27.反向字节() : 返回一个原语int值,该原语int值与给定int值的补码形式的字节顺序相反。

语法:

public static int reverseBytes(int val)

参数:

val : int value whose bits to reverse in order.

28.静态整数比较(整数x,整数y) :此方法以数字方式比较两个int值,并将其视为无符号。

语法:

public static int compareUnsigned(int x, int y)

29.静态整数除数无符号(整数除数,整数除数) :此方法返回第一个参数除以第二个参数的无符号商,其中每个参数和结果都被解释为无符号值。

语法:

public static int divideUnsigned(int dividend, int divisor)

30.静态整数最大值(整数a、整数b) :此方法通过调用Math返回两个int值中的较大值。最大值。

语法:

public static int max(int a, int b)

31.静态最小整数(整数a、整数b) :此方法通过调用Math返回两个int值中较小的一个。敏。

语法:

public static int min(int a, int b)

32.静态int-parseUnsignedInt(字符序列s、int-beginIndex、int-endIndex、int-radix) :此方法将CharSequence参数解析为指定基数中的无符号整数,从指定的beginIndex开始,扩展到endIndex–1。

语法:

public static int parseUnsignedInt(CharSequence s,                                   int beginIndex,                                   int endIndex,                                   int radix)                            throws NumberFormatException

33.静态int-parseUnsignedInt(字符串s) :此方法将字符串参数解析为无符号十进制整数。

语法:

public static int parseUnsignedInt(String s)throws NumberFormatException

34.静态int parseUnsignedInt(字符串s,int基数) :此方法将字符串参数解析为第二个参数指定的基数中的无符号整数。

语法:

public static int parseUnsignedInt(String s,                                   int radix)                            throws NumberFormatException

35.静态整型余数无符号(整型被除数,整型除数) :此方法返回第一个参数除以第二个参数的无符号余数,其中每个参数和结果都被解释为无符号值。

语法:

public static int remainderUnsigned(int dividend, int divisor)

36.静态整数和(整数a,整数b) :此方法根据+运算符将两个整数相加。

语法:

public static int sum(int a, int b)

37.静态长toUnsignedLong(int x) :此方法通过无符号转换将参数转换为long。

语法:

public static long toUnsignedLong(int x)    

38.静态字符串到签名字符串(int i) :此方法将参数的字符串表示形式返回为无符号十进制值。

语法:

public static String toUnsignedString(int i, int radix) 

JAVA

// Java program to illustrate
// various Integer class methods
public class Integer_test {
public static void main(String args[])
{
int b = 55 ;
String bb = "45" ;
// Construct two Integer objects
Integer x = new Integer(b);
Integer y = new Integer(bb);
// 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());
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 numberOfLeadingZeroes
// 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);
}
}


输出:

bytevalue(x) = 55shortvalue(x) = 55intvalue(x) = 55longvalue(x) = 55doublevalue(x) = 55.0floatvalue(x) = 55.0Integer.bitcount(value)=4Integer.numberOfTrailingZeros(value)=0Integer.numberOfLeadingZeros(value)=26Integer.highestOneBit(value)=32Integer.lowestOneBit(value)=1Integer.reverse(value)=-1275068416Integer.reverseBytes(value)=754974720Integer.signum(value)=1hashcode(x) = 55x.equals(y) = falsecompare(x,y) = 1x.compareTo(y) = 1

Java中整数包装类的初始化:

类型1:直接初始化:

Integer类的常量对象将在堆内存中的常量空间内创建。常量空间:为了更好地理解,我们可以想象堆内存中有一些常量空间。

例子:

Integer x = 200;  //initializing directlyx = 300;      //modifying xx = 10;           //modifying x again

整数x=200

  • 编译器将上述语句转换为: 整数x=整数。价值(200)。 这被称为 “自动装箱” .原语整数值200被转换成一个对象。

(要了解自动装箱和拆箱,请点击此处: https://www.geeksforgeeks.org/autoboxing-unboxing-java/ )

  • x指向常数空间中存在的200。参见图1。
Examp img 1

图1

x=300

  • 自动装箱再次完成,因为x是一个直接初始化的整数类对象。
  • 注: 无法修改直接初始化的对象(x),因为它是一个常量。当我们试图通过指向新常量(300)来修改对象时,旧常量(200)将出现在堆内存中,但对象将指向新常量。
  • x指向常数空间中的300。参见图2。
图片[2]-JAVAJava中的整型类-yiteyi-C++库

图2

x=10

  • 注: 默认情况下,值为-128到127,整数。valueOf()方法不会创建Integer的新实例。它从缓存中返回一个值。
  • 缓存中存在的x点10。
图片[3]-JAVAJava中的整型类-yiteyi-C++库

图3

如果我们下次指定x=200或x=300,它将指向已经存在于常数空间中的值200或300。如果我们给x赋值,而不是这两个值,那么它会创建一个新常数。

(为了更好地理解,请查看整数包装器类比较主题)

类型2:动态初始化:

非常量的整数类对象将在常量空间之外创建。它还会在常量空间中创建一个整数常量。变量将指向整数对象&而不是整数常量。

例子:

Integer a = new Integer(250);   //Initializing dynamicallya = 350;            //Type 1 initialization

整数a=新整数(250)

  • 250是在常量空间内外创建的。变量“a”将指向常量空间之外的值。参见图4。
图片[4]-JAVAJava中的整型类-yiteyi-C++库

图4

a=350;

  • 自动装箱后,“a”将指向350。参见图5。
图片[5]-JAVAJava中的整型类-yiteyi-C++库

图5

如果我们下次指定a=250,它将不会指向已经存在的具有相同值的对象,它将创建一个新对象。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。 参考资料: 官方Java文档

本文由 Rishabh Mahrsee .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享