Saturday, 5 May 2018


//jdk 1.7 version Concept:----------------------
//Underscores Between Digits in Numeric Literals

package govind;

public class UnderScorePass {
                public static void main(String[] args) {
                                int carloan=10_00_00_00;
                                int homeloan=10_0000_00_00;
                                System.out.println("Car Loan="+carloan);
                                System.out.println("Home Loan="+homeloan);
                                //String s1="12_12";
                                //int x=Integer.parseInt(s1);//error
                }

}


o/p:------------

Car Loan=10000000
Home Loan=1000000000


Underscores Between Digits in Numeric LiteralsUnderscores are permitted in numeric literals.
 You can place underscores where you feel required to increase readability
 like between hundreds and thousands, thousands and lakhs etc.

This is used to group numbers in a bigger literal value (especially of long data type).
Note: Do not place underscore at the beginning or ending of the literal value.


Note: But following does not work (cannot parse with underscores).

//String s1="12_12";
//int x=Integer.parseInt(s1);//error

The above parsing raises NumberFormatException.

Friday, 4 May 2018


JDK 1.7 Features:----
Handling multiple exceptions in a single catch block


package govind;

public class MultipleExceptionHandleThroughSingleCatch {
           
            public static void main(String[] args) {
                       
                       
                        int x=0;
                        int y[]= {100,200,300,400,500};
                        try
                        {
                                    int c=y[5]/x;    //line 1
                                    //int c=y[4]/x;  //line 2
                        }
                        catch (ArithmeticException|ArrayIndexOutOfBoundsException e) {
                       
                        e.printStackTrace();
                                   
                        }
           
            }

}


o/p:-----------

case :-1

if line 1 is not commented and line 2 is commented then o/p:-

java.lang.ArrayIndexOutOfBoundsException: 5
            at govind.MultipleExceptionHandleThroughSingleCatch.main(MultipleExceptionHandleThroughSingleCatch.java:12)

case :-2
if line 2 is not commented and line 1 is commented then o/p:-
java.lang.ArithmeticException: / by zero
     at govind.MultipleExceptionHandleThroughSingleCatch.main(MultipleExceptionHandleThroughSingleCatch.java:13)


==========================================================================


package govind;

public class MultipleExceptionHandleThroughSingleCatchError {
           
            public static void main(String[] args) {
                       
                        try
                        {
                                    int c=10/0;
                        }
                        catch(ArithmeticException|RuntimeException e)
                        {
                        e.printStackTrace();
                                   
                        }
                       
            }

}


o/p:----------------------
compile time error:------
The exception ArithmeticException is already caught by the alternative RuntimeException

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
            The exception ArithmeticException is already caught by the alternative RuntimeException
                    at govind.MultipleExceptionHandleThroughSingleCatchError.main(MultipleExceptionHandleThroughSingleCatchError.java:11)

Note:--------------
Super class exception must be caught separately (it is a constraint).

So here, in case when your are catching multiple exceptions,
follow the rule of generalized to more specialized. It means that, if you are using

super (general) class, don't use child (specialized) class.


JDK 1.7 Features:----
String in Switch Expression:--------------

package govind;

public class StringPassInSwitch {

public static void main(String[] args) {
String version="j2se";
switch(version)
{
    case "j2se":
    System.out.println("java 2 standard edition:-using j2se you can develop desktop application");
break;
case "j2ee":
System.out.println("java 2 enterprise edition:-using j2ee you can develop web application");
break;
case "j2me":
System.out.println("java 2 micro edition:-The basic aim of this edition was to work on mobiles, wireless devices, set top boxes etc");
break;
default:
System.out.println("java is plateform independent language");

}
}
}


o/p:--------------------
java 2 standard edition:-using j2se you can develop desktop application

Wednesday, 2 May 2018


Steps to creation of Immutable class:-

To create immutable class in java, you have to do following steps.
1.      Declare the class as final so it can’t be extended.
2.      Make all fields private so that direct access is not allowed.
3.      Don’t provide setter methods for variables
4.      Make all mutable fields final so that it’s value can be assigned only once.
5.      Initialize all the fields via a constructor performing deep copy.
6.      Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.
=======================================================================



How to make our class is immutable class?
//ImmutableClassExample.java
// ImmuatableMain.java

//ImmutableClassExample.java

package govind;

import java.util.Date;

public final class ImmutableClassExample {
               
                private final Integer immutableField1;
                private final String immutableField2;
                private final Date mutableField1;
               
                public ImmutableClassExample(Integer immutableField1, String immutableField2,Date date) {
                                super();
                                this.immutableField1 = immutableField1;
                                this.immutableField2 = immutableField2;
                                this.mutableField1=new Date(date.getTime());//(line----1)always create new   //instance for mutable objects
                    //this.mutableField1=date;//  (line----2)
                }
                public Integer getImmutableField1() {
                                return immutableField1;
                }
                public String getImmutableField2() {
                                return immutableField2;
                }
                public Date getMutableField1() {
                                return new Date( mutableField1.getTime());//(line----3)always return new instance //for mutable objects
               
                 //return mutableField1;//(line----4)
                }
}







// ImmuatableMain.java
package govind;

import java.util.Date;

public class ImmuatableMain {
           
            public static void main(String[] args) {
                       
                        Date dt=new Date();
                        ImmutableClassExample ice=new ImmutableClassExample(10, "govind", dt);
                        System.out.println(ice.getImmutableField1()+" "+ice.getImmutableField2()+" "+ice.getMutableField1());
                Date d = ice.getMutableField1();
                System.out.println("Change the date and print date");
        d.setDate(2-06-2011);
        System.out.println(d);
                        System.out.println("plz After the changing the date value  check date value  is changed or not ");
                        System.out.println(ice.getImmutableField1()+" "+ice.getImmutableField2()+" "+ice.getMutableField1());
            }

}



o/p:- view:----------------------

case:1:- (if line-1 and line 3 is not commented:----------and line 2 and line 4 is commented ) then o/p:-------

10 govind Thu May 03 02:10:33 IST 2018
Change the date and print date
Tue Oct 23 02:10:33 IST 2012
plz After the changing the date value check date value it is changed or not
10 govind Thu May 03 02:10:33 IST 2018

Case:-2(if line-2 and line 4 is not commented:-------and line-1 and line-3 is commented) then o/p:---

10 govind Thu May 03 02:23:44 IST 2018
Change the date and print date
Tue Oct 23 02:23:44 IST 2012
plz After the changing the date value  check date value  is changed or not
10 govind Tue Oct 23 02:23:44 IST 2012


Description: --------------
Immutable Class In java

1.      Make java class as final
2.      Make all instance variable declared as final
3.      No setter method for instance variable.
4.      Integer class is immutable class. Therefore we provide only getters.
            It does not have setters method
5.      String class is immutable class. Therefore we provide only getters. It does not have setters method
6.      Problem comes when instance variable is mutable class. we have to take special care let’s take a Date class as instance variable Date is a Mutable class
7.      this.mutableField1=date;//don’t do this  Date class is mutable class, performing above step can make our class change its state through date reference variable here Date class has setters to change its state.
8.      return mutableField1 don’t do this as discussed above it will return a Date to caller method where client can change its state.



Tuesday, 1 May 2018




Static control flow parent to child relationship---------------.

Whenever we are executing java class the following sequence of Steps will be executed as the part of static control flow

1. Identification of static members from parent  to child.
2. Execution of static  variable assignments and static blocks from parent to child.
3. Execution of only child class main method.





package staticcontrolflowpc;

public class Base {
           
            static int i=10;
            static
            {
                        m1();
                        System.out.println("Base Static Block");
            }
           
            public static void main(String[] args) {
                        m1();
                        System.out.println("Base Main");
            }
           
            public static void m1()
            {
                        System.out.println(j);
            }
           
            static int j=20;

}


package staticcontrolflowpc;

public class Derived  extends Base
{
           
            static int x=100;
            static
            {
                        m2();
                        System.out.println("Derived First Static Block");
            }
           
            public static void main(String[] args) {
                        m2();
                        System.out.println("Derived Main");
            }
           
            public static void m2()
            {
                        System.out.println(y);
            }
           
            static
            {
                        System.out.println("Derived Second Static Block");
            }
           
            static int y=200;

           
}



o/p:----------------------------------


0
Base Static Block
0
Derived First Static Block
Derived Second Static Block
200
Derived Main