Java.lang.Integer 类及其方法
Java .lang.Integer 将整数数据类型包装到一个对象,该对象包含一个数据类型为 int 的字段。
构造函数:
- Integer (int arg) :构造表示 int 值的整数对象。
- Integer (String arg) :构造表示字符串值的字符串对象。
整数类方法:
- toBinaryString() : Java.lang.Integer.toBinaryString()方法将参数的整数值转换为二进制表示形式为字符串。
句法
public static String toBinaryString(int arg)
Parameters
arg : integer argument whose Binary representation we want
Return
Binary representation of the argument.
- 位计数(): Java.lang.Integer.bitCount()
方法将参数的整数值转换为二进制字符串,然后返回编号。其中有 1 个。
句法
public static int bitCount(int arg)
Parameters
arg : integer argument whose no. of 1's bit we want
Return
no. of 1's bit present in the argument.
- toHexString() : Java.lang.Integer.toHexString()方法将参数的整数值转换为十六进制表示形式的字符串。
句法
public static String toHexString(int arg)
Parameters
arg : integer argument whose Hexadecimal representation we want
Return
Hexadecimal representation of the argument.
- toOctalString() : Java.lang.Integer.toHexString()方法将参数的整数值转换为十六进制表示形式的字符串。
句法
public static String toHexString(int arg)
Parameters
arg : integer argument whose Hexadecimal representation we want
Return
Hexadecimal representation of the argument.
- parsedatatype() : Java.lang.Integer.parse__()方法返回参数字符串值的原始数据类型。
基数 (r) 表示使用的编号格式位于字符串的基数 'r' 处。
句法
public static int parseInt(String arg)
or
public static int parseInt(String arg, int r)
Parameters
arg : argument passed
r : radix
Return
primitive data type of the argumented String value.
JAVA
// Java code explaining the Integer Class methods
// bitcount(), toBinaryString(), toHexString(), toOctalString(), parse__()
import java.lang.*;
public class NewClass
{
public static void main(String args[])
{
int x = 15, count1, y = 128, count2;
// Use of toBinaryString() method
System.out.println("Binary string of 16 : "
+ Integer.toBinaryString(x));
System.out.println("Binary string of 100 : "
+ Integer.toBinaryString(y));
// Use of bitCount() method
count1 = Integer.bitCount(x);
System.out.println("\n 1's bit present in 16 : "+count1);
count2 = Integer.bitCount(y);
System.out.println(" 1's bit present in 100 : "+count2);
// Use of toHexString() method
System.out.println("\nHexadecimal string of 16 : "
+ Integer.toHexString(x));
System.out.println("Hexadecimal string of 100 : "
+ Integer.toHexString(y));
System.out.println("");
// Use of toOctalString() method
System.out.println("Octal string of 16 : "
+ Integer.toOctalString(x));
System.out.println("Octal string of 100 : "
+ Integer.toOctalString(y) + "\n");
// Use of parseInt() method
int i1 =Integer.parseInt("34");
int i2 = Integer.parseInt("15",8);
double d = Double.parseDouble("54");
System.out.println(i1);
System.out.println(i2);
System.out.println(d);
}
}
JAVA
// Java program explaining Integer class methods
// hashcode(), lowestOneBit(), highestOneBit()
import java.lang.*;
public class NewClass
{
public static void main(String[] args)
{
// Use of incrementExact() method
int f1 = 30, f2 = -56;
f1 = Integer.hashCode(f1);
System.out.println("HashCode value of f1 : "+f1);
f2 = Integer.hashCode(f2);
System.out.println("HashCode value of f2 : "+f2);
System.out.println("\nBinary representation of 30 : "
+ Integer.toBinaryString(f1));
// Use of lowestOneBit() method
// Here it considers 00010 i.e. 2
System.out.println("lowestOneBit of 30 : "
+ Integer.lowestOneBit(f1));
// Use of highestOneBit() method
// Here it considers 10000 i.e. 16
System.out.println("highestOneBit of 30 : "
+ Integer.highestOneBit(f1));
}
}
JAVA
// Java program explaining Integer class methods
// numberOfTrailingZeros(), numberOfLeadingZeros(), reverse()
import java.lang.*;
public class NewClass
{
public static void main(String[] args)
{
int f1 = 30;
// Binary representation of int arg for your understanding
System.out.println("Binary representation of 30 : "
+ Integer.toBinaryString(f1));
// Use of numberOfTrailingZeros() method
// No. of zeros following 1 in 00010 = 1
System.out.println("\nNo. Of Trailing Zeros : "
+ Integer.numberOfTrailingZeros(f1));
// Use of highestOneBit() method
// No. of zeros following 1 in 10000 i.e. 32 - 5 = 27
System.out.println("\nNo. Of Leading Zeros : "
+ Integer.numberOfLeadingZeros(f1));
// Use of Reverse() method
System.out.println("\nReverse : " + Integer.reverse(f1));
}
}
- 输出:
Binary string of 16 : 1111
Binary string of 100 : 10000000
1's bit present in 16 : 4
1's bit present in 100 : 1
Hexadecimal string of 16 : f
Hexadecimal string of 100 : 80
Octal string of 16 : 17
Octal string of 100 : 200
34
13
54.0
- hashCode() : Java.lang.Integer.hashCode()方法返回传递的参数的 hashCode 值。
句法:
public int hashCode(arg)
Parameters:
arg - the argument whose hashCode value we need
Returns:
hashCode value of arg
- minimumOneBit() : Java.lang.Integer.lowestOneBit()方法首先将 int 转换为 Binary,然后寻找 set(1) 位在最低位置,然后重置其余位
例如 arg = 36
It,s 二进制表示 = 0010 0100
它考虑最低位(在 3 处),现在重置其余位,即 0000 0100
所以结果 = 0100 即 4
句法:
public static int lowestOneBit(int arg)
Parameters:
arg - argument passed
Returns:
integer value by only considering lowest 1 bit in the argument.
- highestOneBit() : Java.lang.Integer.highestOneBit()方法首先将 int 转换为 Binary,然后寻找 set(1) 位在最低位置,然后重置其余位
例如 arg = 36
It,s 二进制表示 = 0010 0100
它考虑最高位(在 6 处),现在重置其余位,即 0001 0000
所以结果 = 10000 即 32
句法:
public static int highestOneBit(int arg)
Parameters:
arg - argument passed
Returns:
integer value by only considering highest 1 bit in the argument.
Java
// Java program explaining Integer class methods
// hashcode(), lowestOneBit(), highestOneBit()
import java.lang.*;
public class NewClass
{
public static void main(String[] args)
{
// Use of incrementExact() method
int f1 = 30, f2 = -56;
f1 = Integer.hashCode(f1);
System.out.println("HashCode value of f1 : "+f1);
f2 = Integer.hashCode(f2);
System.out.println("HashCode value of f2 : "+f2);
System.out.println("\nBinary representation of 30 : "
+ Integer.toBinaryString(f1));
// Use of lowestOneBit() method
// Here it considers 00010 i.e. 2
System.out.println("lowestOneBit of 30 : "
+ Integer.lowestOneBit(f1));
// Use of highestOneBit() method
// Here it considers 10000 i.e. 16
System.out.println("highestOneBit of 30 : "
+ Integer.highestOneBit(f1));
}
}
- 输出:
HashCode value of f1 : 30
HashCode value of f2 : -56
Binary representation of 30 : 11110
lowestOneBit of 30 : 2
highestOneBit of 30 : 16
- numberOfTrailingZeros() : Java.lang.Integer.numberOfTrailingZeros()方法将 int 值转换为二进制,然后考虑最低一位并返回 no。紧随其后的零位。
例如 arg = 36
It,s 二进制表示 = 0010 0100
它考虑最高位(6),即 0001 0000
所以结果 = 4
句法:
public static int numberOfTrailingZeros(int arg)
Parameters:
arg - the argument
Returns:
Number of zero bits following the 1 bit at lowest position
- numberOfLeadingZeros() : Java.lang.Integer.numberOfLeadingZeros()方法将 int 值转换为二进制,然后考虑最高一位并返回 no。它前面的零位。
例如 arg = 36
It,s 二进制表示 = 0010 0100
它考虑最高位(在 6 处),即 0010 0000
所以结果 = 32 – 6 即 26
句法:
public static int numberOfLeadingZeros(int arg)
Parameters:
arg - the argument
Returns:
Number of zero bits preceding the 1 bit at highest position
- reverse() : Java.lang.Integer.reverse()方法首先找到传递的参数的 2 的补码,并反转 2 的补码中的位顺序。
句法:
public static int reverse(int arg)
Parameters:
arg - the argument
Returns:
int with reverse order of bits in 2's compliment of the passed argument
Java
// Java program explaining Integer class methods
// numberOfTrailingZeros(), numberOfLeadingZeros(), reverse()
import java.lang.*;
public class NewClass
{
public static void main(String[] args)
{
int f1 = 30;
// Binary representation of int arg for your understanding
System.out.println("Binary representation of 30 : "
+ Integer.toBinaryString(f1));
// Use of numberOfTrailingZeros() method
// No. of zeros following 1 in 00010 = 1
System.out.println("\nNo. Of Trailing Zeros : "
+ Integer.numberOfTrailingZeros(f1));
// Use of highestOneBit() method
// No. of zeros following 1 in 10000 i.e. 32 - 5 = 27
System.out.println("\nNo. Of Leading Zeros : "
+ Integer.numberOfLeadingZeros(f1));
// Use of Reverse() method
System.out.println("\nReverse : " + Integer.reverse(f1));
}
}
- 输出:
Binary representation of 30 : 11110
No. Of Trailing Zeros : 1
No. Of Leading Zeros : 27
Reverse : 2013265920