(Translated by https://www.hiragana.jp/)
Java.lang.Integer class in Java - GeeksforGeeks
Open In App

Java.lang.Integer class in Java

Last Updated : 21 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Integer class is a wrapper class for the primitive type int which contains several methods to effectively deal with an int value like converting it to a string representation, and vice-versa. An object of the Integer class can hold a single int value. 

Constructors: 

  • Integer(int b): Creates an Integer object initialized with the value provided.

Syntax: 

public Integer(int b)

Parameters:

b : value with which to initialize
  • Integer(String s): Creates an Integer object initialized with the int value provided by string representation. Default radix is taken to be 10.

Syntax: 

public Integer(String s) throws NumberFormatException

Parameters:

s : string representation of the int value 

Throws:

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

Methods: 

1. toString() : Returns the string corresponding to the int value. 

Syntax: 

public String toString(int b)

Parameters :

b : int value for which string representation required.

2. toHexString() : Returns the string corresponding to the int value in hexadecimal form, that is it returns a string representing the int value in hex characters-[0-9][a-f] 

Syntax: 

public String toHexString(int b)

Parameters:

b : int value for which hex string representation required.

3. toOctalString() : Returns the string corresponding to the int value in octal form, that is it returns a string representing the int value in octal characters-[0-7] 

Syntax: 

public String toOctalString(int b)

Parameters:

b : int value for which octal string representation required.

4. toBinaryString() : Returns the string corresponding to the int value in binary digits, that is it returns a string representing the int value in hex characters-[0/1] 

Syntax:

public String toBinaryString(int b)

Parameters:

b : int value for which binary string representation required.

5. valueOf() : returns the Integer object initialised with the value provided. 

Syntax: 

public static Integer valueOf(int b)

Parameters:

b : a int value

Syntax: 

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

Parameters:

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

Throws:

NumberFormatException : if String cannot be parsed to a int value in given radix.
  • valueOf(String val): Another overloaded function which provides function similar to new Integer(Integer.parseInt(val,10))

Syntax: 

public static Integer valueOf(String s)
throws NumberFormatException

Parameters:

s : a String object to be parsed as int

Throws:

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

6. parseInt() : returns int value by parsing the string in radix provided. Differs from valueOf() as it returns a primitive int value and valueOf() return Integer object. 

Syntax: 

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

Parameters:

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

Throws:

NumberFormatException : if String cannot be parsed to a int value in given radix.
  • Another overloaded method containing only String as a parameter, radix is by default set to 10.

Syntax: 

public static int parseInt(String val)
throws NumberFormatException

Parameters:

val : String representation of int 

Throws:

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

7. getInteger(): returns the Integer object representing the value associated with the given system property or null if it does not exist. 

Syntax: 

public static Integer getInteger(String prop)

Parameters :

prop : System property
  • Another overloaded method which returns the second argument if the property does not exist, that is it does not return null but a default value supplied by user. 
     

Syntax: 

public static Integer getInteger(String prop, int val)

Parameters:

prop : System property
val : value to return if property does not exist.
  • Another overloaded method which parses the value according to the value returned, that is if the value returned starts with “#”, than it is parsed as hexadecimal, if starts with “0”, than it is parsed as octal, else decimal. 
     

Syntax: 

public static Integer getInteger(String prop, Integer val)

Parameters:

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

8. decode() : returns a Integer object holding the decoded value of string provided. String provided must be of the following form else NumberFormatException will be thrown- 
Decimal- (Sign)Decimal_Number 
Hex- (Sign)”0x”Hex_Digits 
Hex- (Sign)”0X”Hex_Digits 
Octal- (Sign)”0″Octal_Digits 
 

Syntax: 

public static Integer decode(String s)
throws NumberFormatException

Parameters:

s : encoded string to be parsed into int val

Throws:

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

9. rotateLeft() : Returns a primitive int by rotating the bits left by given distance in two’s complement form of the value given. When rotating left, the most significant bit is moved to the right-hand side, or least significant position i.e. cyclic movement of bits takes place. Negative distance signifies right rotation. 

Syntax: 

public static int rotateLeft(int val, int dist)

Parameters:

val : int value to be rotated
dist : distance to rotate

10. rotateRight() : Returns a primitive int by rotating the bits right by given distance in the twos complement form of the value given. When rotating right, the least significant bit is moved to the left hand side, or most significant position i.e. cyclic movement of bits takes place. Negative distance signifies left rotation. 

Syntax: 

public static int rotateRight(int val, int dist)

Parameters:

val : int value to be rotated
dist : 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));
    }
}


Output:

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

11. byteValue() : returns a byte value corresponding to this Integer Object. 

Syntax: 

public byte byteValue()

12. shortValue() : returns a short value corresponding to this Integer Object. 

Syntax: 

public short shortValue()

13. intValue() : returns a int value corresponding to this Integer Object. 

Syntax: 

public int intValue()

13. longValue() : returns a long value corresponding to this Integer Object. 

Syntax:

public long longValue()

14. doubleValue() : returns a double value corresponding to this Integer Object. 

Syntax: 

public double doubleValue()

15. floatValue() : returns a float value corresponding to this Integer Object. 

Syntax: 

public float floatValue()

16. hashCode() : returns the hashcode corresponding to this Integer Object. 

Syntax: 

public int hashCode()

17. bitcount() : Returns number of set bits in twos complement of the integer given. 

Syntax: 

public static int bitCount(int i)

Parameters:

i : int value whose set bits to count

18. numberOfLeadingZeroes() : Returns number of 0 bits preceding the highest 1 bit in twos complement form of the value, i.e. if the number in twos complement form is 0000 1010 0000 0000, then this function would return 4. 

Syntax: 

public static int numberofLeadingZeroes(int i)

Parameters:

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

19. numberOfTrailingZeroes() : Returns number of 0 bits following the last 1 bit in twos complement form of the value, i.e. if the number in twos complement form is 0000 1010 0000 0000, then this function would return 9. 

Syntax: 

public static int numberofTrailingZeroes(int i)

Parameters:

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

20. highestOneBit() : Returns a value with at most a single one bit, in the position of highest one bit in the value given. Returns 0 if the value given is 0, that is if the number is 0000 0000 0000 1111, then this function return 0000 0000 0000 1000 (one at highest one bit in the given number) 

Syntax: 

public static int highestOneBit(int i)

Parameters:

i : int value 

21. LowestOneBit() : Returns a value with at most a single one bit, in the position of lowest one bit in the value given. Returns 0 if the value given is 0, that is if the number is 0000 0000 0000 1111, then this function return 0000 0000 0000 0001 (one at highest one bit in the given number) 

Syntax: 

public static int LowestOneBit(int i)

Parameters:

i : int value 

22. equals() : Used to compare the equality of two Integer objects. This method returns true if both the objects contain the same int value. Should be used only if checking for equality. In all other cases, the compareTo method should be preferred. 

Syntax: 

public boolean equals(Object obj)

Parameters:

obj : object to compare with

23. compareTo() : Used to compare two Integer objects for numerical equality. This should be used when comparing two Integer values for numerical equality as it would differentiate between less and greater values. Returns a value less than 0,0, a value greater than 0 for less than, equal to and greater than. 

Syntax: 

public int compareTo(Integer b)

Parameters:

b : Integer object to compare with

24. compare(): Used to compare two primitive int values for numerical equality. As it is a static method therefore it can be used without creating any object of Integer. 

Syntax: 

public static int compare(int x,int y)

Parameters:

x : int value
y : another int value

25. signum() : returns -1 for negative values, 0 for 0 and +1 for values greater than 0. 

Syntax: 

public static int signum(int val)

Parameters:

val : int value for which signum is required.

26. reverse(): returns a primitive int value reversing the order of bits in two’s complement form of the given int value. 

Syntax: 

public static int reverseBytes(int val)

Parameters:

val : int value whose bits to reverse in order.

27. reverseBytes() : returns a primitive int value reversing the order of bytes in two’s complement form of the given int value. 

Syntax: 

public static int reverseBytes(int val)

Parameters:

val : int value whose bits to reverse in order.

28. static int compareUnsigned(int x, int y): This method compares two int values numerically treating the values as unsigned.

Syntax: 

public static int compareUnsigned(int x, int y)

29. static int divideUnsigned(int dividend, int divisor): This method returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.

Syntax: 

public static int divideUnsigned(int dividend, int divisor)

30. static int max(int a, int b): This method returns the greater of two int values as if by calling Math.max.

Syntax: 

public static int max(int a, int b)

31. static int min(int a, int b): This method returns the smaller of two int values as if by calling Math.min.

Syntax: 

public static int min(int a, int b)

32. static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix): This method parses the CharSequence argument as an unsigned int in the specified radix, beginning at the specified beginIndex and extending to endIndex – 1.

Syntax: 

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

33. static int parseUnsignedInt(String s): This method parses the string argument as an unsigned decimal integer.

Syntax: 

public static int parseUnsignedInt(String s)
throws NumberFormatException

34. static int parseUnsignedInt(String s, int radix): This method parses the string argument as an unsigned integer in the radix specified by the second argument.

Syntax: 

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

35. static int remainderUnsigned(int dividend, int divisor): This method returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.

Syntax: 

public static int remainderUnsigned(int dividend, int divisor)

36. static int sum(int a, int b): This method adds two integers together as per the + operator.

Syntax: 

public static int sum(int a, int b)

37. static long toUnsignedLong(int x): This method converts the argument to a long by an unsigned conversion.

Syntax: 

public static long toUnsignedLong(int x)    

38. static String toUnsignedString(int i): This method returns a string representation of the argument as an unsigned decimal value.

Syntax: 

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
        // reverseBytes() 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);
    }
}


Output : 
 

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

 

Initialization of Integer wrapper class in Java :

Type 1: Initializing directly:

A constant object of Integer class will be created inside the space of constants in the heap memory. Space of constants: It is just to imagine for better understanding that there is some space for constants in heap memory.

Example: 

Integer x = 200;  //initializing directly
x = 300;      //modifying x
x = 10;           //modifying x again

Integer x = 200 

  • The compiler converts the above statement into: Integer x=Integer.valueOf(200) . This is known as “Autoboxing”. The primitive integer value 200 is converted into an object.

( To understand Autoboxing & Unboxing check here: https://www.geeksforgeeks.org/autoboxing-unboxing-java/ ) 

  • x points to 200 which is present in the space of constants. Refer Fig. 1.
Examp img 1

Fig. 1

x = 300

  • Autoboxing is done again because x is an Integer class object which is directly initialized.
  • Note: The directly initialized object(x) cannot be modified as it is a constant. When we try to modify the object by pointing to a new constant(300), the old constant(200) will be present in heap memory, but the object will be pointing the new constant.
  • x points to 300 which is present in the space of constants. Refer Fig. 2.

Fig. 2

x = 10

  • Note: By default for the values -128 to 127, Integer.valueOf() method will not create a new instance of Integer. It returns a value from its cache.
  • x points 10 which is present in cache.

Fig. 3

If we assign x = 200 or x=300 next time, it will point to the value 200 or 300 which is present already in space of constants. If we assign values to x other than these two values, then it creates a new constant.

(Check the Integer wrapper class comparison topic for better understanding)

Type 2: Initializing dynamically:

An Integer class object which is not a constant will be created outside the space of constants. It also creates a Integer constant inside the space of constants. The variable will be pointing to the Integer object & not the Integer constant.

Example: 

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

Integer a = new Integer(250) 

  • 250 is created inside and outside the space of constants. Variable ‘a’ will be pointing the value that is outside the space of constants. Refer Fig. 4.

Fig. 4

a = 350;  

  • After autoboxing, ‘a’ will be pointing to 350. Refer Fig. 5.

Fig. 5

If we assign a = 250 next time, it will not point to the object already present with same value, it will create a new object.

References: Official Java Documentation 
 

 



Previous Article
Next Article

Similar Reads

Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
15+ min read
Java.lang.Class class in Java | Set 2
Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo
15+ min read
Java.lang.Boolean Class in Java
Java provides a wrapper class Boolean in java.lang package. The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field, whose type is boolean. In addition, this class provides useful methods like to convert a boolean to a String and a String to a boolean, while dealing with a boolea
8 min read
Java.Lang.Float class in Java
Float class is a wrapper class for the primitive type float which contains several methods to effectively deal with a float value like converting it to a string representation, and vice-versa. An object of the Float class can hold a single float value. There are mainly two constructors to initialize a Float object- Float(float b): Creates a Float o
6 min read
Java.Lang.Short class in Java
Short class is a wrapper class for the primitive type short which contains several methods to effectively deal with a short value like converting it to a string representation, and vice-versa. An object of Short class can hold a single short value. There are mainly two constructors to initialize a Short object- Short(short b): Creates a Short objec
6 min read
Java.lang.Enum Class in Java
Enum class is present in java.lang package. It is the common base class of all Java language enumeration types. For information about enums, refer enum in java Class Declaration public abstract class Enum<E extends Enum> extends Object implements Comparable, Serializable As we can see, that Enum is an abstract class, so we can not create obje
8 min read
Java.lang.StackTraceElement class in Java
An element in a stack trace, as returned by Throwable.getStackTrace(). Each element represents a single stack frame. All stack frames except for the one at the top of the stack represent a method invocation. The frame at the top of the stack represents the execution point at which the stack trace was generated. This class describes single stack fra
5 min read
Java lang.Long.builtcount() method in Java with Examples
java.lang.Long.bitCount() is a built in function in Java that returns the number of set bits in a binary representation of a number. It accepts a single mandatory parameter number whose number of set bits is returned. Syntax: public static long bitCount(long num) Parameters: num - the number passed Returns: the number of set bits in the binary repr
3 min read
Java.lang.Short toString() method in Java with Examples
toString(short) The static toString() method of java.lang.Short returns a new String object representing the specified short. The radix is assumed to be 10.This is a static method hence no object of Short class is required for calling this method. Syntax: public static String toString(short b) Parameters: This method accepts a parameter b which is
2 min read
Check for integer overflow on multiplication
Given two integer a and b, find whether their product (a x b) exceed the signed 64 bit integer or not. If it exceed print Yes else print No. Examples: Input : a = 100, b = 200 Output : No Input : a = 10000000000, b = -10000000000Output : Yes Approach : If either of the number is 0, then it will never exceed the range.Else if the product of the two
4 min read
Storage of integer and character values in C
Integer and character variables are used so often in the programs, but how these values are actually stored in C are known to few. Below are a few examples to understand this: Taking a positive integer value as char: #include <stdio.h> int main() { char a = 278; printf("%d", a); return 0; } Output: 22 Explanation: First, compiler co
2 min read
Extended Integral Types (Choosing the correct integer size in C/C++)
C/C++ has very loose definitions on its basic integer data types (char, short, int, long, and long long). The language guarantees that they can represent at least some range of values, but any particular platform (compiler, operating system, hardware) might be larger than that.A good example is long. On one machine, it might be 32 bits (the minimum
3 min read
Assigning an integer to float and comparison in C/C++
Consider the below C++ program and predict the output. #include <iostream> using namespace std; int main() { float f = 0xffffffff; unsigned int x = 0xffffffff; // Value 4294967295 if (f == x) cout << "true"; else cout << "false"; return 0; } The output of above program is false if the "IEEE754 32-bit single flo
2 min read
Find One's Complement of an Integer
Given an integer n, find the one's complement of the integer. Examples: Input : n = 5 Output : 2 Input : n = 255 Output : 0 Input : n = 26 Output : 5Recommended PracticeOne's ComplementTry It! Basic Approach : The naïve approach to solve the problem would be to first convert the given number into its binary representation and then change every 1's
7 min read
Java.net.Authenticator class in Java
Authenticator class is used in those cases where an authentication is required to visit some URL. Once it is known that authentication is required, it prompts the user for the same or uses some hard-coded username and password. To use this class, following steps are followed- Create a class that extends the Authenticator. Lets name it customAuth.Ov
3 min read
Java.util.zip.DeflaterOutputStream class in Java
Java.util.zip.DeflaterInputStream class in Java This class implements an output stream filter for compressing data in the "deflate" compression format. It is also used as the basis for other types of compression filters, such as GZIPOutputStream. Constructors and Description DeflaterOutputStream(OutputStream out) : Creates a new output stream with
3 min read
Java.io.ObjectOutputStream Class in Java | Set 2
Java.io.ObjectOutputStream Class in Java | Set 1 More Methods: void write(byte[] buf) : Writes an array of bytes. This method will block until the byte is actually written. Syntax :public void write(byte[] buf) throws IOException Parameters: buf - the data to be written Throws: IOException void write(byte[] buf, int off, int len) : Writes a sub arr
8 min read
Java.net.NetworkInterface class in Java
This class represents network interface, both software as well as hardware, its name, list of IP addresses assigned to it, and all related information. It can be used in cases when we want to specifically use a particular interface for transmitting our packet on a system with multiple NICs. What is a Network Interface? A network interface can be th
6 min read
Java.net.Inet4Address class in Java
This class extends the InetAddress class and represents an IPv4 address. It provides methods to interpret and display useful information about IP addresses. Methods of this class take input in 4 formats: d.d.d.d: When this format is used as input, each of the given values are assigned to 4 bytes of the IP address from left to right.d.d.d: When this
3 min read
Java.net.Inet6Address class in Java
This class represents IPv6 address and extends the InetAddress class. Methods of this class provide facility to represent and interpret IPv6 addresses. Methods of this class takes input in the following formats: x:x:x:x:x:x:x:x -This is the general form of IPv6 address where each x can be replaced with a 16 bit hexadecimal value of the address. Not
5 min read
Java.net.InetSocketAddress class in Java
This class implements IP socket address( combination of IP address and port number). The objects of this class are immutable and can be used for binding, connecting purposes. Constructors : 1. InetSocketAddress(InetAddress addr, int port) : This constructor is similar to the general structure of a socket address with the attributes for Inet address
4 min read
Java.util.TimerTask class in Java
TimerTask is an abstract class defined in java.util package. TimerTask class defines a task that can be scheduled to run for just once or for repeated number of time. In order to define a TimerTask object, this class needs to be implemented and the run method need to be overridden. The run method is implicitly invoked when a timer object schedules
3 min read
Java.util.Timer Class in Java
Timer class provides a method call that is used by a thread to schedule a task, such as running a block of code after some regular instant of time. Each task may be scheduled to run once or for a repeated number of executions. Each timer object is associated with a background thread that is responsible for the execution of all the tasks of a timer
6 min read
Java.util.concurrent.Semaphore Class in Java
A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, then access is denied. What the counter is counting are permits that allow access to the shared resource. Thus, to access the resource, a thread must be granted a permit from the semaphore. Synta
6 min read
Java.net.URI class in Java
This class provides methods for creating URI instances from its components or by parsing the string form of those components, for accessing and retrieving different components of a URI instance. What is URI? URI stands for Uniform Resource Identifier. A Uniform Resource Identifier is a sequence of characters used for identification of a particular
11 min read
Java.net.DatagramSocket class in Java
Datagram socket is a type of network socket which provides connection-less point for sending and receiving packets. Every packet sent from a datagram socket is individually routed and delivered. It can also be used for sending and receiving broadcast messages. Datagram Sockets is the java's mechanism for providing network communication via UDP inst
9 min read
Java.net.DatagramPacket class in Java
This class provides facility for connection less transfer of messages from one system to another. Each message is routed only on the basis of information contained within the packet and it may be possible for different packets to route differently. There is also no guarantee as to whether the message will be delivered or not, and they may also arri
5 min read
Java.net.MulticastSocket class in Java
This class is used for sending and receiving multicast IP packets. It extends DatagramSocket class and provides additional functionality for joining groups. A message sent to the group IP address will be received by all the clients who have joined the group. This must be kept in mind that for sending packets to the group, datagram socket doesn't ha
6 min read
Java.net.URLEncoder class in Java
This class is a utility class for HTML form encoding. Encoding makes the form of URL more reliable and secure. When the user request is triggered by a get method, the form parameters and their values are appended at the end of URL after a '?' sign. The problem arises when special characters are used for their values. In general case, HTML handles t
3 min read
Java.net.URLDecoder class in Java
This is a utility class for HTML form decoding. It just performs the reverse of what URLEncoder class do, i.e. given an encoded string, it decodes it using the scheme specified. Generally when accessing the contents of request using getParameter() method in servlet programming, the values are automatically decoded before they are returned. But some
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg