Java int type

In this chapter you will learn:

  1. Java int type introduction
  2. What is octal integer and how to use it in Java
  3. What is hexadecimal integer and how to use it in Java
  4. Find out Max, min value of an integer type variable
  5. How to create Integer using its constructors
  6. How to compare two integer values
  7. How to reverse and rotate the integer in binary format
  8. How to bit oriented operation on int type value
  9. How to get the sign of an Integer
  10. 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 by s.
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)

ValueDescription
0if this Integer is equal to the argument Integer;
less than 0if this Integer is less than the argument Integer;
greater than 0if 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 bits
  • static 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:

ValueMeaning
-1if i is negative;
0if i is zero;
1if 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-bit
  • static 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:

  1. How to convert an integer value to byte, double, float, int, long and short
  2. How to decode a string and return an integer value
  3. How to convert string to integer
  4. How to convert integer to string
  5. How to convert integer to binary, hexadecimal and octal format
Home » Java Tutorial » Data Types

Java Primitive Data Types
Java byte type
Java byte type conversion
Java short type
Java short type conversion
Java int type
Java int type conversion
Java long type
Java long type conversion
Java float type
Java float type conversion
Java double type
Java double type creation and comparison
Java double type conversion
Java Automatic Type Conversion and Casting
Data type casting
Java type promotion
Java char type
Java char conversion
Java char value and its attributes
Java boolean type
Java boolean type conversion
Autoboxing and auto-unboxing
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array sort
Array to List
Array fill value
Array to String
BigInteger class
BigInteger creation
BigInteger add, subtract, multiply and divide
BigInteger power and modPow
BigInteger conversion
BigInteger to String
BigInteger bit and,or,xor,test,flip,negate
BigInteger bit shift left and right
BigInteger prime value
BigDecimal
BigDecimal constants
BigDecimal Rounding mode
BigDecimal creation
BigDecimal calculation
BigDecimal convert
BigDecimal Comparison
BigDecimal to String
BigDecimal decimal point
BigDecimal precision
BigDecimal format
Currency class