Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Integer wrapper class

Posted By: Sebastien Anderson     Category: Java     Views: 9932

This article explains about wrapper class integer with example in java.

An Integer object encapsulates a simple int value. Integer is a wrapper class for int

Integer is an example of a class that provides both static and instance methods. It defines MAX_VALUE and MIN_VALUE as two of its static variables. These contains the maximum and minimum values that can be accommodated by the 32 bits of a simple int type. 

Note that there is no way to change that value encapsulated by an Integer object. It is immutable after it has been created.

Below are the static variables available in an Integer class.

Static variables in Integer class

 Variables

Description 

 static int MAX_VALUE 

A constant holding the maximum value an int can have, 231-1. 

 static int MIN_VALUE 

A constant holding the minimum value an int can have, -231. 

 static int SIZE 

The number of bits used to represent an int value in two's complement binary form.

 static Class<Integer> TYPE 

The Class instance representing the primitive type int.


Static methods in Integer class

 Method

Description 

static int bitCount(int i) 

Returns the number of one-bits in the two's complement binary representation of the specified int value.

static Integer decode(String nm) 

Decodes a String into an Integer.

static Integer getInteger(String nm) 

Determines the integer value of the system property with the specified name. 

static Integer getInteger(String nm, int val)  

Determines the integer value of the system property with the specified name. 

static Integer getInteger(String nm, Integer val)  

Returns the integer value of the system property with the specified name.

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 in the specified int value. 

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 in the specified int value. 

static int numberOfLeadingZeros(int i)  

Returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified int value. 

static int numberOfTrailingZeros(int i)  

Returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified int value.

static int parseInt(String s)  

Parses the string argument as a signed decimal integer. 

static int parseInt(String s, int radix) 

Parses the string argument as a signed integer in the radix specified by the second argument. 

static int reverse(int i)  

Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value. 

static int reverseBytes(int i)  

Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified int value. 

static int rotateLeft(int i, int distance)  

Returns the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits. 

static int rotateRight(int i, int distance) 

Returns the value obtained by rotating the two's complement binary representation of the specified int value right by the specified number of bits.

static int signum(int i) 

Returns the signum function of the specified int value. 

static String toBinaryString(int i) 

Returns a string representation of the integer argument as an unsigned integer in base 2. 

static String toHexString(int i)  

Returns a string representation of the integer argument as an unsigned integer in base 16. 

static String toOctalString(int i)  

Returns a string representation of the integer argument as an unsigned integer in base 8. 

static String toString(int i)  

Returns a String object representing the specified integer. 

static String toString(int i, int radix)  

Returns a string representation of the first argument in the radix specified by the second argument. 

static Integer valueOf(int i)  

Returns a Integer instance representing the specified int value. 

static Integer valueOf(String s) 

Returns an Integer object holding the value of the specified String.

static Integer valueOf(String s, int radix)  

Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.



Instance methods in integer class

 Method

Description 

byte byteValue() 

Returns the value of this Integer as a byte. 

int compareTo(Integer anotherInteger) 

Compares two Integer objects numerically. 

double doubleValue()  

Returns the value of this Integer as a double. 

boolean equals(Object obj)  

Compares this object to the specified object. 

floatfloatValue()  

Returns the value of this Integer as a float. 

int hashCode()  

Returns a hash code for this Integer. 

int intValue() 

Returns the value of this Integer as an int. 

long longValue()  

Returns the value of this Integer as a long. 

short shortValue()  

Returns the value of this Integer as a short. 

String toString() 

Returns a String object representing this Integer's value.  



Example of Integer class 

class stringToInt
{
     public static void main(String args[])
     {
String s = "123";
Integer intObj = Integer.valueOf(s);
  int i = obj.intValue();
i += 2;
System.out.println(i);
     }
}

Output
125

In above example instance method intValue() is used to obtain a simple int which is equivalent to the value encapsulated by obj.
  
Share: 

 
 

Didn't find what you were looking for? Find more on Integer wrapper class Or get search suggestion and latest updates.

Sebastien Anderson
Sebastien Anderson author of Integer wrapper class is from Perth, Australia.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!