string.trim() doubt

Hi,

why code is comaparing differently ?can you explain me please?

class TestKing {

public static void main(String[] args) {

System.out.println("string".toUpperCase().intern() == "STRING");

System.out.println("gap1");

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

System.out.println("Equal");

else

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

System.out.println("gap2");

if("String".toUpperCase() == "String".toUpperCase())

System.out.println("Equal");

else

System.out.println("Not Equal");// not equa is answere ,why

}

}

output

--

true

gap1

Equal

gap2

Not Equal

thanks

ravikumar

[739 byte] By [ravikumar_hyda] at [2007-11-27 11:57:40]
# 1

YOU COMPARE STRINGS BY USING .EQUALS() ONLY!

DO NOT - I REPEAT - DO NOT USE ==

TuringPesta at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 2

> Hi,

>

> why code is comaparing differently ?can you explain

> me please?

>

> class TestKing {

>public static void main(String[] args) {

>

> ystem.out.println("string".toUpperCase().intern() ==

> "STRING");

>

>

> System.out.println("gap1");

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

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

>

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

>System.out.println("gap2");

> if("String".toUpperCase() ==

> "String".toUpperCase())

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

> se

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

>// not equa is answere ,why

>

>

>

> }

>

> output

> -

>

> true

> gap1

> Equal

> gap2

> Not Equal

>

>

> thanks

> ravikumar

Use the equals() method to compare two objects. The == operator will only tell you if the contents of two variables are equal.

Eg.

"string".equals("string"); //returns true

EDIT: TuringPest beat me to it.

Message was edited by:

maple_shaft

maple_shafta at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for your reply

but I will use equal() function only. when trim() is used it is returning the true,where as toUpperCase() it is opposite.

why I am asking you is, i am preparign for scjp1.5. that is why.

thank you

ravikumar

ravikumar_hyda at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 4

>> if("String".toUpperCase() ==

> "String".toUpperCase())

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

> se

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

>// not equa is answere ,why

This is a typical question asked and answered many times.

When you say == it compares the if the same string is pointed by 2 different (string) variable, to compare the value of two different string variables we have to use string1.equals(string2)

In the first case you used intern() with strings which is same as using equals.

Hope this clears your question.

java_queena at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 5

but I will use equal() function only. when trim() is used it is returning the true,where as toUpperCase() it is opposite.

when you said trim(), it actually returns a copy of the string varaible removing all the whitespaces.

Since both the string point to the same copy its true.

Never use == to compare strings.

java_queena at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 6

If you call trim() on a String that doesn't really need trimming, you get the same String reference back, so == gives "true".

In your toUpperCase() example, you can't return the same String back, because your original String is not all uppercase (I'm not sure what happens if you did try == with toUpperCase() for something like "HELLO" that is already all uppercase).

If you trim() a String that *does* need trimming, then == will give "false":

This will be false:

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

This will be also be false:

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

doremifasollatidoa at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 7

thank you all ,I got the point.

ravikumar_hyda at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 8

Now THAT's what I call a network simulation!

cotton.ma at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 9

if("String".replace('g','G') == "String".replace('g','G'))

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

here my doubt is

--

1."String"same literal is appearing in the replace function. In literal pool

only one String is created or two.

2. "string".f1()== " string".f1()

in above "string" , " string" in second string two spaces are there ,so

two literals are created.

3. first string literals are created then functions are executed,is it true?

thank you

ravikumar_hyda at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 10

WHAT THE F UCK IS WRONG WITH YOU?

What is so hard about using .equals() like youre supposed to?

TuringPesta at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 11

> if("String".replace('g','G') ==

> "String".replace('g','G'))

>System.out.println("Replace Equal");

>

>

> here my doubt is

> --

>

> 1."String"same literal is appearing in the

> replace function. In literal pool

> only one String is created or two.

One.

>

> 2. "string".f1()== " string".f1()

>

> in above "string" , " string" in second

> string two spaces are there ,so

> two literals are created.

>

> 3. first string literals are created then functions

> are executed,is it true?

methods.

Those methods return new Strings which you are comparing.

I hope your exam is not any time soon.

cotton.ma at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 12

> WHAT THE F UCK IS WRONG WITH YOU?

>

> What is so hard about using .equals() like youre

> supposed to?

Please make an attempt to read the answer for this previously posted by the OP.

cotton.ma at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 13

when you do something like

"string" == "string"

you are creating 2 separate objects, using == to compare them will always be false as was previously mentioned, == only compares the references and they are 2 different objects. Very important concept in Java.

Also to clarify, there are no String literals, String is an Object. It may seem like a literal because you can do things like "hello", but really when this is compiled, it's doing

new String("hello") , but the preprocessor/compiler does this for you.

the only literals in Java are numbers and characters, so you can do 5 == 5 , but ... starting to sound like a broken record here ... never use == to compare Strings

m_roznera at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 14

thanks for your replies.

ravikumar_hyda at 2007-7-29 19:15:13 > top of Java-index,Java Essentials,Java Programming...
# 15

HELLO!

If you feel the need to post the message

Use equals() and not == to compare Strings please restrain yourself and re-read the thread until you realize why this isn't neccessary.

Thanks.

cotton.ma at 2007-7-29 19:15:18 > top of Java-index,Java Essentials,Java Programming...
# 16

> Also to clarify, there are no String literals, String

> is an Object.

FALSE!

http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5

<quote>

3.10.5 String Literals

A string literal consists of zero or more characters enclosed in double quotes.

</quote>

BigDaddyLoveHandlesa at 2007-7-29 19:15:18 > top of Java-index,Java Essentials,Java Programming...
# 17

> when you do something like

>

> "string" == "string"

>

> you are creating 2 separate objects, using == to

> compare them will always be false as was previously

> mentioned

I know what you are trying to say, but

"string" == "string"

is true, always.

BigDaddyLoveHandlesa at 2007-7-29 19:15:18 > top of Java-index,Java Essentials,Java Programming...