// count=count++;
// step 1: JVM copy count value to temp
// step 2: count incremented by 1
// step 3: assign temp to count, count reset to 0
public class SelfIncrementTest {
public static void main(String[] args){
int count = 0;
for (int i=0;i<10;i++){
count = count++;
}
System.out.println(count);
}
}
OUTPUT
0
Monday, September 22, 2014
varargs
public class VarargsTest {
public void methodA(String str, Integer... is){
System.out.println("first methodA called");
}
public void methodA(String str, String... strs){
System.out.println("second methodA called");
}
public static void main(String[] args){
VarargsTest vt = new VarargsTest();
vt.methodA("USA", 0);
vt.methodA("USA", "People");
//vt.methodA("USA"); // compile error. compiler does not know which methodA to invoke
//vt.methodA("USA", null); // compile error. null is not any type. compiler does not know which methodA to invoke
}
}
OUTPUT
first methodA called
second methodA called
Ternary type must match
// data types in ternary must match
public class TernaryTest {
public static void main(String[] args){
int i = 80;
String s1 = String.valueOf(i<90?90:100);
String s2 = String.valueOf(i<90?90:100.0); // 90 converted to 90.0
System.out.println("s1="+s1);
System.out.println("s2="+s2);
}
}
OUTPUT
s1=90
s2=90.0
Saturday, September 20, 2014
instanceof
import java.util.Date;
public class InstanceOfTest {
// if no extend or implementation relationship between two objects, it will give compile error.
public static void main(String[] args){
boolean b = false;
b = "a" instanceof String;
System.out.println(b);
b = new String() instanceof String;
System.out.println(b);
b = new Object() instanceof String; // object is not instanceof String
System.out.println(b);
//b = 'A' instanceof Character;
// compile error. 'A' is simple variable and Character is object
b = null instanceof String; // null is not instanceof any types
System.out.println(b);
String s = (String) null; // s is still null even after conversion
System.out.println(s);
b = (String) null instanceof String; // null is still null even after conversion
System.out.println(b);
//b = new Date() instanceof String;
// compile error.
// No extend or implementation relationship between Date and String so compile error.
}
}
OUTPUT
true
true
false
false
null
false
Subscribe to:
Posts (Atom)