|
|
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.
|
|

05-08-2008, 11:56 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
|
|
|
I think it will print "NO"....
__________________
sanjeev,संजीव
|
|

05-08-2008, 12:00 PM
|
 |
Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
|
|
|
@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
|
|

05-08-2008, 12:06 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
|
|
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
|
|

05-08-2008, 12:06 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
|
|
|
Need any more explanations.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-08-2008, 12:08 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
|
|
Originally Posted by rjuyal
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,संजीव
|
|

05-08-2008, 12:10 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
|
|
|
Well Eranga....
again good question....
__________________
sanjeev,संजीव
|
|

05-08-2008, 12:10 PM
|
 |
Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
|
|
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
|
|

05-08-2008, 12:12 PM
|
 |
Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
|
|
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
|
|

05-08-2008, 12:14 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
|
|
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.
if("String".trim() == "String".trim())
and
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
|
|

05-08-2008, 12:15 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
|
|
Originally Posted by rjuyal
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
|
|

05-08-2008, 12:18 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
|
|
Originally Posted by rjuyal
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
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,संजीव
|
|

05-08-2008, 12:19 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
|
|
Originally Posted by 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.
if("String".trim() == "String".trim())
and
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,संजीव
|
|

05-08-2008, 12:20 PM
|
 |
Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
|
|
|
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
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
|
|

05-08-2008, 12:25 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
|
|
Originally Posted by sanjeevtarar
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,
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
|
|

05-08-2008, 12:25 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
|
|
Originally Posted by rjuyal
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
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,संजीव
|
|

05-08-2008, 12:26 PM
|
 |
Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
|
|
|
thanks
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
|
|

05-08-2008, 12:28 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
|
|
Originally Posted by Eranga
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,
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,संजीव
|
|

05-08-2008, 12:30 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
|
|
Originally Posted by rjuyal
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,संजीव
|
|

05-08-2008, 12:30 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
|
|
Seems that I got my banana back, 
Here is an example I came to mind in a second.
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
|
|

05-08-2008, 12:32 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
|
|
See my String object behaviors. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
| | |