In the following question on Marcus Greens exam #1
1)
public class Pass
{
static int j=20;
public static void main(
String argv[])
{
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}
public void amethod(int x)
{
x=x*2;
j=j*2;
}
}
the answer is 10 & 40. Im wondering how. If primitives are passed by value, meaning the value change that occurs within a method is not reflected outside the method, then why does the value of j become 40 and not remain 20. I can see that the value of i is not changed outside the body as expected, but then why is j treated differently?
2)
If a class B is a subclass of A, then
arent
A a=new B();
and
B b1=(sub) new A();
both legal?
If the above is true, then why does the following give a runtime error?
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx
{
public static void main(String argv[])
{
Base b=new Base();
Sub s=(Sub) b;
}
}
[ January 12, 2003: Message edited by: Shashank Gokhale ]