Selasa, 22 Maret 2016

Basic operators in java

Types of Operators in Java:

  1. Arithmetic Operators
  2. Bit-wise Operators
  3. Logical Operators
  4. Relational Operators 
  5. Assignment Operators
  6. Misc Operators



Java operator precedence

java order of operations

Arithmetic Operators
  • Arithmetic operators are used in mathematical expressions.
  • Following are the arithmetic operators in java.
  1. +  :  Addition
  2. -   : Subtraction
  3. *  : Multiplication
  4. /   : Division
  5. % : Modulus

Bit wise Operators:

  • Bit wise operators operates on individual bits of operands.
  • Following are the Bit-wise operators in java. 
  • Bitwise operators in java

  1. ~      : Bit wise unary not
  2. &     : Bit wise And
  3. |       : Bit wise OR
  4. ^      : Bit wise Exclusive OR
  5. >>    : Shift Right
  6. >>>  : Shift right zero fill
  7. <<    :Shift left

Logical Operators:

  1. && : Logical AND
  2. ||      : Logical OR
  3. !      : Logical NOT

Relational Operators in java

  1. ==  : Equal to
  2. !=   : Not Equal to
  3. <    : Less Than
  4. >    : Greater Than
  5. >=  : Greater Than or Equal to
  6. <= : Less Than or Equal to

Assignment Operators:

  1. =    : Simple Assignment
  2. +=  : Add and assign
  3. -=   : Subtract and assign
  4. *=   : Multiply and assign
  5. %=  : Modulus and assign
  6. /=    : Divide and assign
  7. &=   : Bit wise AND assignment
  8. |=     : Bit wise assignment OR
  9. ^=    : Bit wise Exclusive OR assignment
  10. >>= : Shift Right assignment
  11. >>>= : Shift right  zero fill assignment
  12. <<=   : Shift left assignment


Misc Operators:

Increment and Decrement operators:
  1. ++ : increment
  2. --   : decrement


Conditional Operator: (?:)

  • Also known as ternary operator
  • Example: int a= 1>2?1:2;

Ternary operator in java


Instanceof Operator
  • Instance of operator is used to test whether that object is belong to that class type or not.
  • If that object belongs to that class it returns true .otherwise it returns false.
  • Instance of operator is also known as comparison operator.because it compares with the instance of type.
Instanceof operator in java
Baca selengkapnya

Senin, 21 Maret 2016

Top 30 frequently asked java interview programs for freshers

Top 30 frequently asked java interview programs for freshers

Baca selengkapnya

Java Example program convert Decimal to Binary


    • We can convert binary to decimal in three ways
      1.Using Integer.toBinaryString(int num);
      2.Using Stack
      3.Using Custom logic 



    1.Write a Java Program to convert decimal to binary in java using Integer.toBinaryString(int num)

    1. import java.util.Scanner;
    2. public class ConvertDecimalToBinary{
    3.  
    4.     /**
    5.      *www.instanceofjava.com
    6.      */
    7.  
    8.  public static void main(String[] args) {
    9.         
    10.  System.out.println("\nBinary representation of 1: ");
    11.  System.out.println(Integer.toBinaryString(1));
    12.  System.out.println("\nBinary representation of 4: ");
    13.  System.out.println(Integer.toBinaryString(4));
    14.  System.out.println("Binary representation of 10: ");
    15.  System.out.println(Integer.toBinaryString(10));
    16.  System.out.println("\nBinary representation of 12: ");
    17.  System.out.println(Integer.toBinaryString(12));
    18.  System.out.println("\nBinary representation of 120: ");
    19.  System.out.println(Integer.toBinaryString(120));
    20.  System.out.println("\nBinary representation of 500: ");
    21.  System.out.println(Integer.toBinaryString(500));

    22. }
    23.  
    24. }





    Output:

    1. Binary representation of 1: 
    2. 1
    3. Binary representation of 4: 
    4. 100
    5. Binary representation of 10: 
    6. 1010
    7. Binary representation of 12: 
    8. 1100
    9. Binary representation of 120: 
    10. 1111000
    11. Binary representation of 500: 
    12. 111110100

    2.Write a Java Program to convert decimal to binary in java


    1. import java.util.Scanner;
    2. import java.util.Stack;
    3. public class ConvertDecimalToBinary{
    4.  
    5.     /**
    6.      *www.instanceofjava.com
    7.      */
    8.  
    9.  
    10.  public static void main(String[] args) {
    11.         
    12. Scanner in = new Scanner(System.in);
    13.          
    14. // Create Stack object
    15. Stack<Integer> stack = new Stack<Integer>();
    16.      
    17. //Take  User input from keyboard
    18.  System.out.println("Enter decimal number: ");
    19.  int num = in.nextInt();
    20.     
    21. while (num != 0)
    22. {
    23.    int d = num % 2;
    24.    stack.push(d);
    25.    num /= 2;
    26.  
    27.  } 
    28.      
    29.  System.out.print("\nBinary representation is:");
    30.  
    31. while (!(stack.isEmpty() ))
    32.  {
    33.    System.out.print(stack.pop());
    34.  }
    35.         
    36.  
    37. System.out.println();
    38.  
    39. }
    40.  
    41. }
    42.  
    43. }


    Output:

    1. Enter decimal number: 
    2. 12
    3.  
    4. Binary representation is:1100

    java program convert decimal to binary


    3.Write a Java Program to convert decimal to binary in java


    1. import java.util.Scanner;
    2. public class ConvertDecimalToBinary{
    3.  
    4.     /**
    5.      *www.instanceofjava.com
    6.      */
    7.  
    8. public static void convertDeciamlToBinary(int num){
    9.  
    10.   int binary[] = new int[40];
    11.          int index = 0;
    12.  while(num > 0){
    13.            binary[index++] = num%2;
    14.            num = num/2;
    15.  }

    16. for(int i = index-1;i >= 0;i--){
    17.            System.out.print(binary[i]);
    18. }
    19.  
    20. }
    21.  
    22.  public static void main(String[] args) {
    23.         
    24. System.out.println("Binary representation of 1: ");
    25. convertDeciamlToBinary(1);
    26.  
    27. System.out.println("\nBinary representation of 4: ");
    28. convertDeciamlToBinary(4);
    29.  
    30. System.out.println("\nBinary representation of 10: ");
    31. convertDeciamlToBinary(10);
    32.  
    33. System.out.println("\nBinary representation of 12: ");
    34.  convertDeciamlToBinary(12);
    35.  
    36. }
    37.  
    38. }


    Output:

    1. Binary representation of 1: 
    2. 1
    3. Binary representation of 4: 
    4. 100
    5. Binary representation of 10: 
    6. 1010
    7. Binary representation of 12: 
    8. 1100

    You Might Like:

    Baca selengkapnya

    Java Example Program to convert binary to decimal


    • Famous interview java question on conversions . 
    • We can convert binary to decimal in two ways
      1.Using Integer.parseInt() method
      2.Using custom logic



    1.Write a Java Program to convert binary to decimal number in java without using Integer.parseInt() method.

    1. import java.util.Scanner;
    2. public class DecimalFromBinary {
    3.  
    4.     /**
    5.      *www.instanceofjava.com
    6.      */
    7.  
    8.  public static void main(String[] args) {
    9.         
    10.  Scanner in = new Scanner( System.in );
    11.  System.out.println("Enter a binary number: ");
    12.  
    13.   int  binarynum =in.nextInt();
    14.   int binary=binarynum;
    15.         
    16. int decimal = 0;
    17. int power = 0;
    18.  
    19. while(true){
    20.  
    21.  if(binary == 0){
    22.  
    23.         break;
    24.  
    25.  } else {
    26.  
    27.    int tmp = binary%10;
    28.    decimal += tmp*Math.pow(2, power);
    29.    binary = binary/10;
    30.    power++;
    31.  
    32.  }
    33. }
    34.         System.out.println("Binary="+binary+" Decimal="+decimal); ;
    35. }
    36.  
    37. }

    Output:


    1. Enter a binary number: 
    2. 101
    3. Binary=101 Decimal=5




    2.Write a Java Program to convert binary to decimal number in java using Integer.parseInt() method.



    1. import java.util.Scanner;
    2. public class ConvertBinaryToDecimal{
    3.  
    4.     /**
    5.      *www.instanceofjava.com
    6.      */
    7.  
    8.  public static void main(String[] args) {
    9.         
    10.  Scanner in = new Scanner( System.in );
    11.  
    12.  System.out.println("Enter a binary number: ");
    13.  String binaryString =input.nextLine();
    14.  
    15.  System.out.println("Result: "+Integer.parseInt(binaryString,2));

    16.  
    17. }
    18.  
    19. }


    Output:


    1. Enter a binary number:
    2. 001
    3. Result: 1

    java program to convert binary to decimal


    You Might like:

    Baca selengkapnya

    Kamis, 17 Maret 2016

    Get collection values from hashtable example

    Get collection values from hashtable example

    1.Basic java collection framework example program to get Value collection from hashtable

    •   Collection values()   This method used to get collection of values
    •  Important note is that if any value is removed from set the original hashtable key also removed
    1. package com.setviewHashtable;
    2.  
    3. import java.util.Hashtable;

    4. import java.util.Enumeration;
    5. import java.util.Iterator;
    6. import java.util.Set;
    7.  
    8. public class HashtableExample{
    9.  
    10. public static void main(String[] args) {
    11.   
    12.  //create Hashtable object
    13.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
    14.        
    15. //add key value pairs to Hashtable
    16. hashtable.put("1","Java Interview Questions");
    17. hashtable.put("2","Java Interview Programs");
    18. hashtable.put("3","Concept and example program");
    19. hashtable.put("4","Concept and interview Questions");
    20. hashtable.put("5","Java Quiz");
    21. hashtable.put("6","Real time examples");
    22.  
    23.  
    24.  Collection c = hashtable.values();
    25.  System.out.println("Values of Collection created from Hashtable are :");
    26. //iterate through the Set of keys
    27.  
    28.  Iterator itr = c.iterator();
    29.  while(itr.hasNext())
    30.  System.out.println(itr.next());
    31.            
    32.  c.remove("Java Quiz");
    33.            
    34. System.out.println("Elements in hash table");
    35. Enumeration e=hashtable.elements();
    36.         
    37.  
    38.  while (e.hasMoreElements()) {
    39.         System.out.println(e.nextElement());
    40. }    

    41. }
    42.  
    43. }
       



    Output:

    1. Values of Collection created from Hashtable are :
    2. Real time examples
    3. Java Quiz
    4. Concept and interview Questions
    5. Concept and example program
    6. Java Interview Programs
    7. Java Interview Questions
    8. Elements in hash table
    9. Real time examples
    10. Concept and interview Questions
    11. Concept and example program
    12. Java Interview Programs
    13. Java Interview Questions


    Baca selengkapnya
    Java Collections example program Get Set view of Keys from Hashtable example

    Java Collections example program Get Set view of Keys from Hashtable example

    1.Basic java collection framework example program to get set view from hashtable

    • Set keySet()   This method used to get set view of the keys of hashtable
    •  Important note is that if any key is removed from set the original hashtable key also removed
    1. package com.setviewHashtable;
    2.  
    3. import java.util.Hashtable;

    4. import java.util.Enumeration;
    5. import java.util.Iterator;
    6. import java.util.Set;
    7.  
    8. public class HashtableExample{
    9.  
    10. public static void main(String[] args) {
    11.   
    12.  //create Hashtable object
    13.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
    14.        
    15. //add key value pairs to Hashtable
    16. hashtable.put("1","Java Interview Questions");
    17. hashtable.put("2","Java Interview Programs");
    18. hashtable.put("3","Concept and example program");
    19. hashtable.put("4","Concept and interview Questions");
    20. hashtable.put("5","Java Quiz");
    21. hashtable.put("6","Real time examples");
    22.  
    23.  
    24.  Set st = hashtable.keySet();       
    25.  System.out.println("Set created from Hashtable Keys contains :");
    26. //iterate through the Set of keys
    27.  
    28.  Iterator itr = st.iterator();
    29.  while(itr.hasNext())
    30.  System.out.println(itr.next());
    31.            
    32.  st.remove("1");
    33.            
    34. System.out.println("Elements in hash table");
    35. Enumeration e=hashtable.keys();
    36.         
    37.  
    38.  while (e.hasMoreElements()) {
    39.         System.out.println(e.nextElement());
    40. }    

    41. }
    42.  
    43. }
       



    Output:

    1. Set created from Hashtable Keys contains :
    2. 6
    3. 5
    4. 4
    5. 3
    6. 2
    7. 1
    8. Elements in hash table
    9. 6
    10. 5
    11. 4
    12. 3
    13. 2
    Baca selengkapnya

    Rabu, 16 Maret 2016

    Java collections interview programming questions

    java collections interview programs

    1. Introduction to Collection Framework  

    2. Collection Interface in Java 
    3. Top 20 collection framework interview questions for freshers and experienced

    Collection set interface: 

    1. Collection Set Interface    

    Hashset Class in Collection framework: (Java programming questions)

    1. Hashset class in java  
    2.  
    3.  
    4.  

    LinkedHashSet Class in Collection framework: (Java programming questions)

    1.  

    Treeset Class in Collection framework: (Java programming questions)
     
    1.  
    2.  



     Collection List Interface:

    1. Collection List Interface 

    ArrayList Class in Collection framework: (Java programming questions)

    1.    
    2.  
    3.  
    4.  

    Map Interface In java

    1. Map interface  

    HashMap Class in Collection framework: (Java programming questions)

    1.  
    2.  
    3. Convert keys of a map to List 
    4. Convert Values of a map to List 

     Hashtable Class in Collection framework: (Java interview programming questions)

    1.  
    2.  
    3.  

    Java collections programming interview questions 

    1. Top 20 collection framework interview questions for freshers and experienced  
    2. Collection vs Collections
    3. Difference between enumeration and iterator and list iterator? 
    4. Difference between arraylist and vector 
    5. Differences between HashMap and Hash-table  
    6. Comparable vs Comparator 
    7. Custom iterator in java   
    Baca selengkapnya
    Remove all elements from hashtable java example

    Remove all elements from hashtable java example

    1.Basic java example program to remove all elements from hashtable
    • Object remove(Object key)    This method used to remove value pair from hashtable
    • void clear() this method removes all elements from hashtable
    •  
    1. package com.removevalueHashtable;
    2.  
    3. import java.util.Hashtable;

    4. import java.util.Enumeration;
    5.  
    6. public class HashtableExample{
    7.  
    8. public static void main(String[] args) {
    9.   
    10.  //create Hashtable object
    11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
    12.        
    13. //add key value pairs to Hashtable
    14. hashtable.put("1","Java Interview Questions");
    15. hashtable.put("2","Java Interview Programs");
    16. hashtable.put("3","Concept and example program");
    17. hashtable.put("4","Concept and interview Questions");
    18. hashtable.put("5","Java Quiz");
    19. hashtable.put("6","Real time examples");
    20.  
    21. Object obj = ht.remove("2");
    22. System.out.println(obj + " Removed from Hashtable");
    23.  
    24. Enumeration e=hashtable.elements();
    25.           
    26.  
    27.  while (e.hasMoreElements()) {
    28.         System.out.println(e.nextElement());
    29. }    
    30. hashtable.clear();
    31.  
    32.  System.out.println("Total key value pairs in Hashtable are : " + hashtable.size());

    33. }
    34.  
    35. }
       



    Output:

    1. Java Interview Programs Removed from Hashtable
    2. Real time examples
    3. Java Quiz
    4. Concept and interview Questions
    5. Concept and exampe program
    6. Java Interview Questions
    7. Total key value pairs in Hashtable are : 0
    Baca selengkapnya
    Remove key value pair from hashtable java example

    Remove key value pair from hashtable java example

    1.Basic java example program to iterate keys of hashtable
    • Object remove(Object key)    This method used to remove value pair from hashtable
    1. package com.removevalueHashtable;
    2.  
    3. import java.util.Hashtable;

    4. import java.util.Enumeration;
    5.  
    6. public class HashtableExample{
    7.  
    8. public static void main(String[] args) {
    9.   
    10.  //create Hashtable object
    11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
    12.        
    13. //add key value pairs to Hashtable
    14. hashtable.put("1","Java Interview Questions");
    15. hashtable.put("2","Java Interview Programs");
    16. hashtable.put("3","Concept and example program");
    17. hashtable.put("4","Concept and interview Questions");
    18. hashtable.put("5","Java Quiz");
    19. hashtable.put("6","Real time examples");
    20.  
    21. Object obj = ht.remove("2");
    22. System.out.println(obj + " Removed from Hashtable");
    23.  
    24. Enumeration e=hashtable.elements();
    25.           
    26.  
    27.            
    28. // display search result
    29.  while (e.hasMoreElements()) {
    30.         System.out.println(e.nextElement());
    31. }    

    32. }
    33.  
    34. }
       



    Output:

    1. Java Interview Programs Removed from Hashtable
    2. Real time examples
    3. Java Quiz
    4. Concept and interview Questions
    5. Concept and exampe program
    6. Java Interview Questions
    Baca selengkapnya
    Iterate through values of hashtable java

    Iterate through values of hashtable java

    1.Basic java example program to iterate keys of hashtable
    • Enumeration elements()   This method used to get values of hashmap

    1. package com.iteartekeysHashtable;
    2.  
    3. import java.util.Hashtable;

    4. import java.util.Enumeration;
    5.  
    6. public class HashtableExample{
    7.  
    8. public static void main(String[] args) {
    9.   
    10.  //create Hashtable object
    11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
    12.        
    13. //add key value pairs to Hashtable
    14. hashtable.put("1","Java Interview Questions");
    15. hashtable.put("2","Java Interview Programs");
    16. hashtable.put("3","Concept and example program");
    17. hashtable.put("4","Concept and interview Questions");
    18. hashtable.put("5","Java Quiz");
    19. hashtable.put("6","Real time examples");
    20.  
    21.  
    22.  
    23. Enumeration e=hashtable.elements();
    24.           
    25.  
    26.            
    27. // display search result
    28.  while (e.hasMoreElements()) {
    29.         System.out.println(e.nextElement());
    30. }    

    31. }
    32.  
    33. }
       



    Output:

    1. Real time examples
    2. Java Quiz
    3. Concept and interview Questions
    4. Concept and example program
    5. Java Interview Programs
    6. Java Interview Questions
    Baca selengkapnya

    Method overloading interview questions java

    1.What is Method in java?

    • Method is a sub block of a class that contains logic of that class.
    • logic must be placed inside a method, not directly at class level, if we place logic at class level compiler throws an error.
    • So class level we are allowed to place variables and methods.
    • The logical statements such as method calls, calculations and printing related statements must be placed inside method, because these statements are considered as logic. 
    • For more information @ Methods in java



    1. package com.instanceofjava;
    2. class sample{
    3.  
    4. int a;
    5. int b;
    6.  
    7. System.out.println("instance of java"); // compiler throws an error.
    8.  
    9. }
    10.  

    1. package com.instanceofjava;
    2. class sample{
    3.  
    4. static int a=10;
    5.    
    6. public static void main(String args[]){

    7. System.out.println(a); // works fine,prints a value:10
    8.  
    9.  }
    10. }

    2.What is meant by method overloading in java?


    • Defining multiple methods with same name is known as polymorphism.
    • Defining multiple methods with same name and with different arguments known as method overloading.

    1. package com.instanceofjava;
    2. class A{
    3.  
    4. public void show(int a){
    5. System.out.println("saidesh");
    6. }
    7.  
    8. public void show(int a,int b){
    9. System.out.println("ajay");
    10. }
    11.  
    12. public void show(float a){
    13. System.out.println("vinod");
    14. }
    15. public static void main(String args[]){
    16.  
    17. A a=new A();
    18.  
    19. a.show(10);
    20. a.show(1,2);
    21. a.show(1.2);
    22.  
    23. }
    24. }
    Output:

    1. saidesh
    2. ajay
    3. vinod


    3.What are the other names for method overloading?


    • Method overloading also known as static polymorphism or compile time polymorphism because at compile time itself we can tell which method going to get executed based on method arguments.
    • So method overloading also called as static binding. 

    4.What are the basic rules of method overloading?

    • Defining a method with same name and differ in number of arguments
    • Defining a method with same name and differ in type of arguments
    • Defining a method with same name and differ in order of type of arguments
    • Return type of the method not involved in method overloading.

    5.Can we overload static methods in java?

    • Yes. We can overload static methods in java.
    • Method overriding is not possible but method overloading is possible for static methods.
    • Before that lets see about method overloading in java.
    • lets see an example java program which explains static method overloading.
    1. class StaticMethodOverloading{
    2.  
    3. public static void staticMethod(){
    4.  
    5. System.out.println("staticMethod(): Zero arguments");
    6.  
    7.  
    8. public static void staticMethod(int a){
    9.  
    10. System.out.println("staticMethod(int a): one argument");
    11.  
    12.  
    13. public static void staticMethod(String str, int x){
    14.  
    15. System.out.println("staticMethod(String str, int x): two arguments");
    16.  
    17. }
    18.  
    19. public static void main(String []args){
    20.   
    21.   StaticMethodOverloading.staticMethod();
    22.   StaticMethodOverloading.staticMethod(12);
    23.   StaticMethodOverloading.staticMethod("Static method overloading",10);
    24.  
    25. }
    26. }


     Output:

    1. staticMethod(): Zero arguments
    2. staticMethod(int a): one argument
    3. staticMethod(String str, int x): two arguments


    6.Can we overload main method in java?

    Yes we can overload main method in java

    Java Program to overload main method in java

    1. class mainMethodOverloading{
    2.  
    3. public static void main(boolean x){
    4.  
    5. System.out.println("main(boolean x) called ");
    6.  
    7.  
    8. public static void main(int x){
    9.  
    10. System.out.println("main(int x) called");
    11.  
    12.  
    13. public static void main(int a, int b){
    14.  
    15. System.out.println("main(int a, int b) called");
    16.  
    17. }
    18.  
    19. public static void main(String []args){
    20.    
    21.  
    22. System.out.println("main(String []args) called ");
    23.  
    24.   mainMethodOverloading.main(true);
    25.   mainMethodOverloading.main(10);
    26.  mainMethodOverloading.main(37,46);
    27.  
    28.  
    29. }
    30. }




     Output:

    1. main(String []args) called
    2. main(boolean x) called
    3. main(int x) called
    4. main(int a, int b) called

    method overloading interview questions





    7.Can we overload constructors in java?

    • Yes we can overload constructor in java

    1. package instanceofjava;
    2. class ConstructorChaining{
    3. int a,b 
    4. ConstructorChaining(){
    5. this(1,2);
    6.  System.out.println("Default constructor");
    7.  
    8.  
    9. ConstructorChaining(int x , int y){
    10.  
    11. this(1,2,3); 
    12. a=x;
    13. b=y;
    14.  System.out.println("Two argument constructor");
    15.  
    16. }
    17.  
    18. ConstructorChaining(int a , int b,int c){
    19.  System.out.println("Three argument constructor")
    20.  
    21. public static void main(String[] args){
    22.  
    23.  ConstructorChaining obj=new ConstructorChaining();
    24.   System.out.println(obj.a);
    25.   System.out.println(obj.b);
    26.  
    27. }
    28. }


    You Might Like:



    Baca selengkapnya

    Selasa, 15 Maret 2016

    Iterate through keys of hashtable java

    Iterate through keys of hashtable java

    1.Basic java example program to iterate keys of hashtable
    • Enumeration keys()   This method used to get keys of hashmap

    1. package com.iteartekeysHashtable;
    2.  
    3. import java.util.Hashtable;

    4. import java.util.Enumeration;
    5.  
    6. public class HashtableExample{
    7.  
    8. public static void main(String[] args) {
    9.   
    10.  //create Hashtable object
    11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
    12.        
    13. //add key value pairs to Hashtable
    14. hashtable.put("1","Java Interview Questions");
    15. hashtable.put("2","Java Interview Programs");
    16. hashtable.put("3","Concept and exampe program");
    17. hashtable.put("4","Concept and interview Questions");
    18. hashtable.put("5","Java Quiz");
    19. hashtable.put("6","Real time examples");
    20.  
    21.  
    22.  
    23. Enumeration e=hashtable.keys();
    24.           
    25.  
    26.            
    27. // display search result
    28.  while (e.hasMoreElements()) {
    29.         System.out.println(e.nextElement());
    30. }    

    31. }
    32.  
    33. }
       



    Output:

    1. 6
    2. 5
    3. 4
    4. 3
    5. 2
    6. 1
    Baca selengkapnya
    Check particular value exist in hashtable

    Check particular value exist in hashtable

    1.Basic java example program to check particular value exist in hashtable
    • boolean contains(Object value)   This method used to check  particular value exist in hashtable

    1. package com.CheckElelemtHashtable;
    2.  
    3. import java.util.Hashtable;

    4. import java.util.Enumeration;
    5.  
    6. public class HashtableExample{
    7.  
    8. public static void main(String[] args) {
    9.   
    10.  //create Hashtable object
    11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
    12.        
    13. //add key value pairs to Hashtable
    14. hashtable.put("1","Java Interview Questions");
    15. hashtable.put("2","Java Interview Programs");
    16. hashtable.put("3","Concept and exampe program");
    17. hashtable.put("4","Concept and interview Questions");
    18. hashtable.put("5","Java Quiz");
    19. hashtable.put("6","Real time examples");
    20.  
    21.  /*
    22.  To check whether a particular value exists in Hashtable we need to use
    23.  boolean containsKey(Object value) method of Hashtable class.
    24. if the Hashtable contains mapping for specified value It returns true
    25. otherwise returns false.
    26. */
    27.        
    28.  boolean isExists = hashtable.contains("Java Quiz");
    29.  System.out.println("Java Quiz exists in Hashtable ? : " + isExists);

    30. System.out.println("Hashtable values : ");
    31.  
    32. Enumeration e=hashtable.elements();
    33.           
    34.  
    35.            
    36. // display search result
    37.  while (e.hasMoreElements()) {
    38.         System.out.println(e.nextElement());
    39. }    

    40. }
    41.  
    42. }
       



    Output:

    1. Java Quiz exists in Hashtable ? : true
    2. Hashtable values : 
    3. Real time examples
    4. Java Quiz
    5. Concept and interview Questions
    6. Concept and exampe program
    7. Java Interview Programs
    8. Java Interview Questions
    Baca selengkapnya
    Check if a particular key exists in Java Hashtable example

    Check if a particular key exists in Java Hashtable example

    1.Basic java example program to check particular element exist in hashtable
    • boolean containsKey(Object key)   This method used to check  particular key exist in hashtable

    1. package com.CheckElelemtHashtable;
    2.  
    3. import java.util.Hashtable;
    4. import java.util.Iterator;
    5.  
    6. public class HashtableExample{
    7.  
    8. public static void main(String[] args) {
    9.   
    10.  //create Hashtable object
    11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
    12.        
    13. //add key value pairs to Hashtable
    14. hashtable.put("1","Java Interview Questions");
    15. hashtable.put("2","Java Interview Programs");
    16. hashtable.put("3","Concept and exampe program");
    17. hashtable.put("4","Concept and interview Questions");
    18. hashtable.put("5","Java Quiz");
    19. hashtable.put("6","Real time examples");
    20.  
    21.  /*
    22.  To check whether a particular key exists in Hashtable we need to use
    23.  boolean containsKey(Object key) method of Hashtable class.
    24. if the Hashtable contains mapping for specified key It returns true
    25. otherwise returns false.
    26. */
    27.        
    28.  boolean isExists = hashtable.containsKey("5");
    29.  System.out.println("5 exists in Hashtable ? : " + isExists);

    30.  
    31.  
    32. Enumeration e=hashtable.elements();
    33.           
    34.  System.out.println("Display result:"); 
    35.            
    36. // display search result
    37.  while (e.hasMoreElements()) {
    38.         System.out.println(e.nextElement());
    39. }    

    40. }
    41.  
    42. }
       



    Output:

    1. 5 exists in Hashtable ? : true
    2. Hashtable values : 
    3. Display result:
    4. Real time examples
    5. Java Quiz
    6. Concept and interview Questions
    7. Concept and exampe program
    8. Java Interview Programs
    9. Java Interview Questions
    Baca selengkapnya

    Non static blocks in java example

    Non Static Blocks in java

    • When ever object created non static blocks will be executed before the execution of constructor
    • Non static blocks are class level block which does not have prototype


    1. package nonstaticblocks;
    2. public class A {
    3.  
    4.    {
    5.         
    6.        System.out.println("non static block executed");
    7.  
    8.     }
    9.  
    10. }


    What is the need of Non static blocks in java?

    • To execute any logic whenever object is created irrespective of constructor used in object creation.

    Who will execute Non static blocks?

    • Non static blocks are automatically called by JVM for every object creation in java stack area

    How many Non static blocks we can create?
    • We can create any number of Non static blocks

    Order of execution of non static blocks

    • Order of execution of non static blocks will be order as they are defined.
    1. package nonstaticblocks;
    2. public class A {
    3.  
    4. {

    5.   System.out.println("first block");
    6.  
    7. {
    8.  System.out.println("second block");
    9. }
    10.  
    11. {
    12.  System.out.println("third block");
    13. }
    14. public static void main(String[] args) {
    15.   A obj= new A();
    16. }
    17. }

     Output:

    1. first block
    2. second block
    3. third block


     Order of execution of non static blocks with respect to constructor?



    non static blocks in java example program


    Baca selengkapnya
    Remove all elements LinkedHashSet example

    Remove all elements LinkedHashSet example

    1.Basic java example program to remove all elements in linkedhashset
    • clear()   This method used to remove all elements from Linkedhashset.

    1. package com.removeelementLinkedhashset;
    2.  
    3. import java.util.LinkedHashSet;
    4. import java.util.Iterator;
    5.  
    6. public class LinkedHashsetExample{
    7.  
    8. public static void main(String[] args) {
    9.   
    10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
    11.        
    12.         linkedhashset.add("Java Interview Questions");
    13.         linkedhashset.add("Java interview program");
    14.         linkedhashset.add("Concept and example program");
    15.         linkedhashset.add("Concept and interview questions");
    16.         linkedhashset.add("Java Quiz");
    17.     
    18.       
    19. System.out.println("LinkedHashSet before removal : " + linkedhashset);
    20.  
    21.   boolean blnRemoved = linkedhashset.remove("Java Quiz");
    22.   System.out.println("Was Java Quiz removed from LinkedHashSet ? " + blnRemoved);
    23.   

    24.  
    25. System.out.println("LinkedHashSet after removal : ");
    26.  
    27. Iterator it=linkedhashset.iterator();
    28.              
    29. while(it.hasNext()){
    30. System.out.println(it.next());
    31.                      
    32. }   
    33.  linkedhashset.clear();
    34.  System.out.println("LinkedHashSet after removal  all elements"); 
    35.  
    36. }
    37.  
    38. }
       



    Output:

    1. LinkedHashSet before removal : [Java Interview Questions, Java interview program, Concept
    2. and example program, Concept and interview questions, Java Quiz]
    3. Was Java Quiz removed from LinkedHashSet ? true
    4. LinkedHashSet after removal :
    5. Java Interview Questions
    6. Java interview program
    7. Concept and example program
    8. Concept and interview questions
    9. LinkedHashSet after removal all elements  :[]
    Baca selengkapnya
    Differences between Default constructor and no argument constructor in java

    Differences between Default constructor and no argument constructor in java

    Differences between default constructor and no argument constructor


    Default Constructor in java:

    • When we write a class without any constructor then at compilation time java compiler creates a default constructor in our class.
    • The accessibility modifier of the default constructor is same as accessibility modifier of class.
    • The allowed accessibility modifier are public and default.
    • Default constructor added by java compiler this constructor does not have anything except super(); call.





    1. package constructor;
    2. public class A {
    3.   
    4.  
    5. }


    1. package constructor;
    2. public class A {
    3.  
    4.     A(){
    5.         
    6.         super();
    7.  
    8.     }
    9.  
    10. }



    • If our class have any constructor then java compiler does not create default constructor

    No-argument Constructor in java:

    • As a developer we can create our own constructor with no arguments is known as no-argument constructor.
    • It can have all four accessibility modifiers as it is defined by developer.
    • So allowed accessibility modifiers are public, private, protected and default
    • It can have logic including super call.


    1. package constructor;
    2. public class A {
    3.  
    4.     A(){
    5.         
    6.   super();
    7.   System.out.println("no -argument constructor");

    8.     }
    9.  
    10. }

    • The common point between default and no-argument constructor 
    • Both does not have any arguments.
    • And one more point we need to remember that in no-argument constructor also by default first statement will be super() call which is added by java compiler if it does not have.
    Baca selengkapnya
    Remove specified element from Java LinkedHashSet example

    Remove specified element from Java LinkedHashSet example

    1.Basic java example program to remove particular element in linkedhashset
    • boolean remove(Object o)    This method used to remove specified element from Linkedhashset.

    1. package com.removeelementLinkedhashset;
    2.  
    3. import java.util.LinkedHashSet;
    4. import java.util.Iterator;
    5.  
    6. public class LinkedHashsetExample{
    7.  
    8. public static void main(String[] args) {
    9.   
    10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
    11.        
    12.         linkedhashset.add("Java Interview Questions");
    13.         linkedhashset.add("Java interview program");
    14.         linkedhashset.add("Concept and example program");
    15.         linkedhashset.add("Concept and interview questions");
    16.         linkedhashset.add("Java Quiz");
    17.     
    18.       
    19. System.out.println("LinkedHashSet before removal : " + linkedhashset);
    20.  
    21.   boolean blnRemoved = linkedhashset.remove("Java Quiz");
    22.   System.out.println("Was Java Quiz removed from LinkedHashSet ? " + blnRemoved);
    23.   

    24.  
    25. System.out.println("LinkedHashSet after removal : ");
    26.  
    27. Iterator it=linkedhashset.iterator();
    28.              
    29. while(it.hasNext()){
    30. System.out.println(it.next());
    31.                      
    32. }   
    33.  
    34. }
    35.  
    36. }
       



    Output:

    1. LinkedHashSet before removal : [Java Interview Questions, Java interview program, Concept
    2. and example program, Concept and interview questions, Java Quiz]
    3. Was Java Quiz removed from LinkedHashSet ? true
    4. Java Interview Questions
    5. Java interview program
    6. Concept and example program
    7. Concept and interview questions


    Baca selengkapnya
    Check if a particular element exists in Java LinkedHashSet Example

    Check if a particular element exists in Java LinkedHashSet Example

    1.Basic java example program to check particular element is exists in linkedhashset
    • boolean contains(Object o)  This method Returns true if this set contains the specified element

    1. package com.checkelementhashset;
    2.  
    3. import java.util.LinkedHashSet;
    4. import java.util.Iterator;
    5.  
    6. public class LinkedHashsetExample{
    7.  
    8. public static void main(String[] args) {
    9.   
    10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
    11.        
    12.         linkedhashset.add("Java Interview Questions");
    13.         linkedhashset.add("Java interview program");
    14.         linkedhashset.add("Concept and example program");
    15.         linkedhashset.add("Concept and interview questions");
    16.         linkedhashset.add("Java Quiz");
    17.     
    18.  /*
    19.  To check whether a particular value exists in LinkedHashSet we need to use
    20.   boolean contains(Object value) method of HashSet class.
    21.  this method returns true if the LinkedHashSet contains the value, otherwise returns false.
    22.  */
    23.        
    24.         boolean isExists = linkedhashset.contains("Java Quiz");
    25.         System.out.println("Java Quiz exists in LinkedHashSet? : " + isExists);    
    26.  

    27.  
    28. Iterator it=linkedhashset.iterator();
    29.              
    30. while(it.hasNext()){
    31. System.out.println(it.next());
    32.                      
    33. }   
    34.  
    35. }
    36.  
    37. }
       



    Output:

    1. Java Quiz exists in LinkedHashSet ? : true
    2. Java Interview Questions
    3. Java interview program
    4. Concept and example program
    5. Concept and interview questions
    6. Java Quiz
    Baca selengkapnya

    Return statement in finally block in java

    Can we write return statement in finally block

    • Finally block will executes always excepts system.exit().
    • So if we are returning some value in finally means it will be returned always
    • Finally will executed so method always returns finally return value and no need of keeping return value at end of the method.
    • And in finally after return if we keep some statement those statement will be treated as dead code.



    i)Return statement in finally block
    1. package com.exceptionhandlingiinterviewquestions;
    2.  
    3. public class TryCatchReturn{
    4.  
    5. int calc(){ 
    6.         
    7. try {
    8.  

    9.  
    10. } catch (Exception e) {
    11.  
    12. }
    13.  finally(){
    14.  return 1;
    15. }   
    16.  System.out.println("End of the method"); // Error : Unreachable code
    17. }
    18.     
    19.     
    20. public static void main(String[] args) {
    21.         
    22.         TryCatchReturn obj = new TryCatchReturn();
    23.        
    24.  
    25. }

    26. }
       
    ii) return statement in finally



    1. package com.exceptionhandlingiinterviewquestions;
    2.  
    3. public class TryCatchReturn{
    4.  
    5. int calc(){ 
    6.         
    7. try {
    8.  

    9.  
    10. } catch (Exception e) {
    11.  
    12. }
    13.  finally(){
    14.  return 1;
    15. System.out.println("End of the method"); // Error : Unreachable code
    16. }   
    17.  
    18. }
    19.     
    20.     
    21. public static void main(String[] args) {
    22.         
    23.         TryCatchReturn obj = new TryCatchReturn();
    24.        
    25.  
    26. }

    27. }
       
    iii) return statement in try catch and finally blocks



    1. package com.exceptionhandlingiinterviewquestions;
    2.  
    3. public class TryCatchReturn{
    4.  
    5. int calc(){ 
    6.         
    7. try {
    8.  
    9. return 10;
    10.  
    11. } catch (Exception e) {
    12.  return 20;
    13. }
    14.  finally(){
    15.  return 30;
    16. }   
    17.  
    18. }
    19.     
    20.     
    21. public static void main(String[] args) {
    22.         
    23.  TryCatchReturn obj = new TryCatchReturn();
    24.        
    25.  System.out.println(obj.calc())
    26. }

    27. }



    Output:


    1. 30

    finally with return statement in java

    Baca selengkapnya