好好。。多多发些有意义的贴,,,DAY DAY UP
解决方案 »
- 关于接口中方法的疑问
- 关于bufferedOutputStream的问题
- Java静态类里面的公共方法如何访问?
- 打包后,JAR文件内资源相对路径出错的问题
- java中Statement的executeBatch是事务操作么?
- 郁闷我很久的问题
- 如何格式化timestamp类型
- 将图片以byte[]数组的形式保存下来。注意,这个没那么简单,需要有JAVA和C语言的经验才能帮我这个问题,谢谢大家了先。
- Java中在实例化类的时候,是怎样给类中的成员变量、成员方法(包括静态类)分配内存的?
- 为什么TABLE没反应???在线等待--------
- 接口里定义了这个,什么意思?
- throws Throwable
亡灵法师这个词听起来略微专业一点:)
写这个系列是我的同学Sheepy的主意。欢迎以后多来捧场啊。
==就是比较两个引用指向的内存地址是否相同。
而
equals是比较两个引用指向的内存地址的值是否相同。
而equals是两个方法或对象
string是个方法
所以不行
有关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)
这又是什么问题哪????
是不是和常量池有关~~~~~
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. >>显式的“内包”一个计算出的字符串得到的结果跟先前已经存在的相同内容的文字字符串
是同一个字符串。
// 因为时间仓促,翻译的很拗口,大家多多包涵。
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 // 内容一致