Featured Posts


Wrapper Class Concept Simplified in Java

December 13, 2018

Traditional containers like vector can not handle primitive data types like int,float,long,char and double.Primitive data types can be converted to objects using the wrapper classes.Wrapper classes are contained in the java.lang package.

Simple type Wrapper class
boolean Boolean
char Character
double Double
float Float
int Integer
long Long
Wrapper classes have number of unique methods for handling primitive data types and objects.
Converting primitive numbers to object:

Constructor calling conversion action
Interger intval=new Integer(i); primitive integer i to Integer Object
Float floatval=new Float(f); primitive float f to Float Object
Double doubleval=new Double(d); primitive double d to Double Object
Long longval=new Long(l) primitive long l to Long Object
i,f,d,l are primitive data values denoting int,float,double,long data type. They may be constant or any variable.
Converting object numbers to primitive numbers using typeValue() method:

Method Action
int i=intVal.intValue() Object to primitive integer
float f=floatVal.floatValue() Object to primitive float
long l=longVal.longValue() Object to primitive long
double d=doubleVal.doubleValue() Object to primitive double
Converting numbers to String using toString() method:

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

Converting String objects to Numeric objects using static valueOf() method:

Method Action
intVal=Integer.valueOf(str); String to integer object
longval=Long.valueOf(str); String to long object
floatVal=Float.valueOf(Str); String to float object
doubleVal=Double.valueOf(str) String to double object
Converting Numeric Strings to primitive Numbers:

Method Action
int intVal=Integer.parseInt(str) String to primitive integer
long longval=Long.parseLong(str); String to primitive long
float floatVal=Float.parseFloat(str) String to primitive float
double doubleVal=Double.parseDouble(str) String to primitive double
All these methods may throw the NumberFormatException if the value of the String(str) does not represent proper number.
An Example:

public class InterestCalculate{

     public static void main(String []args){
         //converting Number to object
         Float principalAmount=new Float(0);
         Float interestRate=new Float(0);
         int numYears=0;
             DataInputStream in=new DataInputStream(System.in);
             System.out.println("Enter Principal Amount:");
             String principalString=in.readLine();
             //converting string to float object
             System.out.println("Enter rate of interest :");
             String interestString=in.readLine();
             //converting string to float object
             System.out.println("Enter number of years :");
             String yearString=in.readLine();
             //converting string to int
             catch(IOException e)
                System.out.println("I/O Error has occured");
             catch(NumberFormatException e)
                System.out.println("Number format is not correct");
             float value=total(principalAmount,interestRate,numYears);
             System.out.println("The final amount you will get"+value);
     static float total(float p,float r,int n)
         int year=1;
         float sum=p;
         return sum;
Wrapper Class Concept Simplified in Java Wrapper Class Concept Simplified in Java Reviewed by Animesh Chatterjee on December 13, 2018 Rating: 5

DataTypes Concept Simplified in Java

December 11, 2018

To create an object with new for a small simple variable is not efficient.As new operator places objects on the heap. For these types java,instead of creating the variable using new,an automatic variable is created which is not a handle.The variable holds the value itself. Variables are created on the stack instead of heap. Datatypes in java is divided into two major parts. Namely Primitive/intrinsic datatype and Non primitive/derived/reference type.Primitive datatype is divided into Numeric,non numerical datatype. Numerical data type is divided into Integral and floating point. Integral is further divided to Byte,Short,int,long. On the other hand floating point is divided into float and double. Non numerical type is divided into characters and boolean.

Non Primitive or derived or reference type is divided into classes,arrays,String and interfaces.

Integer Type:
Integer can hold whole numbers(exp-14,1234) etc.The size of the value depends on the type of the integer we choose. Storage also gets allocated accordingly.Wider data types requires more time for manipulation hence,we should use smaller data types whenever possible.This will improve the speed of execution(Example-instead of using int to store 10,we can use byte or Short).
we can also make integers long by appending the letter L or l at the end of the number.(Example- 124L or 567l).Java int types may not be used as boolean types and always signed.

Floating point Type: Floating point can hold containing fractional parts of a number.(Example-12.34,56.789 etc). Floating point types is divided into two parts:
  1. float-float type values are single precision numbers.
  2. double-double type values are double precision numbers.

default Floating point numbers are double precision numbers.We need to force them to single precision numbers by appending a f or F to the number. like-
1.23f or 456.789e2F

Double type:
Double precision types are used when we need greater precision in storage of floating point numbers. All mathematical functions-Sin,cos,sqart,etc return double type values.Floating point datatypes support a special values known as Not a Number(NaN).NaN represents the result of operation such as diving by zero,where an actual number is not produced.

Character type: To store character constant java supports character data type called char.

Primitive Datatypes in Java:

DataType Language Elements Wrapper Class Size Range Comments
Logical boolean Boolean 8 bits value true or false
Text Char Character 16 bits

Unicode-0 to Unicode-2 to the power 16-1 ASCII Characters use 16 bits

Literal value enclosed between ''
like 'a'
Integral byte Byte 1 byte
8 bits
To declare variables to store binary form of data
Integral short Short 2 bytes
16 bits
Integral int Integer 4 bytes
32 bits
Integral long Long 8 bytes
64 bits
Real double Double 8 bytes
64 bits
IEEE754 to IEEE1985
Real float Float 4 bytes
32 bits
IEEE754 to IEEE1985
Non Primitive Data types:

DataType Language Elements Wrapper Class Size Range Comments
Non Primitive String String String is a java class that is loaded with class coder . Typically 16 bit characters 
Non Primitive Void - - - -
Non Primitive Reference - - - stores the handle default value is null
Conversion between Primitive Datatypes:
Conversion between datatypes is called casting. So casting means explicitly telling java to force a conversion that the compiler would otherwise not carry out explicitly.Narrowing conversion is often referred as casting. However widening casting is also a type of casting. This is implicit and compiler will do that automatically. For narrowing conversion or down casting we need to provide the desired type is placed inside a bracket "()".
Case-1:When java run time explicitly changes the type without users interaction
Case-2:All conversion of primitive data types is checked at compile time in order to establish whether or not the conversion is permissible.

int i=10;
double d=i;
In this case an int type is converted to double type.It is an automatic conversion.

double d=10.0;//compiler will throw an error.
double d=(int)10.0;//conversion is done with loss of information
int i=d;
It is not directly permissible.The compiler will throw an error that there could be possible loss of precision. To forcefully do the conversion we need to cast it to int. The cast (int) forces d to be an int.

public void someMethod(double myValue)
In this method is expecting a value of type double to be passed to it,when it is invoked.If we pass a float value or other data type(Integral or real), as double being top of the primitive data type hierarchy,the value will be converted to double.
Boolean values can not be converted to any other type.
A non boolean type can be converted to another non-boolean type provided that the conversion is widening conversion.
A non boolean type can not be converted to another non-boolean type if the conversion is narrowing conversion.
DataTypes Concept Simplified in Java DataTypes Concept Simplified in Java Reviewed by Animesh Chatterjee on December 11, 2018 Rating: 5

All About Class Image in AWT in Java

December 10, 2018

The abstract class Image is the superclass of all classes that represent graphical images. The structure of the class is given as below:

public abstract class java.awt.Image extends java.lang.Object{
//member element:
public final static Object UndefinedProperty;
//The UndefinedProperty Object should be returned whenever a property which was defined for a particular
//image is fetched.
public Image()//default constructor
public abstract void flush();
public abstract Graphics getGraphics();
public abstract int getHeight(ImageObserver observer);
public abstract Object getProperty(String name,ImageObserver observer);
public abstract ImageProducer getSource();
public abstract int getWidth(ImageObserver observer);
All About Class Image in AWT in Java All About Class Image in AWT in Java Reviewed by Animesh Chatterjee on December 10, 2018 Rating: 5

All About Class NotSerializableException in Java

December 10, 2018

An application that throws NotSerializableException indicates that the target object , that it tried to serialize,could not be serialized.This can happen to classes which does not implement Serializable interface. A class may implement Serializable interface but may refer or contain a non serializable object,in this case when we try to serialize the class,we will get NotSerializableException.

A subclass of a serializable class can prevent itself from being serialized by throwing this (NotSerializableException) exception from its writeObject() or readObject() methods.

It is a child class of ObjectSreamException.
The structure of the NotSerializableException class given as :

public class java.io.NotSerializableException extends java.io.ObjectStreamException{
public NotSerializableException();//default constructor
public NotSerializableException(String className);
All About Class NotSerializableException in Java All About Class NotSerializableException in Java Reviewed by Animesh Chatterjee on December 10, 2018 Rating: 5

All About Class NoRouteToHostException in Java

December 10, 2018

An application can throw this -NoRouteToHostException exception to indicate a socket could not be connected with remote host, as the host could not be connected due to one of the following reasons:

  • Host could not be connected before establishing socket
  • Some intermediate link/links between local machine to host is/are down.
  • Host may be behind a firewall.
This excaption-NoRouteToHostException is a subclass of SocketException.
The structure of NoRouteToHostException is given as:

public class java.net.NoRouteToHostException extends java.io.SocketException{
public NoRouteToHostException();//default constrcutor
public NoRouteToHostException(String message);
All About Class NoRouteToHostException in Java All About Class NoRouteToHostException in Java Reviewed by Animesh Chatterjee on December 10, 2018 Rating: 5
Powered by Blogger.