0% found this document useful (0 votes)
6 views4 pages

Java Integer.parseInt() Explained

Uploaded by

debasisdas4531
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Java Integer.parseInt() Explained

Uploaded by

debasisdas4531
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1

[Link]() in Java is a static method of the Integer wrapper


class used to convert a string representation of a number into its primitive int data type
equivalent.
Key characteristics and usage:
 Purpose:
The primary function of parseInt() is to parse a string argument and return it as a
signed decimal integer.
 Static Method:
Being a static method, it is invoked directly on the Integer class, not on an instance
of Integer.
 Return Type:
It returns a primitive int value.
 Overloaded Versions:
parseInt() has overloaded versions:
 public static int parseInt(String s): Parses the string s as a
signed decimal integer.
 public static int parseInt(String s, int radix):
Parses the string s as an integer in the specified radix (e.g., 2 for binary, 8 for octal, 10 for
decimal, 16 for hexadecimal).

Exception Handling:
If the input string cannot be successfully parsed into an integer (e.g., if it contains non-
numeric characters, or is not a valid representation in the given
radix), parseInt() throws a NumberFormatException. It is common
practice to enclose calls to parseInt() within a try-catch block to handle this
potential exception.

Example: -
public class ParseIntExample {
public static void main(String[] args) {
String numString = "123";
String hexString = "FF";

try {
int decimalNum = [Link](numString);
[Link]("Decimal number: " +
2

decimalNum);

// Output: Decimal number: 123


int hexNum = [Link](hexString, 16);
[Link]("Hexadecimal number: " +
hexNum); // Output: Hexadecimal number: 255

String invalidString = "abc";


int invalidNum = [Link](invalidString);
// This will throw a NumberFormatException
} catch (NumberFormatException e) {
[Link]("Error: " +
[Link]()); // Output: Error: For input string:
"abc"
}
}
}

Wrapper Classes :-
Wrapper classes in Java provide a mechanism to convert primitive data types
(like int, char, double, etc.) into objects and vice versa. They "wrap" the
primitive value within an object, allowing primitives to be used in contexts
where objects are required.
Why are Wrapper Classes needed?
 Collections:
Java Collections Framework (e.g., ArrayList, HashMap) can only store
objects, not primitive types. Wrapper classes enable storing primitive values
in collections.
 Generics:
Generics in Java work with objects, not primitives. Wrapper classes allow
using primitive types with generic types.
 Utility Methods:
Wrapper classes offer various utility methods for converting between primitive
types and strings, and for performing other operations on the wrapped values
(e.g., [Link](), [Link]()).
3

 Null Values: - Primitive types cannot hold a null value, but wrapper class
objects can, which can be useful in certain scenarios like representing the
absence of a value.
Examples of Wrapper Classes and their Primitive Counterparts:
Primitive Type Wrapper Class

byte Byte

short Short

int Integer

long Long

float Float

double Double

char Character

boolean Boolean

Autoboxing and Unboxing (Since J2SE 5.0):


Java automatically handles the conversion between primitives and their
corresponding wrapper objects through autoboxing (primitive to object) and
unboxing (object to primitive).
Example:
Java

public class WrapperClassExample {


public static void main(String[] args) {
// Autoboxing: int primitive to Integer object
int primitiveInt = 100;
Integer wrapperInt = primitiveInt; // Autoboxing

[Link]("Wrapper Integer: " +


wrapperInt);

// Unboxing: Integer object to int primitive


int anotherPrimitiveInt = wrapperInt; // Unboxing
4

[Link]("Unboxed int: " +


anotherPrimitiveInt);

// Using wrapper class in a collection


[Link]<Integer> numbers = new
[Link]<>();
[Link](50); // Autoboxing 50 (int) to
Integer
[Link](200);

[Link]("ArrayList of Integers: " +


numbers);

// Using a utility method from a wrapper class


String strNumber = "123";
int parsedInt = [Link](strNumber); // Parsing a String to int
[Link]("Parsed int from String: " + parsedInt);
}
}

You might also like