Tuesday 28 August 2018

Defining Integer as Octal


Octal Number System :
  • Octal number system is the base 8 number system.
  • Legal digits in an octal literal can be 0 to 7 only.
  • If you try to write int a = 08 or a = 09 in Java, It will give compile time error.
  • Octal literal starts with leading zero, for example 010,023,011 etc.

Below is the Java program If we write int = 010, then we will be the output.

public class OctalNumberClass{

  public static void main(String []args){
     int abc = 010;
     System.out.println("Octal Values :"+abc);
  }
}

Output : Octal Values :8

Wednesday 22 August 2018

final, finally and finalize in Java

final :
  • final is a modifier applicable for classes, methods and variables.If a class is declared as final then we can't extend that class means we can not create child class for that class.
  • If a method is declared as final then we can't override that method in the child class.
  • If a variable is declared as final then it will become constant and we can't perform re-assignment for that variable.
finally : 
  • finally is a block always associated with try catch to maintain clean up code.
  • Code in the finally code will always be executed even if an exception is thrown.
  • finally block can be used instead of catch block.
  • Sample Code : 
       try
       {
            //risky code
       }
       catch(X e)
       {
            //Handling code
       }
       finally
      {
            //clean up code 
      }

finalize : 
  • finalize() is a method which is present in the object class.
  • Garbage Collector calls the finalize() method just before destroying the object to check which objects are eligible to garbage collect.
  • It is used to clean or release the resources i.e.closing database connection.
Important Point to remember :
      finally is meant for clean up activities related to try catch block and finalize() is meant for clean up activities related to object.

Tuesday 21 August 2018

How to create an immutable class in Java

To create an immutable class, you should consider below points:
  • Declare the class as final so that no one will extend the class.
  • Declare all the variables in the class as final so that no one can change the value of the variables.
  • Declare all variables as private so that others can not access directly.
  • Do not write setter methods so that others can not modify the actual value of the variables.
  • Write the parameterized constructor.
Example of Immutable Class :

public final class ABC {

    private final int id;
    private final String name;
    private final String address;

    public ABC(int id, String name,String address) {
        this.name = name;
        this.id = id;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }
}

Use of Immutable Class :

class Test
{
    public static void main(String args[])
    {
        ABC abc = new ABC(1,"PQRS", "Pune");
        System.out.println(abc.id);
        System.out.println(abc.name);
        System.out.println(abc.address);
    }
}


Monday 6 August 2018

Difference bet Constructor and Method in Java

Constructor Method
Constructor must have the same name as the class
name.
Method name can be same or can not be same
as the class name.
Constructor is used to initialize the variables. Method is used to show the behaviour of the
object and we can write business logic.
Constructor does not return any type. Method must return any type otherwise
It will be consider as constructor.
Constructor is called when we create the object.
Ex. A a = new A();
We need to called the method explicitly when
we require.
Ex. A a = new A();
a.showMessage();
If we don’t write constructor in Java class, then
Java compiler consider the default constructor.
In case of method, Java compiler don’t act like
constructor.