好好。。多多发些有意义的贴,,,DAY DAY UP

解决方案 »

  1.   

    very good, up  up  up
      

  2.   

    建议楼主以后举例子不要用String。虽然它比较常用,但是他既是immutable又有点flyweight这样特殊的性质很有可能误导许多爱写程序研究但又不了解原理的初学者用StringBuffer吧,这个比较纯粹一点。一点建议
      

  3.   

    可以考虑Calendar类,这个够heavy,呵呵。最近论坛很让人鼓舞啊。BTW,楼主怎么改名字了?
      

  4.   

    推荐大家看两本好书:Effective Java和Practical Java,很多初学者搞不清楚的问题,还有很多我们平时编码不大注意的问题都提到了。
      

  5.   

    to sean_gao(大胃)
    亡灵法师这个词听起来略微专业一点:)
    写这个系列是我的同学Sheepy的主意。欢迎以后多来捧场啊。
      

  6.   

    写的太详细了吧。简单的说
    ==就是比较两个引用指向的内存地址是否相同。

    equals是比较两个引用指向的内存地址的值是否相同。
      

  7.   

    哎,java目前不支持运算符重载和模板机制,是它的一大缺憾
      

  8.   

    ==是进行两个变量的比较
    而equals是两个方法或对象
    string是个方法
    所以不行
      

  9.   

    好好,终于找到组织了~~~~
    有关String的“==”和equals的问题去年我想
    了很久,都得不到解答在这里终于有人可以探
    讨了,如
    class Text {
    public static void main(String args[])
    {String a="1234";
    String b="1234";
    String c=new String("1234");
    if(a==b)System.out.println("a==b");
    if(a==c) System.out.println("a<>c");
    if(a.equals(c))System.out.println("a.equals(c)");}}
    运行结果:
    a==b
    a.equals(c)
    这又是什么问题哪????
    是不是和常量池有关~~~~~
      

  10.   

    To  blacksun8334(乌日):类似String s = "ss";的String声明方法可以保证,对于所有在同一个虚拟机中运行的代码,只要它们包含相同的字符串字面常量,则该对象就会被重用[The Java Language Specification(JLS), Second Edition, 3.10.5]。
      

  11.   

    楼上我没有解释清楚么?对于String对象,如果是按照String s = "some string";这样的形式声明的,如果同一个JVM中恰好有相同内容的String对象,那么这个s指向的就是那个已有的对象。但如果使用String s = new String("some string");这样的语法,那么不管JVM中有没有可以重用的String对象,都将新建一个对象。==操作符判断的是对象引用是否指向同一个对象,而equals方法在String类中的实现是判断String对象的内容是否一致。
      

  12.   

    以下是JLS的一段内容的翻译:3.10.5 String Literals>>3.10.5 字符串文字A string literal consists of zero or more characters enclosed in double quotes. 
    Each character may be represented by an escape sequence.>>字符串文字由0或多个包含在双引号之间的字符组成。每个字符都可以由\序列表示。A string literal is always of type String (§4.3.3). A string literal always 
    refers to the same instance (§4.3.1) of class String.>>所有字符串文字都是String类的。任何字符串文字都是指的String类的那个相同的实例。
    StringLiteral:  // 字符串文字格式
    " StringCharactersopt "StringCharacters:  // 字符串字符序列格式
    StringCharacter  // 单个字符
    StringCharacters StringCharacter  // 字符序列+单个字符StringCharacter: // 字符串字符格式
    InputCharacter but not " or \  // 除"和\之外的输入字符
    EscapeSequence  // \序列The escape sequences are described in §3.10.6.>>\序列在3.10.6节有说明。As specified in §3.4, neither of the characters CR and LF is ever considered 
    to be an InputCharacter; each is recognized as constituting a LineTerminator.>>正如在3.4节中指出的,CR和LF并不被当作输入字符处理,分别都被认为是构成行终止
    符。It is a compile-time error for a line terminator to appear after the opening " 
    and before the closing matching ". A long string literal can always be broken 
    up into shorter pieces and written as a (possibly parenthesized) expression 
    using the string concatenation operator + (§15.18.1).>>如果在开始的"和结束的"之间(即字符串中)出现了行终止符都会被认为是编译期错
    误。长的字符串文字总是可以分成较小的块,然后在表达式(通常是在括号内括起来)
    中用+号连接在一起。The following are examples of string literals: >>以下是使用字符串文字的例子:"" // 空字符串
    "\"" // 仅包含"的字符串
    "This is a string" // 包含16个字符的字符串
    "This is a " + // 实际上是一个字符串值的常量表达式,
    "two-line string" // 由两个字符串文字组成。

    Because Unicode escapes are processed very early, it is not correct to write 
    "\u000a" for a string literal containing a single linefeed (LF); the Unicode 
    escape \u000a is transformed into an actual linefeed in translation step 1 
    (§3.3) and the linefeed becomes a LineTerminator in step 2 (§3.4), and so 
    the string literal is not valid in step 3. Instead, one should write "\n" 
    (§3.10.6). Similarly, it is not correct to write "\u000d" for a string 
    literal containing a single carriage return (CR). Instead use "\r". 因为表示Unicode的\序列会较早处理,在字符串文字中用"\u000a"表示LF字符是错误的:
    \u000a这个Unicode的\序列在翻译的第一步(3.3节)转换成事实上的LF字符,接下来
    LF字符在第二步(3.4节)被当作行终止符处理,然后在第三步中这个字符串文字成为非
    法的。在处理字符串中的换行符时,我们应该使用"\n"(3.10.6节)。类似的,在字符
    串文字中使用"\u000d"来表示CR字符也是错误的,应该使用"\r"。Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) 
    of class String (§4.3.3). String objects have a constant value. String 
    literals-or, more generally, strings that are the values of constant 
    expressions (§15.28)-are "interned" so as to share unique instances, using 
    the method String.intern.>>每一个字符串文字都是对String类的一个实例(4.3.1节、12.5节)的引用(4.3节)。
    String对象的值都是常量。字符串文字,或者说得更笼统点,作为常量表达式的值的那
    些字符串,都被使用String.intern方法“内包”了,从而可以共享唯一的实例。Thus, the test program consisting of the compilation unit (§7.3): >>因此,包含下述编译单元(7.3节):package testPackage;class Test {
    public static void main(String[] args) {
    String hello = "Hello", lo = "lo";
    System.out.print((hello == "Hello") + " ");
    System.out.print((Other.hello == hello) + " ");
    System.out.print((other.Other.hello == hello) + " ");
    System.out.print((hello == ("Hel"+"lo")) + " ");
    System.out.print((hello == ("Hel"+lo)) + " ");
    System.out.println(hello == ("Hel"+lo).intern());
    }
    }class Other { static String hello = "Hello"; }and the compilation unit:>>和下述编译单元:package other;public class Other { static String hello = "Hello"; }produces the output:>>的程序产生如下的输出:true true true true false trueThis example illustrates six points:>>这个例子说明了以下六点:Literal strings within the same class (§8) in the same package (§7) represent 
    references to the same String object (§4.3.1). >>在同一个包同一个类中的文字字符串表示的是对同一个String对象的引用。Literal strings within different classes in the same package represent 
    references to the same String object.>>同一个包不同类的字符串文字表示的是对同一个String对象的引用。
     
    Literal strings within different classes in different packages likewise represent 
    references to the same String object. >>不同的包不同的类中的字符串文字同样地表示的是对同一个String对象的引用。Strings computed by constant expressions (§15.28) are computed at compile time 
    and then treated as if they were literals. >>常量表达式中计算的字符串在编译期即被计算然后作为文字字符串处理。Strings computed at run time are newly created and therefore distinct. >>在运行期计算的字符串是即时创建的,所以是不同的对象。The result of explicitly interning a computed string is the same string as any 
    pre-existing literal string with the same contents. >>显式的“内包”一个计算出的字符串得到的结果跟先前已经存在的相同内容的文字字符串
    是同一个字符串。
    // 因为时间仓促,翻译的很拗口,大家多多包涵。
      

  13.   

    Java语言规范中所举的例子比较晦涩,对于初学者不太容易一下子理解,我在这里现个丑,举一个相对简单好理解的例子:public class TestStringIntern {
      
      public static void main(String[] args) {
        testIt();
      }
      
      private static void testIt() {
        String s1 = "sean_gao";
        String s2 = "sean"+"_"+"gao";
        String s3 = new String("sean_gao");
        String s4 = new String("sean_gao").intern();
        System.out.println("s1==s2? "+(s1==s2));
        System.out.println("s1==s3? "+(s1==s3));
        System.out.println("s1==s4? "+(s1==s4));
        System.out.println("s1.equals(s2)? "+(s1.equals(s2)));
        System.out.println("s1.equals(s3)? "+(s1.equals(s3)));
        System.out.println("s1.equals(s4)? "+(s1.equals(s4)));
      }
      
    }以下是结果:s1==s2? true         // 引用的是同一个对象,因为内容一致
    s1==s3? false        // 引用的是不同的对象,因为用了new关键字
    s1==s4? true         // 引用的是同一个对象,因为用了intern方法
    s1.equals(s2)? true  // 内容一致
    s1.equals(s3)? true  // 内容一致
    s1.equals(s4)? true  // 内容一致