A Wrapper class is one whose object contains or wraps primitive data types. When we create an object for a wrapper class, it has a field where we can store primitive data types. To put it another way, we can turn a primitive value into a wrapper class object.
Primitive Type | Wrapper class |
---|---|
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
Java program that converts all primitives into /wrapper objects and vice versa.
public class WrapperExample{
public static void main(String args[]){
byte b=20;
short s=90;
int i=76;
long l=45;
float f=50.0F;
double d=69.0D;
char c='r';
boolean b2=true;
//Autoboxing: Converting primitives into objects
Byte byteobj=b;
Short shortobj=s;
Integer intobj=i;
Long longobj=l;
Float floatobj=f;
Double doubleobj=d;
Character charobj=c;
Boolean boolobj=b2;
//Printing objects
System.out.println("---Printing object values---");
System.out.println("Byte object: "+byteobj);
System.out.println("Short object: "+shortobj);
System.out.println("Integer object: "+intobj);
System.out.println("Long object: "+longobj);
System.out.println("Float object: "+floatobj);
System.out.println("Double object: "+doubleobj);
System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);
//Unboxing: Converting Objects to Primitives
byte bytevalue=byteobj;
short shortvalue=shortobj;
int intvalue=intobj;
long longvalue=longobj;
float floatvalue=floatobj;
double doublevalue=doubleobj;
char charvalue=charobj;
boolean boolvalue=boolobj;
//Printing primitives
System.out.println("---Printing primitive values---");
System.out.println("byte value: "+bytevalue);
System.out.println("short value: "+shortvalue);
System.out.println("int value: "+intvalue);
System.out.println("long value: "+longvalue);
System.out.println("float value: "+floatvalue);
System.out.println("double value: "+doublevalue);
System.out.println("char value: "+charvalue);
System.out.println("boolean value: "+boolvalue);
}}
---Printing object values---
Byte object: 20
Short object: 90
Integer object: 76
Long object: 45
Float object: 50.0
Double object: 69.0
Character object: r
Boolean object: true
---Printing primitive values---
byte value: 20
short value: 90
int value: 76
long value: 45
float value: 50.0
double value: 69.0
char value: r
boolean value: true
The Integer class is a wrapper class for the primitive type int that contains several methods for dealing with int values effectively, such as converting them to string representations and vice versa. An Integer class object can only hold one int value.
Syntax:
public String toString(int b)
2. toHexString(): Returns the string that corresponds to the int value in hexadecimal form, that is, a string that represents the int value in hex characters-[0-9]. [a-f]
Syntax:
public String toHexString(int b)
Syntax:
public String toOctalString(int b)
Syntax:
public String toBinaryString(int b)
Syntax:
public static Integer valueOf(int b)
Syntax:
public static int parseInt(String val, int radix)
throws NumberFormatException
Syntax:
public static Integer getInteger(String prop)
8.decode(): returns an Integer object with the value of the decoded string. The supplied string must take the following form if not NumberFormatException will be thrown –
Decimal- (Sign)Decimal Number
Hex- (Sign)”0x”Hex Digits in Hex
Hex-(Sign) “0X”Hex Digits
Octal-(Sign) “0” Octal Digits
Syntax:
public static Integer decode(String s)
throws NumberFormatException
Syntax:
public static int rotateLeft(int val, int dist)
Syntax:
public static int rotateRight(int val, int dist)
public class IntegerTest {
public static void main(String[] args)
{
int x = 60;
String xx = "35";
System.out.println("toString(x) = " + Integer.toString(x));
Integer z = Integer.valueOf(x);
System.out.println("valueOf(x) = " + z);
z = Integer.valueOf(xx);
System.out.println("valueOf(xx) = " + z);
int zz = Integer.parseInt(xx);
System.out.println("parseInt(xx) = " + zz);
// Call decode() method to decode the binary, hexa, octal and decimal.
// string to corresponding int values.
String decimal = "20";
String binary = "0100";
String octal = "005";
String hex = "0x0f";
Integer dec = Integer.decode(decimal);
System.out.println("decode(45) = " + dec);
dec = Integer.decode(binary);
System.out.println("decode(0100): " +dec);
dec = Integer.decode(octal);
System.out.println("decode(005) = " + dec);
dec = Integer.decode(hex);
System.out.println("decode(0x0f) = " + dec);
}
}
toString(x) = 60
valueOf(x) = 60
valueOf(xx) = 35
parseInt(xx) = 35
decode(45) = 20
decode(0100): 64
decode(005) = 5
decode(0x0f) = 15
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IntegerTest {
public static void main(String[] args) throws IOException
{
// Accept an Integer number from keyboard.
InputStreamReader sr = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(sr);
System.out.println("Enter an integer number:");
String str = bf.readLine();
// Converting string into int type.
int i = Integer.parseInt(str);
System.out.println("In decimal format: " +i);
// Converting into other number systems.
str = Integer.toBinaryString(i);
System.out.println("In binary format: " +str);
str = Integer.toHexString(i);
System.out.println("In hexdecimal format: " +str);
str = Integer.toOctalString(i);
System.out.println("In octal format: " +str);
}
}
Enter an integer number:
12
In decimal format: 12
In binary format: 1100
In hexdecimal format: c
In octal format: 14
Note: also read about the Java Swing Components and Containers -II
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…