Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions

Wrapper Classes

As we have already seen, a variable of primitive data type is by default passed by value and not by reference. Quite often, there might arise the need to consider such variables of primitive data type as reference types. The solution to this lies in the wrapper classes provided by Java. These classes are used to wrap the data in a new object which contains the value of that variable. This object can then be used in a way similar to how other objects are used. For example, we wrap the number 34 in an Integer object in the following way:

Integer intObject = new Integer (34);

The Integer class is the wrapper class that has been provided for the int data type. Similarly, there are wrapper classes for the other data types too. The following table lists the data types and their corresponding wrapper classes.

Data Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Note that the wrapper classes of all the primitive data types except int and char have the same name as that of the data type.

A wrapper class also contains a number of other different methods which may be used in the processing of variables of the corresponding data type. For example, the Character wrapper class contains methods which can be used to check if a character is a digit or a letter or a whitespace and so on.

Creating objects of the Wrapper classes

All the wrapper classes have constructors which can be used to create the corresponding Wrapper class objects by passing either a String or a variable of the same data type as that of the type to which the wrapper class corresponds, except for the Character wrapper class whose object cannot be created with a String. Also, the Float wrapper class allows its object to be created using a double value.

For example, we can create an Integer object which wraps the int 34 in either of the following two ways:

Integer intObject = new Integer (34);
Integer intObject = new Integer ( "34");

Retrieving the value wrapped by a wrapper class object

Each of the eight wrapper classes have a method to retrieve the value that was wrapped in the object. These methods have the form *Value() where star refers to the corresponding data type. For example, to retrieve the value stored in the Integer object intObject, we use the following statement.

int x = intObject.intValue();

Similarly, we have methods for the other seven wrapper classes: byteValue(), shortValue(), longValue(), floatValue(), doubleValue(), charValue(), booleanValue().

Auto boxing and auto unboxing

Creating a wrapper class object using the constructors and retrieving the values wrapped by those objects using the methods as shown above can become quite cumbersome. As an alternative, there exists auto boxing and uutounboxing. Auto boxing refers to an implicit call to the constructor and auto unboxing refers to an implicit call to the *value() method. Therefore, a new wrapper object can be created by specifying the value to be wrapped just as we would do for a primitive data type variable. Also, the value can be retrieved and used in a simple way by specifying the object name. Look at the following code:

Integer intObject = 34;
int x=intObject;
int x = intObject + 7;

The above statements are equivalent to the following set of statements

Integer intObject = new Integer (34);
int x = intObject.intValue();
int x = intObject .intValue()+ 7;

Similarly, auto boxing and auto boxing apply to other wrapper classes also.

Conversion between data types

The wrapper classes also provide methods which can be used to convert a String to any of the primitive data types, except character. All these methods are static. These methods have the format parse*() where * refers to any of the primitive data types except char. And to convert any of the primitive data type value to a String, we use the valueOf() methods of the String class which through method overloading and implicit casting can accept any of the eight primitive types.

int x = Integer.parseInt("34"); // x=34
double y = Double.parseDouble("34.7"); // y =34.7
String s1= String.valueOf('a'); // s1="a"
String s2=String.valueOf(true); // s2="true"

Privacy Policy