Java int type
In this chapter you will learn:
- Java int type introduction
- What is octal integer and how to use it in Java
- What is hexadecimal integer and how to use it in Java
- Find out Max, min value of an integer type variable
- How to create Integer using its constructors
- How to compare two integer values
- How to reverse and rotate the integer in binary format
- How to bit oriented operation on int type value
- How to get the sign of an Integer
- How to get the leading and trailing zeros
int type
int is a signed 32-bit type that has a range from -2,147,483,648
to 2,147,483,647
.
When byte and short values are used in an expression
they are promoted to int when the expression is evaluated.
Integer class is the wrapper class for int primitive type.
octal integer(base eight)
Octal values are denoted in Java by a leading zero. valid value 09 will produce an error from the compiler, since 9 is outside of octal's 0 to 7 range.
public class Main {
//from j a v a 2 s. c o m
public static void main(String[] args) {
int i = 010;
System.out.println(i);
}
}
The output:
hexadecimal integer(base 16)
hexadecimal matches with modulo 8 word sizes, such as 8, 16, 32, and 64 bits. You signify a hexadecimal constant with a leading zero-x, (0x or 0X).
The range of a hexadecimal digit is 0 to 15, so A through F (or a through f ) are substituted for 10 through 15.
An integer literal can always be assigned to a long variable. An integer can also be assigned to a char as long as it is within range.
Max, min value of an integer type variable
static int MAX_VALUE
stores the maximum value an int can have.static int MIN_VALUE
stores the minimum value an int can have.static int SIZE
stores the number of bits used to represent an int value.
public class Main {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.SIZE);/*from jav a 2 s . co m*/
}
}
The output:
Create Integer using its constructors
Integer(int value)
creates an Integer object for the int value.Integer(String s)
creates an Integer object for the int value indicated bys
.
public class Main {
public static void main(String[] args) {
/*from ja v a2s. c o m*/
Integer integer1 = new Integer("1");
Integer integer2 = new Integer("2");
System.out.println(integer1);
System.out.println(integer2);
}
}
The output:
The following code creates Integer objects from int literal and string representation
public class Main {
public static void main(String[] args) {
Integer intObj1 = new Integer(10);
Integer intObj2 = new Integer("10");
System.out.println(intObj1);/* jav a 2 s .c om*/
System.out.println(intObj2);
}
}
The output:
Compare two integer values
int compareTo(Integer anotherInteger)
compares two Integer objects numerically.boolean equals(Object obj)
compares this object to the specified object.
The following table lists the return values from compareTo(Integer anotherInteger)
Value | Description |
---|---|
0 | if this Integer is equal to the argument Integer; |
less than 0 | if this Integer is less than the argument Integer; |
greater than 0 | if this Integer is greater than the argument Integer. |
Let's look at an example.
public class Main {
public static void main(String[] args) {
/*from j ava 2s . c om*/
Integer integer1 = new Integer("1");
Integer integer2 = new Integer("2");
System.out.println(integer1.compareTo(integer2));
}
}
The output:
Reverse and rotate the integer in binary format
static int reverse(int i)
reverses the bits.static int reverseBytes(int i)
reverses the bytes.static int rotateLeft(int i, int distance)
rotates the bits by distance to left.static int rotateRight(int i, int distance)
rotates the bits by distance to right.
The following code reverse decimal value 10 in binary format.
public class Main {
public static void main(String[] args) {
// ja v a 2 s. c o m
System.out.println(Integer.reverse(10));
}
}
The output:
The following code rotates value 10 to left for distance of 2.
public class Main {
public static void main(String[] args) {
/* j av a 2 s .c om*/
System.out.println(Integer.rotateLeft(10,2));
}
}
The output:
Bit oriented operation
static int bitCount(int i)
counts the bitsstatic int highestOneBit(int i)
returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit.static int lowestOneBit(int i)
returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit.
The following code does the bit oriented operation on value 10
.
public class Main {
public static void main(String[] args) {
/*from j a v a2 s . co m*/
System.out.println(Integer.bitCount(10));
System.out.println(Integer.highestOneBit(10));
System.out.println(Integer.lowestOneBit(10));
}
}
The output:
Get the sign of an Integer
static int signum(int i)
returns the signum.
The possible values returned from signum(int i)
are listed below:
Value | Meaning |
---|---|
-1 | if i is negative; |
0 | if i is zero; |
1 | if i is positive. |
In the following code we pass in constant value 10 to signum(int i)
.
public class Main {
public static void main(String[] args) {
//from j a v a 2 s . c om
System.out.println(Integer.signum(10));
}
}
The output:
Get the leading and trailing zeros
static int numberOfLeadingZeros(int i)
returns the number of zero bits preceding the highest-order ("leftmost") one-bitstatic int numberOfTrailingZeros(int i)
returns the number of zero bits following the lowest-order ("rightmost") one-bit
public class Main {
public static void main(String[] args) {
// j a v a2 s. c o m
System.out.println(Integer.numberOfLeadingZeros(10));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- How to convert an integer value to byte, double, float, int, long and short
- How to decode a string and return an integer value
- How to convert string to integer
- How to convert integer to string
- How to convert integer to binary, hexadecimal and octal format