Java – Introduction

Java – Introduction 2017-06-03T13:58:09+00:00

Introduction to wrapper class in java:

Wrapper Class :

  • Java uses primitive types, such as int, char, double to hold the basic data types supported by the language.
  • Sometimes it is required to create an object representation of these primitive types.
  • These are collection classes that deal only with such objects. One needs to wrap the primitive type in a class.
  • To satisfy this need, java provides classes that correspond to each of the primitive types. Basically, these classes encapsulate, or wrap, the primitive types within a class.
  • Thus, they are commonly referred to as type wrapper. Type wrapper are classes that encapsulate a primitive type within an object.
  • The wrapper types are Byte, Short, Integer, Long, Character, Boolean, Double, Float.

These classes offer a wide array of methods that allow to fully integrate the primitive types into Java’s object hierarchy.

 

Wrapper classes for converting simple types

Simple Type

Wrapper class

boolean Boolean
char Character
double Double
float Float
int Integer
long Long

Converting primitive numbers to Object numbers using constructor methods

Constructor calling

Conversion Action

Integer IntVal = new Integer(i); Primitive integer to Integer object
Float FloatVal = new Float(f); Primitive float to Float object
Double DoubleVal = new Double(d); Primitive double to Double object
Long LongVal = new Long(l); Primitive long to Long object

 

NOTE : I, f, d and l are primitive data values denoting int, float, double and long data types. They may be constants or variables.

 

Converting Object numbers to Primitive numbers using typeValue() method

Method calling

Conversion Action

int i = IntVal.intValue(); Object to primitive integer
float f = FloatVal.floatValue(); Object to primitive float
double d = DoubleVal.doubleValue(); Object to primitive double
long l = LongVal.longValue(); Object to primitive long

Converting Numbers to Strings using toString() method

Method calling

Conversion Action

str = Integer.toString(i); Primitive integer i to String str
str = Float.toString(f); Primitive float f  to String str
str = Double.toString(d); Primitive double d to String str
str = Long.toString(l); Primitive long l to String str

Converting String Object in to Numeric Object using static method ValueOf()

Method calling

Conversion Action

IntVal = Integer.ValueOf(str); Convert String into Integer object
FloatVal = Float.ValueOf(str); Convert String into Float object
DoubleVal = Double.ValueOf(str); Convert String into Double object
LongVal = Long.ValueOf(str); Convert String into Long object

Converting Numeric Strings to Primitive numbers using Parsing method

Method calling

Conversion Action

int i = Integer.parseInt(str); Converts String str into primitive integer i
long l = Long.parseLong(str); Converts String str into primitive long l

NOTE : parseInt() and parseLong() methods throw a NumberFormatException if the value of the str does not represent an integer.

Prev Next