Saturday, April 13, 2019

ROW ARRAY ????

public static void main(String[] args) {
 
String[][] schoolbagArray = new String[4][2];
schoolbagArray[0] = new String[] {"Books-0", "Notebooks-00", "Notebooks-000"};
schoolbagArray[1] = new String[] {"Books-1", "Notebooks-11", "Notebooks-111"};
schoolbagArray[2] = new String[] {"Books-2", "Notebooks-22", "Notebooks-222"};
schoolbagArray[3] = new String[] {"Books-3", "Notebooks-33", "Notebooks-333"};

System.out.print(schoolbagArray [0][0] + "  ");
System.out.print("  " + schoolbagArray [0][1] + " ");
System.out.print(schoolbagArray [0][2] + "  " + "\n");

System.out.print(schoolbagArray [1][0] + "  ");
System.out.print("  " + schoolbagArray [1][1] + " ");
System.out.print(schoolbagArray [1][2] + "\n");

System.out.print(schoolbagArray [2][0] + "  ");
System.out.print("  " + schoolbagArray [2][1] + " ");
System.out.print(schoolbagArray [2][2] + "\n");

System.out.print(schoolbagArray [3][0] + "  ");
System.out.print("  " + schoolbagArray [3][1] + " ");
System.out.print(schoolbagArray [3][2] + " ");

}

inside and outside main loop

public static void main(String[] args) {
/*
SupremeSuperClassC supremeSuperClassC = new SubClassB();
supremeSuperClassC.method3();
supremeSuperClassC.method4();
*/
String[][] schoolbagArray = new String[4][2];
  schoolbagArray[0] = new String[] {"Pens", "Pencils"};
  schoolbagArray[1] = new String[] {"Books", "Notebooks"};
  System.out.println( schoolbagArray [1][0] );
}
String[][] schoolbagArray = new String[4][2];
  schoolbagArray[0] = new String[] {"Pens", "Pencils"};
  schoolbagArray[1] = new String[] {"Books", "Notebooks"};
  System.out.println( "sssssssssssss");

Thursday, April 11, 2019

continue keyword example

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

public class Continue {
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
a1.add("1");a1.add("2");a1.add("3");
a1.add("4");a1.add("5");a1.add("6");
a1.add(null);a1.add("");a1.add(" ");

Iterator itr = a1.iterator();
while(itr.hasNext()) {
String a = (String) itr.next();
if(null==a || a.equals("")|| a.equals(" ")) {
continue;
}
else {
System.out.println("else loop " + a);
}
}
}
}

throws Exception class

public class ExceptionThrow {
public static void main(String[] args) throws Exception {
try {
throw new Exception("Exception Caught in the Exception Super class and not in ExceptonA catch block");
}
catch (ExceptionA e) {
System.out.println("e.getMessage() " + e.getMessage());
}
}
}
class ExceptionA extends ExceptionB {
String s;
public ExceptionA(String s) {
super(s);
}
}
class ExceptionB extends Exception{
public ExceptionB(String s) {
super(s);
}
}

Custom Exception Example

public class ExceptionThrow {
public static void main(String[] args) {
try {
throw new ExceptionA("EXCEPTION CAUGHT");
}
catch (ExceptionA e) {

System.out.println(e.getMessage());

}
}
}

(A)
class ExceptionA extends ExceptionB {
String s;
public ExceptionA(String s) {
//this.s=s;
super(s);
}
}
class ExceptionB extends Exception{

public ExceptionB(String s) {
super(s);
}
}
(B)
class ExceptionA extends ExceptionB {
String s;
public ExceptionA(String s) {
this.s=s;
        }
public String getMessage(){
return this.s;
}
}
class ExceptionB extends Exception{
}

Wednesday, April 10, 2019

Comparator before and after sorting

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Student {
int a;
String b,c;
public Student(int a, String b, String c) {
this.a = a;
this.b = b;
this.c = c;
}
public String toString() {
return this.a + "--"+ this.b + "--" + this.c + "--";
}
}
class Teacher implements Comparator<Student> {
@Override
public int compare(Student object1, Student object2) {
return object1.b.compareTo(object2.c);
}
}
class ComparatorBeforeandAfterSorting {
public static void main(String[] args) {
ArrayList<Student> a1 = new ArrayList<Student>();
a1.add(new Student(1,"A","Z"));
a1.add(new Student(1,"F","M"));
a1.add(new Student(1,"R","Q"));
a1.add(new Student(1,"E","U"));
a1.add(new Student(1,"C","U"));
System.out.println("before sorting");
for(int i=0;i<a1.size();i++) {
System.out.println(a1.get(i));
}
Collections.sort(a1, new Teacher());
System.out.println("after sorting");
for (int i = 0; i < a1.size(); i++) {
System.out.println(a1.get(i));
}
}
}

Sunday, April 7, 2019

StringInsideStringBuffer Example

import java.util.HashMap;
public class StringInsideStringBuffer {
public static void main(String[] args) {
HashMap<String,String> hmap = new HashMap();
hmap.put("appa-1", "amma-1");
StringBuffer sb1 = new StringBuffer(new String("balaji"));
System.out.println(sb1.toString());
StringBuffer sb2 = new StringBuffer(hmap.get("appa-1"));
System.out.println(sb2.toString());
}
}

java.util.NoSuchElementException Example

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorForLoop {
public static void main(String[] args) {
List l1 = new ArrayList();
l1.add("1");l1.add("2");l1.add("3");
l1.add("4");l1.add("5");l1.add("6");
for(Iterator itr=l1.iterator();itr.hasNext();itr.next()) {
while(itr.hasNext()) {
System.out.print(itr.next());
}
}
}
}

Answer : 

1 2 3 4 5 6
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(ArrayList.java:862)

at IteratorForLoop.main(IteratorForLoop.java:10)

Reason :

because itr.next() has been given in the for Loop

Iterator for loop


ConcreteClass extending AbstractSuperClass extending AnotherAbstractSuperClass extending... Example

public abstract class SupremeSuperClassA {
abstract void methodA();
abstract void methodB();
}

public abstract class SuperClassA extends SupremeSuperClassA{
abstract void method1();
abstract void method2();
abstract void method3();
}

public abstract class SubClassB extends SuperClassA {
@Override
void method1() {
}
}

public class SubClassC extends SubClassB {
@Override
void method3() {
}
@Override
void method2() {
}
@Override
void methodA() {
}
@Override
void methodB() {
}
}

Math.pow example

package Math;
public class Math_pow {
public static void main(String[] args) {

int val;
val = (int) Math.pow(2, 5);
System.out.println(val);

}
}

CollectionSizetoStringArray Example

import java.util.List;
import java.util.ArrayList;
public class CollectionSizetoStringArray {
public static void main(String[] args) {
List l1 = new ArrayList();
l1.add("10");l1.add("20");l1.add("30");l1.add("40");
String[] array = new String[5];
array = (String[]) l1.toArray(array);
for(String arrayElement : array) {
System.out.println("arrayElement-1 " + arrayElement);
}
array = (String[]) l1.toArray(new String[5]);
array[4] = "Balaji Balaji Balaji";
for(String arrayElement : array) {
System.out.println("arrayElement-2 " + arrayElement);
}

  String[] arraySizeX = (String[]) l1.toArray(new String[l1.size()+100]);
  for(String arrayElement : arraySizeX) {
        System.out.println("arrayElement-1 " + arrayElement);
  }
}
}

Saturday, April 6, 2019

radix example

public class IntegerparseIntExample {
public static void main(String[] args) {
int tenA = Integer.parseInt("1111", 2);
System.out.println(tenA);

  int a=2;
  int tenB = Integer.parseInt("1111", ++a);
  System.out.println(tenB);

}
}

NumberFormatException example

public class IntegerparseIntExample {
public static void main(String[] args) {
int ten = Integer.parseInt("Not a number", 2);
System.out.println(ten);
}
}

Answer:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Not a number"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)

at IntegerparseIntExample.main(IntegerparseIntExample.java:5)

StringArrays via Collection Example

import java.util.*;
public class StringArraysVIACollection_Example {
public static void main(String[] args) {
String returnArray[] = method1();
System.out.println(returnArray[0]);
System.out.println(returnArray[1]);
System.out.println(returnArray[2]);
System.out.println(returnArray[3]);
System.out.println(returnArray[4]);
System.out.println(returnArray[5]);
System.out.println(returnArray.length);
}
public static String[] method1() {
List l1 = new ArrayList();
l1.add("10");l1.add("20");l1.add("30");l1.add("40");l1.add("50");
String[] array = new String[6];
int i = 0;
Iterator itr = l1.iterator();
while(itr.hasNext()) {
array[i++] = (String) itr.next();
}
return array;
}
}

Thursday, April 4, 2019

adding method in an enum class

import java.io.Serializable;

public enum EnumValues implements Serializable {
   BALAJI("BALAJI"),
   BANG_AND_JANG_KUTTIES("THEY ARE THE WORLD'S GREATEST KUTTIES");

   private String displayName;
 
   private EnumValues(String displayName) {
      this.displayName = displayName;
   }
   public static EnumValues fromString(String bang_and_jang_kutties) {
       return BANG_AND_JANG_KUTTIES;
   }
   public static EnumValues getvalues() {
       return BANG_AND_JANG_KUTTIES;
   }
   public static void main(String[] args) {
   EnumValues[] array = EnumValues.values();
   System.out.println(array[0] +" "+ array[1]);
   System.out.println(EnumValues.getvalues());
   System.out.println(EnumValues.fromString("The Best"));
   }
}

enum values()

public enum EnumValues implements Serializable {
   BALAJI("BALAJI"),
   BANG_AND_JANG_KUTTIES("THEY ARE THE WORLD'S GREATEST KUTTIES");

   private String displayName;
 
   private EnumValues(String displayName) {
      this.displayName = displayName;
   }
 
   public EnumValues getValues(String input) {
   return BALAJI;
   }
   public static void main(String[] args) {
   System.out.println(EnumValues.values());  
   EnumValues[] array = EnumValues.values();
   System.out.println(array[0] +" "+ array[1]);
   }
}

Wednesday, April 3, 2019

How to add Initial Capacity to ArrayList

We can add initial capacity to arraylist like this

List list = new ArrayList(new String[10].length + 5);

Class Array Example


(a)
public class MainArrayTest {
public static void main(String[] args) {
MyExampleTest[] myexamples = new MyExampleTest[20];
myexamples[0] = new MyExampleTest();
myexamples[0].setQueryKey1(EnumValues.BALAJI);
myexamples[0].setQueryKey2(EnumValues.BANG_AND_JANG_KUTTIES);
System.out.println(myexamples[0].getQueryKey1());
System.out.println(myexamples[0].getQueryKey2());

myexamples[1] = new MyExampleTest();
myexamples[1].setQueryKey1(EnumValues.BALAJI);
myexamples[1].setQueryKey2(EnumValues.BANG_AND_JANG_KUTTIES);
System.out.println(myexamples[1].getQueryKey1());
System.out.println(myexamples[1].getQueryKey2());

myexamples[2] = new MyExampleTest();
myexamples[2].setServiceType3("BALAJI");
myexamples[2].setServiceType4("IS THE GREATEST");
System.out.println(myexamples[2].getServiceType3());
System.out.println(myexamples[2].getServiceType4());

myexamples[3] = new MyExampleTest();
String[] familyArraysKuttiValues = { "Bang","Jang","Chellam","Kutties" };
myexamples[3].setServiceType5(new String[10]);
myexamples[3].setServiceType6(familyArraysKuttiValues);
String kutties = myexamples[3].getServiceType6()[0] + " and " +
myexamples[3].getServiceType6()[1] + " and " +
myexamples[3].getServiceType6()[2] + " and " +
myexamples[3].getServiceType6()[3];
System.out.println(kutties + " are the best in the world");

}
}

(b)
public class MyExampleTest {

private EnumValues serviceType1;
private EnumValues serviceType2;

public void setQueryKey1(EnumValues serviceType1) {
this.serviceType1 = serviceType1;
}
public void setQueryKey2(EnumValues serviceType2) {
this.serviceType2 = serviceType2;
}
public EnumValues getQueryKey1() {
return serviceType1;
}
public EnumValues getQueryKey2() {
return serviceType2;
}

private String serviceType3;
private String serviceType4;

public String getServiceType3() {
return serviceType3;
}
public void setServiceType3(String serviceType3) {
this.serviceType3 = serviceType3;
}
public String getServiceType4() {
return serviceType4;
}
public void setServiceType4(String serviceType4) {
this.serviceType4 = serviceType4;
}

private String serviceType5[];
private String serviceType6[];

public String[] getServiceType5() {
return serviceType5;
}
public void setServiceType5(String[] serviceType5) {
this.serviceType5 = serviceType5;
}
public String[] getServiceType6() {
return serviceType6;
}
public void setServiceType6(String[] serviceType6) {
this.serviceType6 = serviceType6;
}
}

(c)

import java.io.Serializable;

public enum EnumValues implements Serializable {
   BALAJI("BALAJI"),
   BANG_AND_JANG_KUTTIES("THEY ARE THE WORLD'S GREATEST KUTTIES");

   private String displayName;
 
   private EnumValues(String displayName) {
      this.displayName = displayName;
   }
}

BooleanTest Example

package searchOrange;
import java.util.HashMap;
public class BooleanTest {
public static void main(String[] args) {
boolean value1=false;
HashMap hmap1 = new HashMap();
hmap1.put("balaji-1",value1?Boolean.TRUE:Boolean.FALSE);
System.out.println("balaji-1 " + hmap1.get("balaji-1"));
//if the boolean value is false
//its taking in only the second value

hmap1.put("balaji-2",value1?Boolean.FALSE:Boolean.TRUE);
System.out.println("balaji-2 " + hmap1.get("balaji-2"));
//if the boolean value is false
//its taking in only the second value


boolean value2=true;
HashMap hmap2 = new HashMap();
hmap2.put("balaji-3",value2?Boolean.FALSE:Boolean.TRUE);
System.out.println("balaji-3 " + hmap2.get("balaji-3"));
//if the boolean value is true
//its taking in only the first value 

hmap2.put("balaji-4",value2?Boolean.TRUE:Boolean.FALSE);
System.out.println("balaji-4 " + hmap2.get("balaji-4"));
//if the boolean value is true   
//its taking in only the first value
}
}

Answer : 

balaji-1 false
balaji-2 true
balaji-3 false
balaji-4 true

BECAUSE THE VALUE OF BOOLEAN IS CHECKED FIRST AS TRUE AND THEN AS FALSE IN THE BOOLEAN CONDITION ITSELF 

AS IN ANYCASE WITH ANY IF ELSE CONDITION...

Saturday, March 23, 2019

.ExceptionInInitializerError example

public class ExceptionInInitializerError {
    private static String test = null;
    static {
         test = test.substring(0, 5);
    }
    public static void main(String[] args) {
    }
}

Answer : 

When you get an error upfront in the static block itself its called as ExceptionInInitializerError 

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at com.mkyong.ExceptionInInitializerError.<clinit>(ExceptionInInitializerError.java:21)

ResourceBundle Example

import java.util.Locale;
import java.util.ResourceBundle;
public class Application {
public static void main(String[] args) {
System.out.println("Current Locale: " + Locale.getDefault());
ResourceBundle mybundle = ResourceBundle.getBundle("Labels");
System.out.println("Say you are the best in US English: " + mybundle.getString("you-are-the-best"));
Locale.setDefault(new Locale("zh", "CN"));
System.out.println("Current Locale: " + Locale.getDefault());
mybundle = ResourceBundle.getBundle("Labels");
System.out.println("Say you are the best in Chinese: " + mybundle.getString("you-are-the-best"));
}
}

Labels_en_US.properties 

you-are-the-best = you are Not Only the Best But the Greatest of All Time

Labels_zh_CN.properties 

you-are-the-best = I Don't Know Chinese




Friday, March 22, 2019

IntegerArrayPrint Example

public class IntegerArrayPrint {
    public static void main(String[] args)
    {
        int intArr1[] = { 10, 20, 15, 22, 35 };
        for(int array : intArr1) {
        System.out.println(array);
        }
    }
}

Saturday, March 16, 2019

Character Array Example

import java.util.*;
public class charPrint {
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
a1.add("12345");a1.add("11112");a1.add("11113");
a1.add("11114");a1.add("11115");a1.add("11116");
Object[] ref = a1.toArray();
System.out.println(ref[0]);
String refS = ref[0].toString();
char[] array = refS.toCharArray();
for(char c : array) {
System.out.println(c);
}
for(int i=0;i<array.length;i++) {
System.out.println(array[i]);
}
}
}

Thursday, March 14, 2019

SoftReference Example

        SoftReference mPropsMap = null;
        SoftReference ourRef = mPropsMap;
Map propsMap = ourRef == null ? null : ((Map) ourRef.get());
propsMap = new HashMap(22);
propsMap = Collections.unmodifiableMap(propsMap);
mPropsMap = new SoftReference(propsMap);

StringBuffer.setCharAt Example

StringBuffer buf = new StringBuffer();
buf.append("abcd");
buf.setCharAt(3, 'E');
System.out.println(buf.toString());

Collections.unmodifiableMap(map) Example

package com.javainuse;
import java.util.*;
public class JavaStringBuffer {
public static  <T> void main(String[] args) {

             Map<Integer, String> map = new HashMap<>();
             map.put(1, "one");
             map.put(2, "two");
             System.out.println("Initial Unmodifiable Map: "+map);
             Map<Integer, String> map2 = Collections.unmodifiableMap(map);
             map2.put(3, "three");
             System.out.println(map);

}
}

Reference Example

import java.util.*;
public class JavaStringBuffer {
public static  <T> void main(String[] args) {
     
             T Reference = (T) "values";
             System.out.println(Reference);
        
}
}

Sunday, February 3, 2019

How to convert String.class to Class ?

Class<?> classes = String.class;
System.out.println("Classes.getName() is " + classes.getName());
System.out.println("Classes.getSimpleName() " + classes.getSimpleName());
System.out.println("Classes.toGenericString " + classes.toGenericString());

Answer :
-----------

Classes.getName() is java.lang.String
Classes.getSimpleName() String
Classes.toGenericString public final class java.lang.String



Saturday, February 2, 2019

What does > symbol mean in html ?

<body>
What is this ?: {{>status}}
</body>

Answer :
-----------
> symbol means to open the folder, say if it is inside the template folder then open the template folder and if it is status then it means open the status.html page inside the template folder

Exception in thread "main" java.lang.IllegalStateException

When you have input like this below

ApplicationContext applicationContext = new ClassPathXmlApplicationContext();

you get output like

Exception in thread "main" java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

Hence you have to define the bean in the ClassPathXmlApplicationContext("SpringBeans.xml");

Thursday, January 31, 2019

list methods example

package com.concretepage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;

public class Listmethods{
public static void main(String[] args) {

LinkedList<String> list = new LinkedList<String>();
list.add("A");
list.add("B");

list.addFirst("D");
list.add(2,"E");
list.addLast("C");
list.add("F");
list.add("G");

System.out.println("Linked List " + list);
System.out.println(list.size());
String returns = list.get(2);
System.out.println(returns);

Collection<String> ref = new ArrayList<String>();
ref.add("H");
ref.add("I");
ref.add("J");
ref.add("K");
ref.add("L");
list.addAll(1,ref);
System.out.println(list);
}
}

Collections.addAll example

package com.concretepage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class TestOneYes {
public static void main(String[] args) {

List<String> list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.add("F");
boolean b = Collections.addAll(list, "1", "2", "3");
System.out.println(b);
System.out.println("arrayList after Operation " + list);

}
}

Collectors.toList() example

package com.concretepage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class TestOneTwo {
public static void main(String[] args) {

String a[] = new String[] {"A", "B", "C", "D", "E", "F", "G", "H", "B", "C"};
List<String> ref = Arrays.asList(a);
System.out.println(ref);

       // input list with duplicates
        List<Integer> list = new ArrayList<>(
            Arrays.asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));
            // Print the Arraylist
        System.out.println("ArrayList with duplicates: "
                           + list);
 
        // Construct a new list from the set constucted from elements
        // of the original list
        List<Object> newList = list.stream().distinct().collect(Collectors.toList());
        // Print the ArrayList with duplicates removed
        System.out.println("ArrayList with duplicates removed: " + newList);
       
        System.out.println();System.out.println();System.out.println();
        Stack<String> reference = new Stack<>();
        reference.push("A");
        reference.push("B");
        reference.push("C");
        reference.push("D");
        reference.push("E");
       
        System.out.println("The elements at the top of stack is 1 " + reference.pop());
        System.out.println("The elements at the top of stack is 2 " + reference.peek());
        System.out.println("The elements at the top of stack is 3 " + reference.peek());
        System.out.println("The elements at the top of stack is 4 " + reference);

}
}

arraylist indexOf, lastIndexOf example

package com.concretepage;

import java.util.ArrayList;

public class TestOneFour {
public static void main(String[] args) {
    ArrayList<Object> ref = new ArrayList<>();
    ref.add(1);ref.add(2);ref.add("Three");ref.add(4);ref.add(5);
    ref.add(6);ref.add("Seven");ref.add("Three");ref.add("Three");ref.add(8);ref.add(9);
    System.out.println(ref.indexOf("Three"));
    System.out.println(ref.lastIndexOf("Three"));
    System.out.println(ref.indexOf("Seven"));
}
}

>> operator example

package com.concretepage;
public class Test {
public static void main(String args[])  {
   int a = 10;
   System.out.println(a+a>>1); 
}
}

subclass parent constructor protected example

package com.concretepage;
public class SubClassParentConstructorProtected {
public static void main(String[] args) {
SubClassParentConstructorProtected sub = new SubClassParentConstructorProtected();
ParentConstructorProtected pub = new ParentConstructorProtected();
}
}

subclass example

package com.concretepage;
class SuperClass {
void method1() {
System.out.println("i am method1 of superclass");
}
void method2() {
System.out.println("i am method2 of superclass");
}
void method3() {
System.out.println("i am method3 of superclass");
}
}
public class SubClass extends SuperClass {
void method1() {
System.out.println("i am method1 of subclass");
}
void method2() {
System.out.println("i am method2 of subclass");
}
void method3() {
System.out.println("i am method3 of subclass");
}
public static void main(String[] args) {
SuperClass superclass = new SuperClass();
superclass.method1();
superclass.method2();
superclass.method3();
}
}

subclass of abstract class example

package com.concretepage;

public abstract class SubAbstractClass extends Abstractclass {

@Override
void method3() {
// TODO Auto-generated method stub
super.method3();
}
public static void main(String[] args) {
// TODO Auto-generated method stub

}

@Override
void method2() {
// TODO Auto-generated method stub

}

}


package com.concretepage;

public abstract class Abstractclass {
abstract void method1();
abstract void method2();
void method3() {
System.out.println("hi i am from method3");
}

}

reducing the visibility of the overriddenmethod subclass

package com.concretepage;
public class ReducingtheVisibilityOftheOverriddenMethod {

public void method1() {
System.out.println("I am inside method1");
}

}


package com.concretepage;
public class ReducingtheVisibilityOftheOverriddenMethodSubClass extends ReducingtheVisibilityOftheOverriddenMethod {
private void method1() {
}
}

overriding static methods example

package com.concretepage;
public class OverridingStaticMethods {
public static void method1() {
System.out.println("I am inside method1");
}
public static void method1(String a) {
    System.out.println("I am inside method1 of String2 " + a);
}
void method2() {
System.out.println("I am inside method2");
}
public static void main(String[] args) {
method1();
method1("Additional Comments");
}
}

overriding static methods subclass example

package com.concretepage;

public class OverridingStaticMethodsSubClass extends OverridingStaticMethods {
public void method1() {
}
@Override
void method2() {
// TODO Auto-generated method stub
super.method2();
}
}


package com.concretepage;
public class OverridingStaticMethods {
public static void method1() {
System.out.println("I am inside method1");
}
public static void method1(String a) {
    System.out.println("I am inside method1 of String2 " + a);
}
void method2() {
System.out.println("I am inside method2");
}
public static void main(String[] args) {
method1();
method1("Additional Comments");
}
}

parent constructor protected example

package com.concretepage;
public class ParentConstructorProtected {
protected ParentConstructorProtected() {
}
}

overloaded constructor compilation error

package com.concretepage;

public class OverloadConstructor {
/*
public OverloadConstructor() {
}*/
public OverloadConstructor(String a) {
System.out.println("Overloaded Constructors Value is " + a);
}
public static void main(String[] args) {
OverloadConstructor overloadConstructor1  = new OverloadConstructor("String");
OverloadConstructor overloadConstructor2  = new OverloadConstructor();
}
}

vector elementAt, hashtable.get example

package com.concretepage;

import java.util.Hashtable;
import java.util.Vector;

public class NewTest {

public static void main(String[] args) {

int arr[] = new int[] {1,2,3,4};
Vector<Integer> vector = new Vector();
Hashtable<Integer,String> hashtable = new Hashtable();
vector.addElement(1);
vector.addElement(2);
hashtable.put(1,"GODS");
hashtable.put(2, "OF");
hashtable.put(3, "I WILL");

System.out.println(arr[0]);
System.out.println(vector.elementAt(0));
System.out.println(vector.size());
System.out.println(hashtable.get(2));

}

}

System.arraycopy example

package com.concretepage;
import java.lang.*;
public class NewClass
{
 public static void main(String[] args)
 {
     int s[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
     int d[] = { 15, 25, 35, 45, 55, 65, 75, 85, 95, 105};

     int source_arr[], sourcePos, dest_arr[], destPos, len;
     source_arr = s;
     sourcePos = 3;
     dest_arr = d;
     destPos = 5;
     len = 3;
   

     // Print elements of source
     System.out.print("source_array : ");
     for (int i = 0; i < s.length; i++)
         System.out.print(s[i] + " ");
     System.out.println("");

     System.out.println("sourcePos : " + sourcePos);
     
     // Print elements of source
     System.out.print("dest_array : ");
     for (int i = 0; i < d.length; i++)
         System.out.print(d[i] + " ");
     System.out.println("");
     
     System.out.println("destPos : " + destPos);
     
     System.out.println("len : " + len);
     
     // Use of arraycopy() method
     System.arraycopy(source_arr, sourcePos, dest_arr, 
                                         destPos, len);
     
     // Print elements of destination after
     System.out.print("final dest_array : ");
     for (int i = 0; i < d.length; i++)
         System.out.print(d[i] + " ");
 }
}

Multiple ArrayList of ArrayLists example

package com.concretepage;
import java.util.*;
public class MultipleArrayListOfArrayListsJava {
public static void main(String[] args) {

ArrayList<ArrayList<String>> arrayListBasicExample = new ArrayList<ArrayList<String>>();

ArrayList<ArrayList<ArrayList<String>>> arrayListMain = new ArrayList<ArrayList<ArrayList<String>>>();
ArrayList<ArrayList<String>> arrayList1 = new ArrayList<ArrayList<String>>();

ArrayList<String> arrayList1Sub1 = new ArrayList<String>();
arrayList1Sub1.add("Balaji"); arrayList1Sub1.add("Padma");
arrayList1Sub1.add("Appa"); arrayList1Sub1.add("Amma");
arrayList1.add(arrayList1Sub1);
arrayListMain.add(arrayList1);
ArrayList<String> arrayList1Sub2 = new ArrayList<String>();
arrayList1Sub2.add("BALAJI");   arrayList1Sub2.add("PADMA");
arrayList1Sub2.add("APPA");     arrayList1Sub2.add("AMMA");
arrayList1.add(arrayList1Sub2);
arrayListMain.add(arrayList1);

for(int i=0;i<arrayListMain.size();i++) {
System.out.println("arrayListMain Size is " + arrayListMain.size());
for(int j=0;j<arrayList1.get(i).size();j++) {
System.out.println("arrayList1.get(i).size is " + arrayList1.get(i).size());
System.out.println("arrayList1.get(i).get(j) is " + arrayList1.get(i).get(j));
}
}

}
}

Lambda example

package com.concretepage;
import java.util.HashMap;
import java.util.Map;
public class MapLamdaTest {
    public static void main(String[] args) {
        Map<String, Boolean> booleanMap = new HashMap<String, Boolean>(2);
        booleanMap.put("A", Boolean.valueOf("true"));booleanMap.put("B", Boolean.FALSE);booleanMap.put("C", Boolean.TRUE);
        booleanMap.put("D", true);booleanMap.put("E", Boolean.valueOf("true"));booleanMap.put("F", Boolean.valueOf("false"));
        System.out.println("Hashmap created, here are the values");
        // Print out the map
        booleanMap.forEach((m, l) -> System.out.println(m + " " + l));
        booleanMap.replaceAll((m,l) -> Boolean.FALSE);
        System.out.println("Hashmap replaceAll, here are the values");
        long startTime = System.currentTimeMillis();
        // Print out the modified values
        booleanMap.forEach((m,l) -> System.out.println(m + " " + l));
        long stopTime = System.currentTimeMillis();
        System.out.println("Replace took time " + (stopTime-startTime));
    }
}

Complex example, he he he :)

package com.concretepage;
public class Main {
public static void main(String[] args) {
Complex complex1 = new Complex(10, 15);
Complex complex2 = new Complex(complex1);
Complex complex3 = complex2;
System.out.println(complex2);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.concretepage;
public class Complex {
private double re, im;
public Complex(double re, double im) {
this.re = re;
this.im = im;
}
    Complex(Complex c){
    System.out.println("Copy Constructor called");
    re = c.re;
    im = c.im;
    }
    @Override
    public String toString() {
    return "(" + re + " + " + im + "i)";
    } 
}

list of arrays example

package com.concretepage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map.Entry;
import java.util.Set;

public class ListOfArrays {
public static void main(String[] args) {

List<String[]> list = new ArrayList<String[]>();
String[] s1 = {"A","B","C"};
String[] s2 = {"D","E","F"};
list.add(s1);
list.add(s2);
for(String ref[] : list) {
System.out.println(Arrays.toString(ref));
}    
String[] s1ArrayObject = new String[]{"a","b","c"};
String[] s2ArrayObject = new String[]{"d","e","f"};
HashMap<String[],String[]> hmap = new HashMap();
hmap.put(s1ArrayObject,s2ArrayObject);
Set<Entry<String[], String[]>> ref = hmap.entrySet();
ArrayList<Entry<String[], String[]>> arrayListReference = new ArrayList<>(ref);
Iterator<Entry<String[], String[]>> refIterator = arrayListReference.iterator();
while(refIterator.hasNext()) {
Entry<String[],String[]> refIteratorAgain = refIterator.next();
String[] keyValue = refIteratorAgain.getKey();
String[] valueIs  = refIteratorAgain.getValue();
System.out.println("keyValue is " + Arrays.toString(keyValue));
System.out.println("value is    " + Arrays.toString(valueIs));
}

HashMap<String[], String[]> hmapReferenceAgain = new HashMap<>();
hmapReferenceAgain.put(new String[]{"a","b","c"}, new String[]{"d","e","f"});
hmapReferenceAgain.put(new String[]{"g","h","i"}, new String[]{"j","k","l"});
hmapReferenceAgain.put(new String[]{"m","n","o"}, new String[]{"p","q","r"});
hmapReferenceAgain.put(new String[] {"s","t","u"}, new String[] {"v","w","x"});

Collection<String[]> StringArrayReferece  = hmapReferenceAgain.values();
ArrayList<String[]> arrayList = new ArrayList<String[]>(StringArrayReferece);
Iterator<String[]> referenceAgain = arrayList.iterator();
while(referenceAgain.hasNext()) {
System.out.println(Arrays.toString(referenceAgain.next()));
}
ArrayList<String[]> arrayListReferenceAgain = new ArrayList<String[]>(StringArrayReferece);
ListIterator<String[]> referenceAgainNow = arrayListReferenceAgain.listIterator();
while(referenceAgainNow.hasNext()) {
System.out.println(Arrays.toString(referenceAgainNow.next()));
}
System.out.println();
List<Integer> listNow = new ArrayList<Integer>();
listNow.add(1);
listNow.add(2);
listNow.add(3);
listNow.add(4);
listNow.add(5);
Object[] referenceArrayList = listNow.toArray();
    for(Object obj : referenceArrayList) {
    System.out.println(obj);
    }
    System.out.println();   
    /*
    Integer[] array = new Integer[listNow.size()];
    array = listNow.toArray(array);
    for(Integer i : array) {
    System.out.println(i);
    }*/    
    Integer[] referenceList = new Integer[listNow.size()];
    for(int k=0;k<referenceList.length;k++) {
    System.out.println(referenceList[k]);
    }
   
    referenceList = listNow.toArray(referenceList);
    for(Integer i : referenceList) {
    System.out.println("i Value is " + i);
    }
}
}

List SplitIterator example

package com.concretepage;
import java.lang.ref.Reference;
import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;

public class ListNowSplitIterator {
public static void main(String[] args) {

List l1 = new ArrayList();
l1.add(1);
l1.add(2);
l1.add(3);
l1.add(4);

Spliterator<String> reference = l1.spliterator();
System.out.println(reference.estimateSize());
System.out.println(reference.getExactSizeIfKnown());

}
}

List as array java

package com.concretepage;
import java.util.ArrayList;
import java.util.List;

public class ListArrayJava {
public static void main(String[] args) {

List<String> l1 = new ArrayList<>();
l1.add("One"); l1.add("Two");
l1.add("Three"); l1.add("Four");

List<String> l2 = new ArrayList<>();
l2.add("OneAgain"); l2.add("TwoAgain");
l2.add("ThreeAgain"); l2.add("FourAgain");

List<String>[] listArray = new List[2];
listArray[0] = l1;
listArray[1] = l2;

for(int i = 0; i<listArray.length; i++) {
System.out.println(listArray[i]);
}
}
}

interface methods as private

package com.concretepage;
public interface InterfaceMethodsAsPrivate {
abstract void method1();
}

What will happen if you import a package twice ?

package com.concretepage;
import java.lang.*;
import java.lang.*;

public class ImportingTheJavaLangPackageTwice {
public static void main(String[] args) {
       System.out.println("I have exploded Out of this Package");
}
}

Answer :
-----------
Well nothing will happen

HashMap to CharArray example

package com.concretepage;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Iterator;
import java.util.LinkedHashMap;
public class HashMaptoCharArray {
public static void main(String[] args) {
HashMap hmap = new LinkedHashMap<>();
hmap.put("One", "1");hmap.put("Two", "2");
hmap.put("Three", "3");hmap.put("Four","4");
hmap.remove("One", "1");
Set<Entry<String, String>> reference = hmap.entrySet();
Iterator<Entry<String, String>> iterator1 = reference.iterator();
while(iterator1.hasNext()) {
Entry<String, String> entry1 = iterator1.next();
System.out.print(entry1.getValue());System.out.print(" ");System.out.print(entry1.getKey());
System.out.println();
}
System.out.println("************************************************************************");

//hmap.remove("Two");
//hmap.remove("Two","2");
//hmap.replace("Two", "22");
//hmap.replace("Two", "2", "22000rs");
Set<Entry<String, String>> referenceAgain = hmap.entrySet();
Iterator<Entry<String, String>> iterator2 = referenceAgain.iterator();
while(iterator2.hasNext()) {
Entry<String, String> entry2 = iterator2.next();
System.out.print(entry2.getValue());System.out.print(" ");System.out.print(entry2.getKey());
System.out.println();
}
System.out.println("************************************************************************");
}
}

HashMap to ArrayList Example

package com.concretepage;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapToArrayListExample {

public static void main(String[] args) {

        HashMap<String, String> studentPerformanceMap = new HashMap<String, String>();
        //Adding elements to HashMap
        studentPerformanceMap.put("John Kevin", "Average");
        studentPerformanceMap.put("Rakesh Sharma", "Good");
        studentPerformanceMap.put("Prachi D", "Very Good");       
        studentPerformanceMap.put("Ivan Jose", "Very Bad");
        studentPerformanceMap.put("Smith Jacob", "Very Good");
        studentPerformanceMap.put("Anjali N", "Bad");
       
        //Getting Set of keys
       
        Set<String> keySet = studentPerformanceMap.keySet();
ArrayList<String> listOfKeyValues = new ArrayList<String>(keySet);
for(String keys : listOfKeyValues) {
//System.out.println(keys);
}

Collection<String> ref = studentPerformanceMap.values();
    ArrayList<String> values1 = new ArrayList<String>(ref);
    for(String values2 : values1) {
    System.out.println(values2);
    }

Set<Entry<String,String>> home = studentPerformanceMap.entrySet();
ArrayList<Entry<String, String>> homeRef = new ArrayList<Entry<String,String>>(home);

for(Entry<String, String> reference : homeRef) {
System.out.println("reference.getKey " +reference.getKey() + "--" + reference.getValue());
}

}


}

HashMap putIfAbsent example

package com.concretepage;
import java.util.HashMap;
public class HashMapPutIfAbsentExample {
public static void main(String[] args) {
HashMap<Integer, String> map = test();
String mapAgain = map.putIfAbsent(4,"Five");
System.out.println(mapAgain);
String result = map.putIfAbsent(73654,"Darwin Bocalbos");
System.out.println(result);
}
private static HashMap<Integer, String> test(){
HashMap<Integer,String> hmapAgain = new HashMap();
hmapAgain.put(1,"One");
hmapAgain.put(2,"Two");
hmapAgain.put(3,"Three");
hmapAgain.put(4,"Four");
hmapAgain.put(73654,"Shyras Travels");
return hmapAgain;
}
}

HashMapofHashMapValue example

package com.concretepage;
import java.util.*;
import java.util.Map.Entry;

public class HashMapofHashMapValue {
public static void main(String[] args) {
HashMap hmap = new HashMap();
hmap.put("One", "ABCD"); hmap.put("Two", "EFGH");
hmap.put("Three", "IJKL");  hmap.put("Four", "MNOP");
HashMap<String, HashMap> hmapObject = new HashMap<>();
hmapObject.put("One-One", hmap);
System.out.println(hmapObject.get("One-One"));
Set<Entry<String, HashMap>> reference = hmapObject.entrySet();
Iterator<Entry<String, HashMap>> referenceIterator = reference.iterator();
while(referenceIterator.hasNext()) {
Entry<String, HashMap> entry = referenceIterator.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
HashMap hmapObjectValue = entry.getValue();
System.out.print(hmapObjectValue.get("One")+" ");
System.out.print(hmapObjectValue.get("Two")+" ");
System.out.print(hmapObjectValue.get("Three")+" ");
System.out.print(hmapObjectValue.get("Four")+" ");
}
System.out.println();
HashMap hmapSecondObject = new HashMap();
hmapSecondObject.put("One", "ABCD");
hmapSecondObject.put("Two", "EFGH");
hmapSecondObject.put("Three","IJKL");
hmapSecondObject.put("Four", "MNOP");
HashMap<String, HashMap<String, ArrayList>> hmapObjectsSecondObject = new HashMap<>();
hmapObjectsSecondObject.put("One-One", hmapSecondObject);
System.out.println("hmapObjectsSecondObject Value is " + hmapObjectsSecondObject.get("One-One"));

HashMap hmapThirdObject = new LinkedHashMap<>();
List a1 = new ArrayList();
a1.add("ABCD");a1.add("EFGH");a1.add("IJKL");a1.add("MNOP");
hmapThirdObject.put("One", a1);hmapThirdObject.put("Two", a1);
hmapThirdObject.put("Three", a1);hmapThirdObject.put("Four", a1);
HashMap<String, HashMap<String, ArrayList>> hmapObjectsThirdObject = new HashMap<>();
hmapObjectsThirdObject.put("One-One", hmapThirdObject);
System.out.println("hmapObjectsThirdObject Value is " + hmapObjectsThirdObject.get("One-One"));
HashMap hmapObjectsThirdObjectReference = hmapObjectsThirdObject.get("One-One");
Set<Entry<String, ArrayList>> set = hmapObjectsThirdObjectReference.entrySet();
Iterator<Entry<String, ArrayList>> itr = set.iterator();
while(itr.hasNext()) {
Entry<String, ArrayList> itrReference = itr.next();
System.out.println(itrReference.getKey());
System.out.println(itrReference.getValue());
Iterator objectValue = itrReference.getValue().iterator();
while(objectValue.hasNext()) {
System.out.println(objectValue.next());
}
}
}
}

HashMap NullKey example

package com.concretepage;
import java.util.HashMap;
public class HashMapNullKey {
public static void main(String[] args) {
HashMap hmap = new HashMap();
hmap.put(null, "1");
System.out.println(hmap.get(null));
}
}

HashMap into ArrayList example

package com.concretepage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapIntoArrayList {
public static void main(String[] args) {
HashMap<String, String> hmap = new HashMap<>();
hmap.put("One", "One");
hmap.put("Two", "Two");
hmap.put("Three", "Three");
hmap.put("Four", "Four");
Set<Entry<String,String>> ref = hmap.entrySet();
ArrayList<Entry<String, String>> refAgainArrayList = new ArrayList<Entry<String, String>>(ref);
Iterator<Entry<String, String>> refIterator = refAgainArrayList.iterator();
while(refIterator.hasNext()) {
     System.out.println("refIterator.next() " + refIterator.next());
}
}
}

HashMap CollectionValues example

package com.concretepage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
public class HashMapCollectionValues {
public static void main(String[] args) {
HashMap<String, String> hmap = new HashMap<>();
hmap.put("One", "One");
hmap.put("Two", "Two");
Collection<String> ref = hmap.values();
ArrayList<String> arrayListAgain = new ArrayList<>(ref);
Iterator<String> refAgain = arrayListAgain.iterator();
while(refAgain.hasNext()) {
System.out.println(refAgain.next());
}
}
}

HashMap to ArrayList example

package com.concretepage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapArrayListAgain {

public static void main(String[] args) {
    HashMap<String, String> map = new HashMap<String, String>();
map.put("1", "1");
map.put("2", "2");
map.put("3", "3");
map.put("4", "4");
//Getting the Set of entries
Set<Entry<String, String>> entrySet = map.entrySet();
//Creating an ArrayList Of Entry objects
ArrayList<Entry<String, String>> listOfEntry = new ArrayList<Entry<String,String>>(entrySet);


HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("1", "1");
map1.put("2", "2");
map1.put("3", "3");
map1.put("4", "4");
//Getting the Set of entries
Set<Entry<String, String>> entrySet1 = map.entrySet();
//Creating an ArrayList Of Entry objects
ArrayList<Entry<String, String>> listOfEntry1 = new ArrayList<Entry<String,String>>(entrySet);
Iterator itr = listOfEntry1.iterator();
while(itr.hasNext()) {
Object ref = itr.next();
    System.out.println(ref);


}

}

}

HashMap to ArrayList

package com.concretepage;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

public class HashMapArrayList {

public static void main(String[] args) {


HashMap<String, String> map = new HashMap<String, String>();
map.put("1", "1");
map.put("2", "2");
map.put("3", "3");
map.put("4", "4");
//Getting Collection of values from HashMap
       
Collection<String> values = map.values();
       
//Creating an ArrayList of values
       
ArrayList<String> listOfValues = new ArrayList<String>(values);


Iterator itr = listOfValues.iterator();

while(itr.hasNext()) {
System.out.println(itr.next());
}



}


}

vector.subList example

package com.concretepage;

import java.util.*;

public class VectorSubList{
    public static void main(String[] args)
    {
    Vector<String> vector = new Vector<String>();
    vector.add("Welcome");
    vector.add("to");
    vector.add("House");
   
    List<String> list = new ArrayList<String>();
    list = vector.subList(2, 3);
    System.out.println(list);
    }
}

final value into parameter

package com.concretepage;

public class finalValueIntoParameter {

void method1(String j) {
j = j + "1";
System.out.println(j);
}
void method2(final StringBuffer sb) {
StringBuffer sb2 = sb.append("100 again");
//sb2 = sb.append("100 again");
System.out.println(sb);
System.out.println(sb2);
}

public static void main(String[] args) {
    /*
        String str = "journaldev";

//string to char array
char[] chars = str.toCharArray();
System.out.println(chars.length);

//char at specific index
char c = str.charAt(2);
System.out.println(c);

//Copy string characters to char array
char[] chars1 = new char[7];
str.getChars(0, 3, chars1, 0);
System.out.println(chars1);
*/


     
        String s1 = "Welcome to Javatpoint"; 
        char[] ch = s1.toCharArray(); 
        int len = ch.length; 
        System.out.println("Char Array length: " + len); 
        System.out.println("Char Array elements: "); 
        for (int i = 0; i < len; i++) { 
            System.out.println(ch[i]); 
        }
 
/*
finalValueIntoParameter f = new finalValueIntoParameter();
    f.method1("100");
    f.method2(new StringBuffer("121"));
*/




}

}