Hi,
I have several questions and hope that someone could help me.
Q.1
//in fileA.java
package p1;
public class A {
protected int i = 10;
public int getI(){return i;}
}
//in fileB.java
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i = a.i*2;
}
public static void main(
String [] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println(a.getI());
}
}
Why the object b cannot access i ?
-----------------------------------------------------------------------------------------------------------------------------
Q.2
public class
Test {
public static void main(String [] args)
{
int ia[][] = {{1,2}, null};
int ij[][] = (int[][])ia.clone();
System.out.println((ia==ij) + " ");
System.out.println(ia[0]==ij[0] && ia[1]==ij[1]);
}
}
Why is the result: true, false. When should I use the object.clone()?
-------------------------------------------------------------------------------------------------------------------------
Q.3
public class Test {
public Test(){
s1 = sM1("1");
}
static String s1 = sM1("a");
String s3 = sM1("2");
{
s1 = sM1("3");
}
static
{
s1 = sM1("b");
}
static String s2 = sM1("c");
String s4 = sM1("4");
public static void main(String [] args)
{
Test it = new Test();
}
private static String sM1(String s)
{
System.out.println(s);
return s;
}
}
Why the results are a b c 2 3 4 1? Why '1' will be the last one to be displayed?
--------------------------------------------------------------------------------------------------------------------------
Q.4
public class Test{
public static void main(String [] args)
{
int x = 4;
int a [][][] = new int[x][x=3][x];
System.out.println(a.length + " " + a[0].length + " " + a[0][0].length);
}
}
Why the answer is 4 3 3 ? Why the last 3rd dimension of array is 3?
-----------------------------------------------------------------------------------------------------------------------------
Q.5
public class Test{
public static void main(String [] args)
{
String str = "111";
boolean a[] = new boolean[1];
if (a[0]) str = "222";
System.out.println(str);
}
}
Why the a[0] represents false?
-----------------------------------------------------------------------------------------------------------------
Thanks for your kind help.
Andrew