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);
        
}
}