Created
June 16, 2012 10:18
-
-
Save YoniTsafir/2940899 to your computer and use it in GitHub Desktop.
Does it compile? questions in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class A { | |
private int x = 7; | |
public A(int x) { | |
this.x = x; | |
System.out.println("A::A, x=" + x); | |
} | |
public int getX() { | |
return x; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
A a = new A(9); | |
System.out.println(a.getX()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Swapper { | |
public static void swap1(int a, int b) { | |
int temp = a; | |
a = b; | |
b = temp; | |
} | |
public static void swap2(int[] a, int[] b) { | |
if (a.length > 0 && b.length > 0) { | |
int temp = a[0]; | |
a[0] = b[0]; | |
b[0] = temp; | |
} | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
int a = 1; | |
int b = 2; | |
int[] c = {a}; | |
int[] d = {b}; | |
Swapper.swap1(a, b); | |
Swapper.swap2(c, d); | |
System.out.println("a=" + a + ", b=" + b + ", c=[" + c[0] +"], d=[" + d[0] +"]"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class A { | |
private int x = 7; | |
public A(int x) { | |
// notice this line... | |
x = x; | |
System.out.println("A::A, x=" + x); | |
} | |
public int getX() { | |
return x; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
A a = new A(9); | |
System.out.println(a.getX()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class A { | |
protected int x = 7; | |
public A(int x) { | |
this.x = x; | |
System.out.println("A::A, x=" + x); | |
} | |
public int getX() { | |
return x; | |
} | |
} | |
public class B extends A { | |
public int getX() { | |
return x + 2; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
B b = new B(); | |
System.out.println(b.getX()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class A { | |
private int x; | |
public A(int x) { | |
this.x = x; | |
System.out.println("A::A, x=" + x); | |
} | |
public int getX() { | |
return x; | |
} | |
} | |
public class B extends A { | |
public B() { | |
super(7); | |
System.out.println("B constructor called"); | |
} | |
public int getX() { | |
return x + 2; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
B b = new B(); | |
System.out.println(b.getX()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class A { | |
private int x; | |
public A(int x) { | |
this.x = x; | |
System.out.println("A::A, x=" + x); | |
} | |
public int getX() { | |
return x; | |
} | |
} | |
public class B extends A { | |
public B() { | |
super(7); | |
System.out.println("B constructor called"); | |
} | |
public int getX() { | |
return super.getX() + 2; | |
} | |
} | |
public class C extends B { | |
public int getX() { | |
return 20; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
int size = 2; | |
A[] arr = new A[size]; | |
arr[0] = new B(); | |
arr[1] = new C(); | |
for (int i = 0; i < size; i++) { | |
System.out.println(arr[i].getX()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class A { | |
protected int num; | |
public A(int x) { | |
num = x; | |
System.out.println("A: num=" + x); | |
} | |
public String toString() { | |
return getClass().getName() + ": " + num + ", " + fruit(); | |
} | |
public abstract String fruit(); | |
} | |
public class B extends A { | |
public B(int bla) { | |
super(bla + 1); | |
System.out.println("B: bla=" + bla); | |
} | |
public String fruit() { | |
return "Banana"; | |
} | |
} | |
public class C extends A { | |
private String fakeFruit; | |
public C(A other) { | |
super(other.num * 2); | |
fakeFruit = other.toString(); | |
System.out.println("C: initialized from " + other); | |
} | |
public String fruit() { | |
return fakeFruit; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
A a1 = new B(8); | |
A a2 = new C(a1); | |
System.out.println(a1); | |
System.out.println(a2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
final int a = 10; | |
double b = a; | |
char c = a; | |
a += 5; | |
System.out.println("a=" + a + ", b=" + b + ", c=" + c); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Main { | |
public static void main(String[] args) { | |
int[] a = new int[5]; | |
final int SIZE = 10; | |
for (int i = 0; i < SIZE; i++) { | |
a[i] = i + 1; | |
} | |
System.out.println(Arrays.toString(a)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Main { | |
private static void increaseArray(int[] arr) { | |
for (int i = 0; i < arr.length; i++) { | |
arr[i] += 1; | |
} | |
} | |
public static void main(String[] args) { | |
int[] arr1 = {1, 2, 3, 4}; | |
int[] arr2 = new int[3]; | |
increaseArray(arr1); | |
increaseArray(arr2); | |
System.out.println(Arrays.toString(arr1)); | |
System.out.println(Arrays.toString(arr2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment