第一个 一定错  sub继承base   
第2个  默认为0

解决方案 »

  1.   

    第一个:可以向上转型,但不能向下转型。
    如果
    Sub a=new Base();
    Base b=(Sub)a;
    是可以的
    想想:
    class 动物:
    class 人 extends 动物;
    如果
    动物 a=new 动物();
    人 b=(人)a;
    那么b就缺少人的部分,不能正常操作
    但是
    人 a=new 人();
    动物 b=(动物)a;
    b就指向a中的动物部分,可以正常操作第二个:类里的变量都有初值,但方法里的变量就没有初值。
      

  2.   

    你对 Java 的理解可能没错,但是对英文的理解可能有错。 :)
    1、Without the cast to sub you would get a compile time error. 
    参考译文:如果没有(显式)到 sub 的 cast 的话,你将会得到一个编译错误。
    注意 would 的用法,虚拟语态。这句话的意思其实是,例子中因为有 cast 到 sub,所以不会有编译错误!2、Class level variables are always initialised to default values. In the case of an int this will be 0. Method level variables are not given default values and if you attempt to use one before it has been initialised it will cause the Error Variable i may not have been initialized 
    参考译文:类级别的变量总是被初始化成缺省值。在这种情况(即例子情况)下 int 会是 0。在类的方法一级的变量是不会被赋初值的,如果你在变量被初始化前使用它,将会得到一个错误:变量 i 可能未被初始化。看样子你还是应该同时也补习一下英文呀,否则小心理解得正好相反可就糟了。 :)-----------------------------------------------
    public class t 
    {
    static int i;
    public static void main(String argv[])
    {
      System.out.println(i);
      
    }
    }
    Class level variables are always initialised to default values. In the case of an int this will be 0. Method level variables are not given default values and if you attempt to use one before it has been initialised it will cause the Error Variable i may not have been initialized 
      

  3.   

    to:luodi(无知者无畏) 
    是这样的,SCJP的标准答案是:
    1 选择a. error variable i may not have been initialized
    2 选择c. runtime exception
    这难道不是说是出错的吗
      

  4.   

    可是我没看见你说的答案在哪呀?Runtime Exception 如果是说 (sub) 的那个,当然是对的,具体一点的Exception 应该是 ClassCastException,因为强制(cast)只能欺骗编译器,运行时一样可以检查出不是sub 类型也不是 sub 类型的子类,当然不能转换。另外那个再具体一点我才能搞清楚你说什么。
      

  5.   

    原题如下:
    1、what will happen if you attempt to compile and run the following code:
    class Base {}
    class Sub extends Base {}
    class Sub2 extends Base {}public class t 
    {public static void main(String argv[])
    {
      Base b = new Base();
      Sub s = (Sub)b;
      
    }
    }
    a.Compile and run without error 
    b.Compile time Exception
    c.Runtime Exception (answer:right)2、what will happen when you compile the following code:
    public class t 
    {
    static int i;
    public static void main(String argv[])
    {
      System.out.println(i);
      
    }
    }
    a.error variable i may not have been initialized (answer:right)
    b.null
    c.1
    d.0
      

  6.   

    2题应该是d阿,member variable都可以有一个缺省的初始化值的。第一道题就不是太明白,这不是narrow casting 么 
      

  7.   

    1 是c 因为不能把父类cast成子类,否则虽然通过了编译,但
    会导致一个 ClassCastException(是RunTimeException的子类)2我作了试验了,是d书上(咬咬牙)
    是不是书上错了。。嘻嘻
      

  8.   

    我可以100%肯定:
    第一题答案是
    c.Runtime Exception (answer:right)
      因为,子类才能赋值给父类(这当然是对的)这条规则是在runtime时候才体现出来的,编译时只要casting转换正确就行。第二题答案是
    d.0
      因为,如大家所说,i是class variable, 故不仅有缺省值,而且可以直接引用。一般是用t.i引用,但这里是在自己的main函数里,所以可以把t.i简写为i。清楚了吗?
    我刚考完scjp。
    模拟题上有些答案也会错的。
      

  9.   

    谁说基类转型付给子类是不允许的。你试试java的这两个类。Graphics 和 Graphics2D;Graphics2D是Graphics的子类。但是可以这样:(Graphics2D)(new Graphics())不信你试试!!!!!!!!!!!