这道题目错在哪里啊,就高手讲解
1.What is wrong in the following code?class TempClass {
int i;
public TempClass(int j) {
int i = j;
}
}public class C {
public static void main(String[] args) {
TempClass temp = new TempClass(2);
}
}这道题目为什么选e呢?
2. Analyze the following code. epublic class Test {
int x;
public Test(String t) {
System.out.println("Test");
}
public static void main(String[] args) {
Test test = null;
System.out.println(test.x);
}
}
a) The program has a syntax error because test is not initialized.
b) The program has a syntax error because x has not been initialized.
c) The program has a syntax error because you cannot create an object from the class that defines the object.
d) The program has a syntax error because Test does not have a default constructor.
e) The program has a runtime NullPointerException because test is null while executing test.x
1.What is wrong in the following code?class TempClass {
int i;
public TempClass(int j) {
int i = j;
}
}public class C {
public static void main(String[] args) {
TempClass temp = new TempClass(2);
}
}这道题目为什么选e呢?
2. Analyze the following code. epublic class Test {
int x;
public Test(String t) {
System.out.println("Test");
}
public static void main(String[] args) {
Test test = null;
System.out.println(test.x);
}
}
a) The program has a syntax error because test is not initialized.
b) The program has a syntax error because x has not been initialized.
c) The program has a syntax error because you cannot create an object from the class that defines the object.
d) The program has a syntax error because Test does not have a default constructor.
e) The program has a runtime NullPointerException because test is null while executing test.x
解决方案 »
- 软件开发用java的swing还是C#?
- 高手帮忙用标签格式化下时间,菜鸟感激不尽。
- 关于Ext Table布局的问题
- Jsp中使用Servlet,<img src>其中src= servlet名字,然后可以调用servlet么?
- EJB3.0如何在WebService上发布,当用到EntityManager时,EntityManager出现空值
- 找工作进来,金蝶软件公司j2ee题目
- 那位高手用 SpringIDE开发过Spring程序呀,是否能够将详细的不骤?/
- 关于运行junit出现Class not found 的问题。谢谢
- 求教jsp中一条sql语句问题
- 急,求大神指教审核流程的开发思路
- tomcat5.5 myeclipse8.6 jstl1.1 ssh框架 jsp页面上c:if可以用 而用c:forEach会出错
- struts如何用select实现多条件查询
a) The program has a compilation error because TempClass does not have a default constructor.
b) The program has a compilation error because TempClass does not have a constructor with an int argument.
c) The program compiles fine, but it does not run because class C is not public.
d) The program compiles and runs fine.
e) The program has a run-time error because TempClass does not have a default constructor.
第一题的构造器是想把传过来的参数 int j 的值赋值给 TempClass类得属性 i吧,那应该这样写
class TempClass {
int i;
public TempClass(int j) {
i = j; //这里不能写 int i = j; 这样的话这个i是临时变量,作用域在这个构造方法内,
//根本就不能对属性 i进行赋值
}
}
第二题,a syntax error 的意思是 语法错误,这个是没有语法错误的,所以编译的时候不会报错,但
是在运行的时候会抛出一个java.lang.NullPointerException异常,也就是空指针异常。
null表示不存在,Test test = null;表示声明了一个 Test 类型的引用变量 test,但是 test 没有指
向任何一个对象。
第二题,因为Test test = null;只是定义一个test变量,让它指向一个null对象
所以
a)不对,这里没有语法错误,而且=null也是一种初始化
b)不对,成员变量不初始化,系统会自动初始化,所以也没有语法错误
c)不对,不能从一个定义对象的类中生成对象,没这回事,但实例模式就是很好的例子,所以没有语法错误
d)不对,类可以没有默认构造器,只要new的时候,参数正确就行,这里也没法发生new,所以不存在这个问题
e)正确,因为test为null,也就是没有分配内存,所以不能访问它的属性或方法,所以会报空指针异常
public TempClass(int j),是含int参数的,这个构造器是覆盖掉无参构造器,所以
TempClass temp = new TempClass(2)是能创建TempClass的对象的。综上:第一题没有错误。
对象new完之后的形式应该是什么样的呢? 就是把构造给去掉,然后构造方法里面的参数会返回到对象里面,所以这里在构造里面又定义了个int i,本来这个类就有一个变量I了,这里又创建了一个,呵呵! 其实错误就是重复,编译是可以的,就是执行的时候出错了,编译器是知道这个错误的,但是没法表达,所以就会出现B选项的那个错误,它其实是想说同一个int类型的变量i已经存在了,就好像在项目中连续定义两个一样名字的变量一样! 如果你换成K就不会有这个错误了!
重新说说吧
应该是 由于‘test.x’这句话,你的程序有一个运行时发生的空指针异常
不能运行出结果就发生了运行时错误
由于你的Test未定义而引起的
完全答案 接分 嘿嘿
Test test = null 说明Test类对象都没有在内存中创建
在这里test只是引用 只有new Test();才会在内存中创建对象