Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #21 (permalink)  
Old 05-08-2008, 11:56 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
I think it will print "NO"....

__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #22 (permalink)  
Old 05-08-2008, 12:00 PM
rjuyal's Avatar
Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
rjuyal is on a distinguished road
@Eranga
let us know the correct answer

@sanjeev

why do you think it will print No, is this only because it is posted in Quiz section
__________________
Life was much better in 2021
Bookmark Post in Technorati
Reply With Quote
  #23 (permalink)  
Old 05-08-2008, 12:06 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok, the answer is Yes.

So I post a big banana to Delhi.

Here is the thing I want to point there, I really hate that code. Because == used for string comparison. Not a good practice at all. Because '==' operator compares two object references and check whether two objects are exactly the same object or not.

Should I put an example, in that case I really need a gift.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #24 (permalink)  
Old 05-08-2008, 12:06 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Need any more explanations.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #25 (permalink)  
Old 05-08-2008, 12:08 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
Quote:
Originally Posted by rjuyal View Post
let us know the correct answer

@sanjeev

why do you think it will print No, is this only because it is posted in Quiz section
No dear......It doesn't mean this.
If we are doing practice by this way it is good for us.....


I guessed It.....may be two different "String" will be created in Heap.....
that's why i said no....because == checks for reference.
__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #26 (permalink)  
Old 05-08-2008, 12:10 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
Well Eranga....

again good question....

__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #27 (permalink)  
Old 05-08-2008, 12:10 PM
rjuyal's Avatar
Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
rjuyal is on a distinguished road
yes, certainly

why it was in Quiz section lol

what about

==, even if we use,
equals/equalsIgnoreCase/contentEqual

we will get the same answer "YES"

so, what was the question,

Or simply can you give us an example where
"==" is different from "equals"
__________________
Life was much better in 2021
Bookmark Post in Technorati
Reply With Quote
  #28 (permalink)  
Old 05-08-2008, 12:12 PM
rjuyal's Avatar
Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
rjuyal is on a distinguished road
Code:
I guessed It.....may be two different "String" will be created in Heap..... that's why i said no....because == checks for reference.

Strings are interned
__________________
Life was much better in 2021
Bookmark Post in Technorati
Reply With Quote
  #29 (permalink)  
Old 05-08-2008, 12:14 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Still there are lots of points to talk. May be not for us, for newbie specially.

Ok, Sanjeev and Rakesh, can you the difference about those two line of code.

Code:
if("String".trim() == "String".trim())
and

Code:
if("String".trim() == "String ".trim())
Did you see the slight difference there. My next quiz is, the answer is changed or remain as "Yes"?

__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #30 (permalink)  
Old 05-08-2008, 12:15 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by rjuyal View Post
Or simply can you give us an example where
"==" is different from "equals"
A BIG difference in real world application pal.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #31 (permalink)  
Old 05-08-2008, 12:18 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
Quote:
Originally Posted by rjuyal View Post
yes, certainly

why it was in Quiz section lol

what about

==, even if we use,
equals/equalsIgnoreCase/contentEqual

we will get the same answer "YES"

so, what was the question,

Or simply can you give us an example where
"==" is different from "equals"
Check It

Code:
public class EqualsCheck { public static void main(String[] args) { String s = "Hello"; String s1 = "Hello"; String s2 = new String("Hello"); if(s == s1) System.out.println("Yes, =="); else System.out.println("No, =="); if(s == s2) System.out.println("Yes, =="); else System.out.println("No, =="); /******************************/ if(s.equals(s1)) System.out.println("Yes, equals()"); else System.out.println("No, equals()"); if(s.equals(s2)) System.out.println("Yes, equals()"); else System.out.println("No, equals()"); /******************************/ } }
__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #32 (permalink)  
Old 05-08-2008, 12:19 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
Quote:
Originally Posted by Eranga View Post
Still there are lots of points to talk. May be not for us, for newbie specially.

Ok, Sanjeev and Rakesh, can you the difference about those two line of code.

Code:
if("String".trim() == "String".trim())
and

Code:
if("String".trim() == "String ".trim())
Did you see the slight difference there. My next quiz is, the answer is changed or remain as "Yes"?



Ans will be YES ...... because trim() method will be called first
__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #33 (permalink)  
Old 05-08-2008, 12:20 PM
rjuyal's Avatar
Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
rjuyal is on a distinguished road
BIG was much bigger in year 2023
You will get you banana back as your gift, if you give us the example

Using String, use '==' and 'equals' but answer should be different.

Please sanjeev, if you have some example then post it here


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

i guess the answer should be same again.

Both will become "String" after trimming, and as String are interned both will have same reference.
__________________
Life was much better in 2021
Bookmark Post in Technorati
Reply With Quote
  #34 (permalink)  
Old 05-08-2008, 12:25 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by sanjeevtarar View Post




Ans will be YES ...... because trim() method will be called first
No, you are wrong. Because trim() not will effect with '==' operators. That one of the deprecate process take place by Suns'

If you do it as follows,

Code:
if("String".equals("String ".trim())) System.out.println("Yes"); else System.out.println("No");
Your answer is "Yes". trim() do the omitting white spaces perfectly.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #35 (permalink)  
Old 05-08-2008, 12:25 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
Quote:
Originally Posted by rjuyal View Post
You will get you banana back as your gift, if you give us the example

Using String, use '==' and 'equals' but answer should be different.

Please sanjeev, if you have some example then post it here


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

i guess the answer should be same again.

Both will become "String" after trimming, and as String are interned both will have same reference.

Rakesh.....see example in reply #31
__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #36 (permalink)  
Old 05-08-2008, 12:26 PM
rjuyal's Avatar
Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
rjuyal is on a distinguished road
thanks
Code:
public class EqualsCheck { public static void main(String[] args) { String s = "Hello"; String s1 = "Hello"; String s2 = new String("Hello"); if(s == s1) System.out.println("Yes, =="); else System.out.println("No, =="); if(s == s2) System.out.println("Yes, =="); else System.out.println("No, =="); /******************************/ if(s.equals(s1)) System.out.println("Yes, equals()"); else System.out.println("No, equals()"); if(s.equals(s2)) System.out.println("Yes, equals()"); else System.out.println("No, equals()"); /******************************/ } }


Wow... i wanted this actually

Thanks Sanjeev
__________________
Life was much better in 2021
Bookmark Post in Technorati
Reply With Quote
  #37 (permalink)  
Old 05-08-2008, 12:28 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
Quote:
Originally Posted by Eranga View Post
No, you are wrong. Because trim() not will effect with '==' operators. That one of the deprecate process take place by Suns'

If you do it as follows,

Code:
if("String".equals("String ".trim())) System.out.println("Yes"); else System.out.println("No");
Your answer is "Yes". trim() do the omitting white spaces perfectly.
Hey Eranga

Really ....... It's important point.

askkkkkkkkkkkkkkkkkk morreeeeeeeeeee dear..
__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #38 (permalink)  
Old 05-08-2008, 12:30 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
Quote:
Originally Posted by rjuyal View Post

Wow... i wanted this actually

Thanks Sanjeev
It's Ok Rakesh.

Do you really know how both (== and equals()) are working and why output is different. Give some thoughts and then explain here.

__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #39 (permalink)  
Old 05-08-2008, 12:30 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Seems that I got my banana back,

Here is an example I came to mind in a second.

Code:
public class WhisIsTheBest { public static void main(String[] args) { String strObjOne = new String("String"); String strObjTwo = new String("String"); String strObjThree = strObjOne; System.out.println("String 1 is " + strObjOne); System.out.println("String 2 is " + strObjTwo); System.out.println("String 3 is " + strObjThree); System.out.println(strObjThree == strObjOne); System.out.println(strObjThree == strObjTwo); System.out.println(strObjThree.equals(strObjTwo)); } }
What did you say now pal.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #40 (permalink)  
Old 05-08-2008, 12:32 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
See my String object behaviors.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now