Classes in Java

Object     Float

 Class-" Double" in Java 

Double is the class present in the package java.lang.*;

Double class is defined as final so that it cannot be extended further.

Double class must be instantiated with the single argument of its type (ie. double value must be passed as an argument to the constructor)

Double class has two types of constructor
 as Double(double val)
     Double(String str)

Double class extends the class Number(Which has methods for numeric values such as floatValue(), shortValue(),etc... )

There are several methods inbuilt for processing values which are of double type. Those methods are accessed using an object for the Double class.

Some of the methods are

isFinite()----     Returns true if the double is
        infinitely long and false otherwise.

isNaN() ------    Returns true if the double value is not a number and false otherwise

parseDouble()-- Returns the double value of the string which is given
as argument i.e. parseDouble(String s)

There are several other methods defined in Double class which can be illustrated by the following java program

Program to understand the methods defined in Double class
import java.lang.*;
public class DuubleExample {
 public static void main(String ar[]){
 Double d=new Double(2.59329127653013); /* Constructor must contain a double value as single argument */ 
 Double d2 = new Double("1234"); /*Another type of Constructor that is passed with String, which can be casted as floating point double if it is numeric, otherwise exception will be raised */
 double dubb1=25.20;
 double dubb2=29.00;
 System.out.println("Value1:\t"+dubb1);
 System.out.println("Value2:\t"+dubb2);
 System.out.println("compare():" + d.compare(dubb1,dubb2));  /*Compares the 2 arguments and returns greater than 0, if arg1 > arg2 0, less than 0 if arg1 < arg2 */ 
 int k = d.compareTo(2.45); /* Compares the given argument with the constructor's argument and returns the values as similar as compare() method */
 System.out.println("compareTo():"+k);
 float val= d.floatValue(; /* Returns the float value of the double object i.e. arg passed through constructor*/
 System.out.println("float value:\t"+val);
 int s= d.hashCode(); /* Returns the hashcode of passed object (constructor arg)*/
 System.out.println("Hashcode:\t"+s);
 String str;
 str=d.toString();  /* To convert the passed double object to string */
 System.out.println("String:\t\t"+str);
 str= d.toString(3.0525); /* To represent the double as string */
 System.out.println("toString:\t"+str);
 dubb2=d.valueOf(str); /* To return the double value of the string */
 System.out.println("valueOf:\t"+ (dubb2+0.0075)+"\n");  /* The value is converted as double and hence sop will return added value i.e. 3.06 */
 double du=d.parseDouble(str);
 System.out.println(du+1.00);
 System.out.println(d2); /* To print the string that is passed in the constructor is converted as floating point double */
 }
}


Class- "Float" in Java

  • Float is another class defined in java.lang package.
  • Float is defined as final class which means that it cannot be extended further.
  • Float class wraps a primitive type float in an object.
  • The constructor for Float must possess at least one argument. 
  • Float class has 3 type of constructors which are as follows
  1. Float(float val)--> wraps the primitive float in an Float object
  2. Float(double val)--> wraps the double value into an Float object
  3. Float(String str)-->wraps the string that representing a float value in an Float object. If the string is not a numeric value then java.lang.NumberFormatException is raised.


  • Float class has several methods that deals with Float values easier.

          --> compare(float valone,float valtwo)
                    This compare method compares the two float values and returns the integer which represents
                    0    - if equals
                    1    - if valone > valtwo
                   -1    - if valone < valtwo
                   This is a static method and hence should be called as Float.compare(valone,valtwo);

          --> compareTo(float anotherfloatvalue)
                    This compareTo method behave same as compare() except that it compares the Float
    object with anotherfloat value.

          --> compareTo(Object o)
                    This compareTo method behave same as compareTo(). The difference here is that
                     it can compare two objects.(this comes to use when primitive values to be wrapped as objects).
          --> doubleValue()
                    This method returns the double value of the given object.
          --> floatValue()
                    This returns the float value of this Float object.
          --> valueOf(String str)
                    This returns the float value of the given string. This method is static. 
          --> parseFloat(String str)
                    This returns the float value of the string representation. It is similar to the valueOf
                    method. This method is also static.
          --> shortValue()
                    This Returns the value of this Float as a short.
 
     Similar to Double class, the Float class also has several other methods such as toString(), longValue(), intValue() etc.

Program to understand Float class

public class DemoFloat
{
    public static void main(String... arg)
    {
        Float ff=new Float("1.05");
        Float fa=new Float(3.00f);

        Double d=new Double(3.137138998);
        //System.out.println(ff);       
        int val=Float.compare(2.009f,2.01f);
        //System.out.println(val);
        val=ff.compareTo(2.009f);
        //System.out.println(val);
        val=ff.compareTo(fa);
        //System.out.println(val);
        float fval=d.floatValue();
        //System.out.println(fval);
        fval=Float.valueOf("123");
        //System.out.println(fval);
        fval=Float.parseFloat("1234");
        //System.out.println(fval);
        Double valOne=3.204;
        short fvalue=valOne.shortValue();
        System.out.println(fvalue);
    }
}


Class- "Object" in Java

      The class object is the root of the class hierarchy.  In Java, every class has object class as its super class. The object class consists of several methods such as finalize(), clone(), wait(), notify, etc.

We will see those methods one by one here:

protected Object clone()
     Here the return type is Object. This method returns the exact copy of the given object. The cloned object(instance) will contain the fields same as the given object. The state of that object also
copied in the new object.
     clone() method is protected which means that,only the same class can clone the object. This is because the changes made in cloned object also reflected in original content. Hence its dangerous using
clone() method.
     Cloning the object is possible only if that class implements Cloneable interface, Otherwise it will raise CloneNotSupportedException.

boolean equals(Object o)
     The equals() method compares the two objects and returns whether true or false.

Class getClass()
     This method will be useful to know the class name to which the specified object belongs. This method returns the type Class.

int hashCode()
     As we know,every class must provide a hashCode() method which digests the data stored in an instance of the class into a single hash value (a 32-bit signed integer). Here in Object class, this method will return the hash code of the specified object which will be of integer type.

void wait()
  wait method is used to make a thread to wait until it is notified(using notify method or notify all method) to continue.

final void wait(long timeout)
     This wait method is used to make a thread to wait until get notified or until a specified amount of time elapsed.

final void wait(long timeout,int nanos)
     This wait method is used to make a thread to wait until get notified or until a specified amount of time elapsed or until it gets interrupted by another thread.

final void notify()
     This method Wakes up a single thread that is made to wait by the object monitor. The choice of waking up the thread is arbitrary.

final void notifyAll()
     This method is useful for waking up all the threads that are in are made to wait by the object's monitor.

Program to understand some of the methods in Object class 

public class ObjectDemo implements Cloneable        //To make use of the clone method the class
{                                                                                   //must implement Cloneable interface

    public static void main(String arg[]) throws CloneNotSupportedException
    {
        try
        {
          String str=new String(" ");
          ObjectDemo obj=new ObjectDemo();
          ObjectDemo obj1=new ObjectDemo();
          ObjectDemo obj3=obj;                                      //obj and obj3 are exactly same now
          System.out.println(obj1);                                    //returns the address of the obj1
          System.out.println(obj);                                      //returns the address of the obj
          System.out.println(obj.hashCode());                 //returns the hash code of the object
          System.out.println(str.getClass());                    //prints the name of class to which this object belongs
          ObjectDemo obj2=(ObjectDemo)obj.clone(); //returns exact copy of the object of type ObjectDemo
          System.out.println(obj2);
          System.out.println(obj1.equals(obj));                 //reference of objects differs and hence false
          System.out.println(obj3.equals(obj));                 //reference same hence returns true
          str=obj.toString();                                               //returns the string representation of object
          System.out.println(str.concat("++"));
        }
        catch(CloneNotSupportedException e)                 /* To catch the exception raised due to copying*/
        {                                                                          /* of object that does not implement Cloneable*/
          System.out.println(e);
        }
    }
}