Wednesday, 20 August 2014

Enum in Java



Enum concept introduced in JDK 1.5 version.

Java enum more powerful than other language.

The main purpose of enum is if we want to developed or represent group of constant then we should go for enum.


e.g

      enum Friend
{
       kunal,sachin,jagdish,atish,vaibhav,vishal;
        
}



Internally enum represent as liked this Class concepts


  Every enum by default public static final
  Every constant represent as object of type enum

 
class Friend
{
Public static final Friend sachin=new Friend();
Public static final Friend atish=new Friend();
.
.
.
}


Specialty of java enum:


In old language we represent only constant but in java enum we declare variable,constructor and method also and enum can have main method also we run enum from cmd also

i.e java enum is more powerful than other languages;
//simple.java

enum month                                                                            
{
                jan,feb,march,may;

public static void main(String[] args)
                {
                                System.out.println("Hello World!");
                  month n=month.jan;
       System.out.println(n);
                 
                }
}
o/p:

 

 

Some valid combination java enum:


  • ·         It can be inside the class.
  • ·         class can be inside  enum.
  •      But inside method does not allowed

     
enum is direct child class of java.lang.Enum so we cannot extend any other enum

so that it cannot extend any another enum.

it can implement any number of interface.


One of the most important interview question I faced


                                                                              

Difference between enum Enum and Enumeration..??


enum:-it is used for creating java enum it is keyword

Enum:it is class represent in java.lang.Enum which act as based class for all java enum

Enumaration-it is cursor it used for retrieving element from collection one by one.
 

// EnumDemoExample.java
enum Friend
{
       kunal,sachin,jagdish,atish,vaibhav,vishal;
     int x=10;
       Friend()
  {
         System.out.println("inside the constructor");
  }

}
public class EnumDemoExample {

       public static void main(String[] args) {
              // TODO Auto-generated method stub

             
             
              Friend f=Friend.kunal;
        System.out.println("hellow friend");
        System.out.println(f.x);
       Friend f1[]=Friend.values();
       for(Friend a:f1)
       {
              System.out.println(a+"---"+a.ordinal());
       }
       }

}

O/P-
inside the constructor
inside the constructor
inside the constructor
inside the constructor
inside the constructor
inside the constructor
hellow friend
10
kunal---0
sachin---1
jagdish---2
atish---3
vaibhav---4
vishal---5

 



Tuesday, 5 August 2014

NAN(Not A NUMBER) and Infinity Concept in Double and Float Classes

NAN -->


NAN is applicable for only Double and Float classes

In integral Arithmetic there is no way to represent undefined result
but in floating point arithmetic there is way to represent such kind of result.

//Simple example to demonstrate above concept


public class JavaApplication4 {

   public static void main(String[] args) {
        // TODO code application logic here
    double a=0.0;
    double b=0.0;
        System.out.println("Result="+a/b);
        }
}

O/p:

Result=NaN

Infinity-

  Infinity is applicable for only Double and Float classes

In integral Arithmetic there is no way to represent divide by zero condition
but in floating point arithmetic there is way to represent such kind of result
.
.
//Simple example to demonstrate above concept

public class JavaApplication4 {

   public static void main(String[] args) {
        // TODO code application logic here
    double a=10.0;
    double b=0.0;
        System.out.println("Result="+a/b);
        }
}

 O/p:
Result=Infinity



You can see the static final variable available in the class by using following way


C:\Users\Kunal>javap java.lang.Float
Compiled from "Float.java"
public final class java.lang.Float extends java.lang.Number implements java.lang
.Comparable<java.lang.Float> {
  public static final float POSITIVE_INFINITY;
  public static final float NEGATIVE_INFINITY;
  public static final flaoat nan;
 


Difference Between Bitwise operator and Short-circuit operator

Bit-wise operator :-


  •  In Bit wise operator both argument   
     are evaluated. 

  •  It work with both integral ans Boolean.                                 

  •  Relatively performance is slow.


Short-circuit operator :-


  • In Short-circuit operator 2nd  argument  evaluation is optional

  • It work with only Boolean type only.   

  • Relatively performance is fast


note:

&-operator ---> if both argument are true then only result is true otherwise false
 

|  -operator   ---> if at least one argument is false then result is false otherwise true

In Short-circuit operator

T && T  --

In this case the 1st argument is true then only 2nd argument is evaluated
           if 1st argument is false then 2nd argument is not evaluated.

Saturday, 2 August 2014

Rownum and Row-id Feature of Oracle Database



Rownum:

  • Rownum is pseudo column and behave like table column.
  •  It having temporary value.
  •  It assign number  to each row in table at time of selection.
rownum has value < ,<= value it can't worked with > value

Row-id:


when we are inserting data into table Oracle server automatically generates an unique 
identification number for identifying record uniquely.



The Main Difference between Rownum and Row-id is
Rownum is temporary values where Row-id having fixed valued it cannot changed.

Some Example:


SQL> select * from t1;

       SNO NAME       ADDRESS
---------- ---------- ----------
         1 kunal      nagar
        21 sachin     parbhani
        11 vabhaiv    satara
        44 jags       daund

SQL> select * from t1 minus (select * from t1 where rownum <=(select count(*)-1 from t1));

       SNO NAME       ADDRESS
---------- ---------- ----------
        44 jags       daund

SQL> select * from t1 minus (select * from t1 where rownum <=(select count(*)-2 from t1));

       SNO NAME       ADDRESS
---------- ---------- ----------
        11 vabhaiv    satara
        44 jags       daund


SQL> select rowid,rownum,sno,name from t1;

ROWID                  ROWNUM        SNO NAME
------------------ ---------- ---------- ----------
AAARC0AAEAAAAA/AAA          1          1 kunal
AAARC0AAEAAAAA/AAB          2         21 sachin
AAARC0AAEAAAAA/AAC          3         11 vabhaiv
AAARC0AAEAAAAA/AAD          4         44 jags

SQL>



Tuesday, 22 July 2014

Difference between length and length()...????

ans:-

length:-

  • -It is final variable applicable for arrays .
  • -It is used to calculate length of array.


length():-

  • -It is final method of String class.
  • -It is applicable for String.
  • -It is used to calculate number of char present in String.
  • -It's return type is integer.

Monday, 14 July 2014

Is there is any way to stop execution of finally block

yes ,we can stop execution of finally block but condition is that control must entered into the finally block
finally block is executed only when control entered into try block so we need write code in try block to stop execution of finally block

//Simple example to Demonstrate above concept without stoping execution of finally block:


public class MainAppp {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
     try
     {
    System.out.println("IN THE TRY BLOCK");
     }
     finally
     {
    System.out.println("IN THE FINALLY BLOCK");
     }

}

}

O/P
IN THE TRY BLOCK
IN THE FINALLY BLOCK

//Simple Example To Stop Finally block Execution




public class MainAppp {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
     try
     {
    System.out.println("IN THE TRY BLOCK");
     System.exit(0);
     }
     finally
     {
    System.out.println("IN THE FINALLY BLOCK");
     }
}

}

O/P:
IN THE TRY BLOCK

Explanation
System.exit(0):
0--- indicate status code  it means normal termination
nonzero--indicate abnormal termination.


Monday, 16 June 2014

How to print without main method

There is example that demonstrate above concept


public class Mainapp {

/**
* @param args
*/

static
{
System.out.println("hi...");
System.exit(0);
}

}


O/P:

hi...

this is one of the important question ask in interviews...
plz go through it..