论坛首页 Java企业应用论坛

Java容易搞错的知识点-觉得基础扎实的来看

浏览 61879 次
该帖已经被评为良好帖
作者 正文
   发表时间:2011-08-07  
全对。。。
没难度。。
0 请登录后投票
   发表时间:2011-08-08  
最后一个有一点点问题,方法传参数除了基本变量之外,其他的参数传进去的不是地址麽??也就是说b传进去的应该是b的地址,当地址指向的那个值改变的时候,即使跳出方法,也会有影响的..为什么会打印出b呢??还是static的问题??没深究过...这是为什么呢?
0 请登录后投票
   发表时间:2011-08-08  
学习了,有的可以做对,但是不能很正确的解释出来为什么,表示基础还是很不扎实,有好多东东还是不熟悉。希望各位和我一样、比我强的都一起努力学习,毕竟coding需要不断学习,没人全部都会吧
0 请登录后投票
   发表时间:2011-08-08  
学习了,多指教
0 请登录后投票
   发表时间:2011-08-08  
错了2道啊,基础不扎实
0 请登录后投票
   发表时间:2011-08-08   最后修改:2011-08-08
第七题 public static void append(StringBuffer a, StringBuffer b) { 
        a.append(b); 
        b = a; 
    } 
这个方法中的a,b首先只是个局部变量,其次他们的生命周期只是在这个方法中, 最后
java api 5.0中关于append的解释
public StringBuffer append(String str)
    Appends the specified string to this character sequence.
    The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument. If str is null, then the four characters "null" are appended.
    Let n be the length of this character sequence just prior to execution of the append method. Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n; otherwise, it is equal to the character at index k-n in the argument str.
    Parameters:
        str - a string.
    Returns:
        a reference to this object.

0 请登录后投票
   发表时间:2011-08-15  
学习了。不错
0 请登录后投票
   发表时间:2011-08-15  
第一道题
输出结果:2 3 4
原因:由于不存在break语句,只要找到满足条件的case,那语句就不会停止一直执行下去,defult会默认执行。
第二题
结果:
x+y equals z:true
a == z:true
x == hello:false
a == helloworld:false
a == x+y:false
原因分析:个人理解equals比较的是两个字符串的内容是否一致,==比较的是字符串引用中的地址是否一致

第三题
结果:
child static say
child say
原因分析:类继承,方法覆盖

第四题
结果:
7
原因分析:说不清楚,知道是类型转换

第五题
结果:
The value of c :0
原因分析:类的构造方法是不可以有void修饰符的,虽然没有返回值,如果添加上,那么这个方法就不是构造方法了,系统默认会存在一个不带参数的构造方法,
所以那个看起来像构造方法的代码只是一个普通的方法,成员变量c是没有被赋值的,系统会为没有初始化的成员变量进行初始化,int型的初始化为0.

第六题
结果:
first = 2
原因分析:感觉是,原因不知道

第七题
结果:
ab,b
ab,ab
原因分析:画一下内存分析图就比较清楚了,其实就是注意一下,实参向形参传递数值时,传递的是引用的地址,注意这点就可以了。

没看答案,错了3个,还是基础不行啊,哎~
0 请登录后投票
   发表时间:2011-08-16  
学习了,失望了
0 请登录后投票
   发表时间:2011-08-16  
ghl 写道
System.out.println("a == x+y:" + (a == (x + y)));  
这个整错了
String x = "hello";  
String y = "world";
String a = "helloworld";
13行String b = "hello" + "world";
14行String c = x + y;
生成的字节码不一样:
L3
    LINENUMBER 13 L3
    LDC "helloworld"
    ASTORE 4
   L4
    LINENUMBER 14 L4
    NEW java/lang/StringBuilder
    DUP
    ALOAD 1
    INVOKESTATIC java/lang/String.valueOf(Ljava/lang/Object;)Ljava/lang/String;
    INVOKESPECIAL java/lang/StringBuilder.<init>(Ljava/lang/String;)V
    ALOAD 2
    INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
    INVOKEVIRTUAL java/lang/StringBuilder.toString()Ljava/lang/String;
    ASTORE 5

13行 "hello" + "world";2个都是常量,编译器在编译器应该是可以确定的,直接 LDC "helloworld"
14行就是先new StringBuilder了


"hello" + "world"应该是jvm自己预编译的时候直接处理成"helloworld"的
至于(a == (x + y))为什么是false 我比较看重这个证据
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics