Friday, 30 December 2016

While Loop In Java Example

package com.kt4study.corejava.loop;

import java.util.ArrayList;
import java.util.Iterator;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class WhileLoopExample {

  public static void whileLoopExampleDemo() {
    
    //While Loop Type 1
    int i = 1;
    // Check condition
    while (i <= 3) {
      System.out.println("Current value Of 'i' is :: " + i);
      i++;
    }
    
    //While Loop Type 2 forArrayList Object 
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("Java Guider");
    arrayList.add("Kt4Study.blogspot.co.uk");
    // Get list of ArrayList elements to iterate
    Iterator<String> iterator = arrayList.iterator();

    // Check condition
    while (iterator.hasNext()) {
      System.out.println("Iterated element from while loop :: " + iterator.next());
    }

  }

  public static void main(String[] args) {
    WhileLoopExample.whileLoopExampleDemo();
  }
}

Output →

Current value Of 'i' is :: 1
Current value Of 'i' is :: 2
Current value Of 'i' is :: 3
Iterated element from while loop :: Java Guider
Iterated element from while loop :: Kt4Study.blogspot.co.uk

For Loop In Java Example

package com.kt4study.corejava.loop;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class ForLoopExample {
  
  public static void  forLoopExampleDemo(){
    
    //initialization, condition check, Increment/Decrement
    for(int i = 0; i<4; i++){
      System.out.println("Current value of 'i' is :: " + i);
    }
  }
  
  public static void main(String[] args) {
    ForLoopExample.forLoopExampleDemo();
  }
}

Output →

Current value of 'i' is :: 0
Current value of 'i' is :: 1
Current value of 'i' is :: 2
Current value of 'i' is :: 3

For Each Loop In Java Example

package com.kt4study.corejava.loop;

import java.util.ArrayList;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class ForEachLoopExample {
  
  public static void forEachDemo(){
    
    //Iterate ArrayList with For Each Loop
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("Hello World");
    arrayList.add("kt4study.blogspot.co.uk");
    for (String element : arrayList) {
      System.out.println("Element from ArrayList :: " + element);
    }
    
    //Iterate One Directional Array with For Each Loop
    int intArray[] 156};
    for (int element : intArray) {
      System.out.println("Element from OneDirectionalArray :: "+  element);
    }
    
    //Iterate Multi Directional Array with For Each Loop
    int[][] intMultiArray = new int[2][2];
    intMultiArray[0][010;
    intMultiArray[0][112;
    intMultiArray[1][014;
    intMultiArray[1][116;
    
    for (int[] array : intMultiArray) {
      for (int element: array) {
        System.out.println("Element from MultiDirectionalArray :: " + element);
        }
    }
  }
  
  public static void main(String[] args) {
    ForEachLoopExample.forEachDemo();
  }

}

Output →

Element from ArrayList :: Hello World
Element from ArrayList :: kt4study.blogspot.co.uk
Element from OneDirectionalArray :: 15
Element from OneDirectionalArray :: 6
Element from OneDirectionalArray :: 4
Element from MultiDirectionalArray :: 10
Element from MultiDirectionalArray :: 12
Element from MultiDirectionalArray :: 14
Element from MultiDirectionalArray :: 16

Do While Loop In Java Example

package com.kt4study.corejava.loop;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class DoWhileLoopExample {

  public static void doWhileLoopExampleDemo() {

    int i = 1;
    do{
      System.out.println("Value Of 'i' :: " + i);
      i++;
    }while(i <4)// Post condition check
    
  }

  public static void main(String[] args) {
    DoWhileLoopExample.doWhileLoopExampleDemo();
  }
}

Output →

Current value Of 'i' is :: 1
Current value Of 'i' is :: 2
Current value Of 'i' is :: 3

Ternary Operator In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class TernaryOperatorExample {
  
  public static void ternaryOperator(){
    int a = 10;
    int b = 5;
    int result;
    
    // ?: Ternary Operator Example
    result = ((a+b== 151000 2000 ;
    System.out.println("Result  :: " + result);
    
  }

  public static void main(String[] args) {
    TernaryOperatorExample.ternaryOperator()
  }
}

/*Output -->
Result  :: 1000
*/
Output →

Result :: 1000

Relational Operators In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class RelationalOperatorsExample {

  public static void relationalOperators(int a, int b) {

    boolean result;

    // == Equal To example
    result = (a == b);
    System.out.println("Does 'a' equal to 'b' ::" + result);

    // != Not Equal To example
    result = (a != b);
    System.out.println("Does 'a' not equal to 'b' ::" + result);

    // > Greater Than example
    result = (a > b);
    System.out.println("Does 'a' greater than 'b' ::" + result);

    // < Less Than example
    result = (a < b);
    System.out.println("Does 'a' less than 'b' ::" + result);

    // > Greater Than Or Equal To example
    result = (a >= b);
    System.out.println("Does 'a' greater than or equal to 'b' ::" + result);

    // < Less Than Or Equal To example
    result = (a <= b);
    System.out.println("Does 'a' less than or equal to 'b' ::" + result);

  }

  public static void main(String[] args) {
    RelationalOperatorsExample.relationalOperators(2015);
  }
}
Output →

Does 'a' equal to 'b' ::false
Does 'a' not equal to 'b' ::true
Does 'a' greater than 'b' ::true
Does 'a' less than 'b' ::false
Does 'a' greater than or equal to 'b' ::true
Does 'a' less than or equal to 'b' ::false

Logical Operators In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class LogicalOperatorsExample {
  
  public static void logicalOperators() {
    
    boolean a = true;
    boolean b = false;
    boolean result;
    
    // && Logical AND example
    result = (a && b);
    System.out.println("Logical AND  (a && b) :: " + result);
    
    // || Logical OR example
    result = (a || b);
    System.out.println("Logical OR  (a || b) :: " + result);
        
    // ! Logical Not example
    result = !(a || b);
    System.out.println("Logical NOT  !(a || b) :: " + result);
  }

  public static void main(String[] args) {
    LogicalOperatorsExample.logicalOperators();
  }

}

Output →

Logical AND (a && b) :: false
Logical OR (a || b) :: true
Logical NOT !(a || b) :: false

Instanceof Operator In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class InstanceofOperatorExample {

  public static void instanceofOperator(){
    String tempString = "Hello World";
    
    if (tempString instanceof String){
      System.out.println("tempString is instanceof String Object.");
    }else{
      System.out.println("tempString is Not instanceof String Object.");
    }
  }
  
  public static void main(String[] args) {
    InstanceofOperatorExample.instanceofOperator();
  }
}

Output →

tempString is instanceof String Object.

Bitwise Operators In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class BitwiseOperatorsExample {
  
  public static void bitwiseOperators() {
    
    int a = 24;
    //Bit Value - 0001 1000
    int b = 20;
    //Bit Value - 0001 0100
    int result;
    
    // & Binary AND example
    result = (a & b);
    System.out.println("Binary AND (a & b) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // | Binary OR example
    result = (a | b);
    System.out.println("Binary OR (a | b) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // ^ Binary XOR example
    result = (a ^ b);
    System.out.println("Binary XOR (a ^ b) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // ~ Binary Complement example
    result = ~a;
    System.out.println("Binary Complement of '~a' ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // << Binary Left Shift example
    result = (a << 5);
    System.out.println("Binary Left Shift (a << 5) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // >> Binary Right Shift example
    result = (a >> 5);
    System.out.println("Binary Right Shift (a >> 5) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // >>> Binary Zero Fill Right Shift example
    result = (a >>> 5);
    System.out.println("Binary Zero Fill Right Shift (a >>> 5) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
  }

  public static void main(String[] args) {
    BitwiseOperatorsExample.bitwiseOperators();
  }

}

Output →

Binary AND (a & b) ::16 :: Binary String ::10000
Binary OR (a | b) ::28 :: Binary String ::11100
Binary XOR (a ^ b) ::12 :: Binary String ::1100
Binary Complement of '~a' ::-25 :: Binary String ::11111111111111111111111111100111
Binary Left Shift (a << 5) ::768 :: Binary String ::1100000000
Binary Right Shift (a >> 5) ::0 :: Binary String ::0
Binary Zero Fill Right Shift (a >>> 5) ::0 :: Binary String ::0

Assignment Operators In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class AssignmentOperatorsExample {

  public static void assignmentOperators(int a, int b) {

    int result;

    // = Simple Assignment Example
    result = (a + b);
    System.out.println("Simple Assignment (=) ::" + result);

    // += Addition Assignment example
    result = (a += b);
    System.out.println("Addition Assignment (a += b) ::" + result);

    // -= Subtraction Assignment example
    result = (a -= b);
    System.out.println("Subtraction Assignment (a -= b) ::" + result);

    // *= Multiplication Assignment example
    result = (a *= b);
    System.out.println("Multiplication Assignment (a *= b) ::" + result);

    // /= Division Assignment example
    result = (a /= b);
    System.out.println("Division Assignment (a /= b) ::" + result);

    // &=  Binary AND Assignment example
    result = (a &= b);
    System.out.println("Binary AND Assignment (a &= b) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // ^= Binary XOR Assignment example
    result = (a ^= b);
    System.out.println("Binary XOR Assignment (a ^= b) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // ^= Binary OR Assignment example
    result = (a |= b);
    System.out.println("Binary OR Assignment (a |= b) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // << Binary Left Shift Assignment example
    result = (a <<= 5);
    System.out.println("Binary Left Shift Assignment (a <<= 5) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // >> Binary Right Shift Assignment example
    result = (a >>= 5);
    System.out.println("Binary Right Shift Assignment (a >>= 5) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    
    // >>> Binary Zero Fill Right Shift Assignment example
    result = (a >>>= 5);
    System.out.println("Binary Zero Fill Right Shift Assignment (a >>>= 5) ::" + result +" :: Binary String ::"+ Integer.toBinaryString(result));
    

  }

  public static void main(String[] args) {
    AssignmentOperatorsExample.assignmentOperators(2015);
  }
}
Output →

Simple Assignment (=) ::35
Addition Assignment (a += b) ::35
Subtraction Assignment (a -= b) ::20
Multiplication Assignment (a *= b) ::300
Division Assignment (a /= b) ::20
Binary AND Assignment (a &= b) ::4 :: Binary String ::100
Binary XOR Assignment (a ^= b) ::11 :: Binary String ::1011
Binary OR Assignment (a |= b) ::15 :: Binary String ::1111
Binary Left Shift Assignment (a <<= 5) ::480 :: Binary String ::111100000
Binary Right Shift Assignment (a >>= 5) ::15 :: Binary String ::1111
Binary Zero Fill Right Shift Assignment (a >>>= 5) ::0 :: Binary String ::0

Arithmetic Operators In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class ArithmeticOperatorsExample {

  public static void arithmeticOperators() {

    int a = 10;
    int b = 5;
    int result;

    // + Addition example
    result = a + b;
    System.out.println("Addition of two number is :: " + result);

    // - Subtraction example
    result = a - b;
    System.out.println("Subtraction of two number is :: " + result);

    // * Multiplication example
    result = a * b;
    System.out.println("Multiplication of two number is :: " + result);

    // / Division example
    result = a / b;
    System.out.println("Division of two number is :: " + result);

    // % Remainder example
    result = a % b;
    System.out.println("Remainder of two number is :: " + result);

    // ++ Post Increment example
    result = a++;
    System.out.println("Post Increment of a is :: " + result);
    // ++ Pre Increment example
    a = 10//reset value of a
    result = ++a;
    System.out.println("Pre Increment of a is :: " + result);

    // -- Post Decrement example
    result = b--;
    System.out.println("Post Decrement of b is :: " + result);
    // -- Pre Decrement example
    b = 5//reset value of b
    result = --b;
    System.out.println("Pre Decrement of b is :: " + result);
  }

  public static void main(String[] args) {
    
    ArithmeticOperatorsExample.arithmeticOperators();
  }

}

Output →

Addition of two number is :: 15
Subtraction of two number is :: 5
Multiplication of two number is :: 50
Division of two number is :: 2
Remainder of two number is :: 0
Post Increment of a is :: 10
Pre Increment of a is :: 11
Post Decrement of b is :: 5
Pre Decrement of b is :: 4

How to create Hashtable in Java?

package com.kt4study.corejava.collection;

import java.util.Hashtable;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class HashtableExample {

  public static Hashtable<Integer, String> getHashtable() {
    
    // Create An Instance Of Hashtable
    Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>();
    // Add Integer as Key and String as Value in Hashtable.
    hashtable.put(11"Eleven");
    hashtable.put(1"One");
    hashtable.put(6"Six");
    //hashtable.put(null, null); //Hashtable can't have "null" key and "null" value

    return hashtable;
  }

  public static void main(String[] args) {

    Hashtable<Integer, String> hashtable = HashtableExample.getHashtable();

    // Print all the elements From Hashtable
    for (Integer key : hashtable.keySet()) {
      System.out.println("Element from Hashtable:: " + key + " - " + hashtable.get(key));
    }

  }
}

Output → Hashtable can't have "null" key and "null" value. All keys are unique in Hashtable.

Element from Hashtable:: 6 - Six
Element from Hashtable:: 1 - One
Element from Hashtable:: 11 - Eleven

How to create LinkedHashMap in Java?

package com.kt4study.corejava.collection;

import java.util.LinkedHashMap;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class LinkedHashMapExample {

  public static LinkedHashMap<String, Integer> getLinkedHashMap() {
    // Create An Instance Of LinkedHashMap
    LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<String, Integer>();
    // Add String as Key and Integer as Value in LinkedHashMap.
    linkedHashMap.put("No Of Players In Football"11);
    linkedHashMap.put("No Of Players In Chess"1);
    linkedHashMap.put("No Of Players In Volleyball"6);
    linkedHashMap.put("Variable"40);
    linkedHashMap.put(null, null);
    linkedHashMap.put("Variable"88);

    return linkedHashMap;
  }

  public static void main(String[] args) {

    LinkedHashMap<String, Integer> linkedHashMap = LinkedHashMapExample.getLinkedHashMap();

    // Print all the elements From LinkedHashMap
    for (String key : linkedHashMap.keySet()) {
      System.out.println("Element from LinkedHashMap:: " + key + " - " + linkedHashMap.get(key));
    }

  }
}

Output → LinkedHashMap can have "null" key and "null" value. All keys are unique in LinkedHashMap.

Element from LinkedHashMap:: No Of Players In Football - 11
Element from LinkedHashMap:: No Of Players In Chess - 1
Element from LinkedHashMap:: No Of Players In Volleyball - 6
Element from LinkedHashMap:: Variable - 88
Element from LinkedHashMap:: null - null

How to create TreeMap in Java?

package com.kt4study.corejava.collection;

import java.util.TreeMap;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class TreeMapExample {

  public static TreeMap<Integer, String> getTreeMap() {
    
    // Create An Instance Of TreeMap
    TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
    // Add String as Key and Integer as Value in TreeMap
    treeMap.put(11"Eleven");
    treeMap.put(1"One");
    treeMap.put(6"Six");

    return treeMap;
  }

  public static void main(String[] args) {

    TreeMap<Integer, String> treeMap = TreeMapExample.getTreeMap();

    // Print all the elements From TreeMap
    for (Integer key : treeMap.keySet()) {
      System.out.println("Element from TreeMap:: " + key + " - " + treeMap.get(key));
    }

  }
}

Output → All the elements are sorted on Key basis in TreeMap

Element from TreeMap:: 1 - One
Element from TreeMap:: 6 - Six
Element from TreeMap:: 11 - Eleven