public class If1 { static boolean b; public static void main(String [] args) { short hand = 42; if ( hand < 50 && !b ) /* Line 7 */ hand++; if ( hand > 50 ); /* Line 9 */ else if ( hand > 40 ) { hand += 7; hand++; } else --hand; System.out.println(hand); } }
for (int i = 0; i < 4; i += 2) { System.out.print(i + " "); } System.out.println(i); /* Line 5 */
public class Delta { static boolean foo(char c) { System.out.print(c); return true; } public static void main( String[] argv ) { int i = 0; for (foo('A'); foo('B') && (i < 2); foo('C')) { i++; foo('D'); } } }
'B' is printed as it is part of the test carried out in order to run the loop.
'D' is printed as it is in the loop.
'C' is printed as it is in the increment section of the loop and will 'increment' only at the end of each loop. Here ends the first loop. Again 'B' is printed as part of the loop test.
'D' is printed as it is in the loop.
'C' is printed as it 'increments' at the end of each loop.
Again 'B' is printed as part of the loop test. At this point the test fails because the other part of the test (i < 2) is no longer true. i has been increased in value by 1 for each loop with the line: i++;
This results in a printout of ABDCBDCB
public class SwitchTest { public static void main(String[] args) { System.out.println("value =" + switchIt(4)); } public static int switchIt(int x) { int j = 1; switch (x) { case l: j++; case 2: j++; case 3: j++; case 4: j++; case 5: j++; default: j++; } return j + x; } }
int x = 3; int y = 1; if (x = y) /* Line 3 */ { System.out.println("x =" + x); }
int x = l, y = 6; while (y--) { x++; } System.out.println("x = " + x +" y = " + y);
while(true) { //insert code here }
public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 3; z++) { switch (z) { case y: System.out.print("0 "); /* Line 11 */ case x-1: System.out.print("1 "); /* Line 12 */ case x: System.out.print("2 "); /* Line 13 */ } } } }
TreeSet map = new TreeSet(); map.add("one"); map.add("two"); map.add("three"); map.add("four"); map.add("one"); Iterator it = map.iterator(); while (it.hasNext() ) { System.out.print( it.next() + " " ); }
public class Test { private static int[] x; public static void main(String[] args) { System.out.println(x[0]); } }
private static int[]x = new int[5];
private static int[x] declares a static i.e. class level array.
the "new" keyword is the word that actually creates said array.
int[5] in association with the new sets the size of the array. so since the above code contains no new or size decalarations when you try and access x[0] you are trying to access a member of an array that has been declared but not intialized hence you get a NullPointerException at runtime.
import java.util.*; class H { public static void main (String[] args) { Object x = new Vector().elements(); System.out.print((x instanceof Enumeration)+","); System.out.print((x instanceof Iterator)+","); System.out.print(x instanceof ListIterator); } }
package foo; import java.util.Vector; /* Line 2 */ private class MyVector extends Vector { int i = 1; /* Line 5 */ public MyVector() { i = 2; } } public class MyNewVector extends MyVector { public MyNewVector () { i = 4; /* Line 15 */ } public static void main (String args []) { MyVector v = new MyNewVector(); /* Line 19 */ } }
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.