Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

String problem

Thread view: 
gk - 01 Oct 2006 20:16 GMT
//--------- part -1--------------

if("String".toString() == "String")

        System.out.println("Equal"); // prints

    else

        System.out.println("Not Equal");

        //--------- part -2--------------

    if(" String ".trim() == "String")

        System.out.println("Equal");

    else

        System.out.println("Not Equal"); // prints

part-1 prints because there are 2 same string literals are
compared...hence it prints 'Equal'

Now, look at part-2.
It prints 'Not Equal'

Question is WHY it prints 'Not Equal' ?

first let us look at,
" String ".trim()

according to API , trim() Returns a copy of the string, with leading
and trailing whitespace omitted.

so ultimately it becomes " String ".trim() = "String"  ...is not it ?

IF , its so then it is as similar as part-1 and hence the output should
have been 'Equal' BUT output is 'Not Equal'

WHY ?
RedGrittyBrick - 01 Oct 2006 20:38 GMT
> //--------- part -1--------------
>
[quoted text clipped - 36 lines]
>
> WHY ?

This is a FAQ.

Use .equals() to compare String *values*. The "==" operator tests if
they are the same *object*.

Two *different* objects may contain the same value.

in your case I imagine that the JVM decided to create a new String
object for the result of the trim() operation whereas it was trivially
able to assign the literal values to a common object.
Lew - 01 Oct 2006 21:05 GMT
>> //--------- part -1--------------
>> if("String".toString() == "String")
[quoted text clipped - 15 lines]
>>
>> Question is WHY it prints 'Not Equal' ?

> This is a FAQ.
>
[quoted text clipped - 6 lines]
> object for the result of the trim() operation whereas it was trivially
> able to assign the literal values to a common object.

Not only "trivially able to assign the literal values to a common object" but
required to do so (JLS 3.10.5):
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#101083

- Lew
gk - 02 Oct 2006 06:39 GMT
> > //--------- part -1--------------
> >
[quoted text clipped - 47 lines]
> object for the result of the trim() operation whereas it was trivially
> able to assign the literal values to a common object.

do you mean a new String object is created because of trim() ?

see the API  "trim() Returns a copy of the string, with leading
and trailing whitespace omitted."

does copy means here a new String object ?
Patricia Shanahan - 02 Oct 2006 08:53 GMT
...
> do you mean a new String object is created because of trim() ?
>
> see the API  "trim() Returns a copy of the string, with leading
> and trailing whitespace omitted."
>
> does copy means here a new String object ?

Yes. Note that it does not say anything like "Returns the original
string if it has no leading and trailing spaces, or a copy with leading
and trailing whitespace omitted if there is any."

Patricia
Simon Brooke - 02 Oct 2006 10:02 GMT
> //--------- part -1--------------
>
[quoted text clipped - 15 lines]
>
> System.out.println("Not Equal"); // prints

This is the old LISP eq problem.

== means not 'is equivalent to', but 'is identically the same object as'.
That your first example worked is not specified by the language design but
is implementation dependent; it happened to work on your JVM. To test
whether two strings have the same sequence of characters, use

       "String".equals( "String")

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; This email may contain confidential or otherwise privileged
;; information, though, quite frankly, if you're not the intended
;; recipient and you've got nothing better to do than read other
;; folks' emails then I'm glad to have brightened up your sad little
;; life a tiny bit.

Chris Uppal - 02 Oct 2006 10:45 GMT
> > if("String".toString() == "String")

> == means not 'is equivalent to', but 'is identically the same object as'.
> That your first example worked is not specified by the language design but
> is implementation dependent; it happened to work on your JVM.

At the risk of seeming pedantic...

Actually, the result of the example is defined (at least if you accept the
JavaDoc for java.lang.String.toString() as normative).

Since String.toString() is required to answer the string itself (not a copy);
and given the rules about duplicate occurences of string literals (they must
always refer to the same object); the result of
   "String".toString() == "String"
/must/ be true in any conforming implementation.

   -- chris
Jussi Piitulainen - 02 Oct 2006 11:54 GMT
>>> if("String".toString() == "String")
>
[quoted text clipped - 13 lines]
>     "String".toString() == "String"
> /must/ be true in any conforming implementation.

The rule about String literals being the same is also given in the
String Javadoc. It's at .intern().
Lew - 03 Oct 2006 06:59 GMT
>> Actually, the result of the example is defined (at least if you
>> accept the JavaDoc for java.lang.String.toString() as normative).

> The rule about String literals being the same is also given in the
> String Javadoc. It's at .intern().

And in the Java Language Specificiation!

(JLS 3.10.5):
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#101083

It is not just a good idea, it's the law.

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.