• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings' Puzzle!

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all ..
can u pls help me with the following code??
1. if( "String".endsWith(""))
System.out.println("True");
else
System.out.println("False");
The code produces True
2. if( "String".startsWith(""))
System.out.println("True");
else
System.out.println("False");
The code produces True
3. if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
The code prints "Not Equal".

4. if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
The code prints "Not Equal".
5.public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
The code compiles cleanly and shows "String Version" .

6. public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
The code does not compile.

Hope u guys will be able to bail me out of thie puzzle!!
thanx

------------------
Hima
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. if( "String".endsWith(""))
System.out.println("True");
else
System.out.println("False");
The code produces True
2. if( "String".startsWith(""))
System.out.println("True");
else
System.out.println("False");
The code produces True

this is because "String" is nothing but ""+"String"+""
3. if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
The code prints "Not Equal".

correct while using == operator for comparing strings you are actually comparing the memory address at which they are stored and not the content. for content comparison use equals() method.
4. if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
The code prints "Not Equal".
again same
but you should keep in mind that ("String"=="String") is true
because first one is assigned memory in system then to compare it again assigns a memory to second one but here in case of strings java first checks whether the content is already stored in memory or not , if yes not new allocation is made.
and hence memory addresses are same.
here " String " is allocated a memory space , then "String" is allocated a space now what you are comparing is " String ".trim() which is still in volatile memory not stored anywhere so memory addresses are different.

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi hima,
ur first 4 doubts r being efficiently solved.
i am solving 4 & 5.
always remember that the most specific option is selected.
here String is more specific then Object so it gets selected.
in the 5th code both String & StringBuffer are equally specific
hence compiler complains of ambiguity.
hope this clears ur doubt.
kriti
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
so Java interpretes the parameter null as a String???
O.k. every object can be null. But Java takes null as every object???

really confused
Axel
 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Axel Janssen:
Hi,
so Java interpretes the parameter null as a String???
O.k. every object can be null. But Java takes null as every object???

Axel


hi Axel,
String & StringBuffer are also Objects.so null is a valid argument for all the methods in the 5th & 6th ques above.but the most specific one is chosen."Object<String=StringBuffer" is the order of specificity.>

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kriti sharma:
hi hima,
ur first 4 doubts r being efficiently solved.
i am solving 4 & 5.
always remember that the most specific option is selected.
here String is more specific then Object so it gets selected.
in the 5th code both String & StringBuffer are equally specific
hence compiler complains of ambiguity.
hope this clears ur doubt.
kriti



Hi Kriti,

I think u r perfactly right for the 5th one. But ..can u explain 2 me why the last one(6th) is not compiling ?
(More specifically the 6th one)
THANKS IN ADV.
<marquee> Ratul Banerjee</marquee>


[This message has been edited by ratul banji (edited March 24, 2001).]
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kriti sharma:
hi hima,
ur first 4 doubts r being efficiently solved.
i am solving 4 & 5.
always remember that the most specific option is selected.
here String is more specific then Object so it gets selected.
in the 5th code both String & StringBuffer are equally specific
hence compiler complains of ambiguity.
hope this clears ur doubt.
kriti


Hi Kirti,

I think u r perfactly right for the 5th one. But ..can u explain 2 me why the last one(6th) is not compiling ?(More specifically)
THANKS IN ADV.
<marquee> Ratul Banerjee</marquee>
 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ratul,
in the 6th ques there are two equally specific arguments ie String & StringBuffer.hence such a program would not compile & the compiler would complain of ambiguity.always remember
Object<String=StringBuffer in terms of specificity.>
Kriti
 
Hima Mangal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody..
thanx for ur help and replies.. but i still don't understand the first four ques.. :-(
is it always that a string begins and ends with whitespaces??
also, in the ques using replace , since both the sides are equal, don't u think that the left hand side will be calculated first and since the right hand side is also equal to the result of L.H.S. , it will be assigned the same memory location, thereby result being "Equal" instead of "Not Equal"?

------------------
Hima
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hima(Remember me),
<code>
is it always that a string begins and ends with whitespaces??
</code>
Yes...U can think String r along with Trim by default.
Thanks.
<marquee> Ratul Banerjee </marquee>
 
Hima Mangal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..
thanx for all the replies..
well.. Sridevi.. i am trying to find out whether my string ends with whitespaces rather than what u r suggesting, and so the code should print false rather than true which it is actually printing..
also, i would greatly appreciate it if somebody will explain me the last two ques..
thanx..
------------------
Hima
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hima,
When the compiler executes a method it searches the class for a method whose signature matches the invoking methods signature.
In question 5, the invoking method is <code>question.method(null)</code>. The compiler finds two methods with the same name; both of which can legally take a 'null' as an argument.
Because it finds two which match, it checks to see which method is the more specific. In this case, String is a subclass of Object; it is more specific, so it is chosen.
In question 6, the same situation occurs, only this time neither class is mores specific. <code>method(StringBuffer sb)</code> and <code>method(String s)</code> can both legally accept a 'null' as an argument. StringBuffer is not a subclass of String, nor is String a subclass of StringBuffer. The compiler doesn't know what to do! There is no way it can tell which method you want it to use so it throws a compile-error and tells you the method reference is ambiguous.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited March 26, 2001).]
 
Hima Mangal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..
Thanx Jane for such a precise and clear reply.. it sure clears all my doubts regd ques 5) and 6)
but i am not yet sure about the first four ones.. could u pls take time out to reply them as clearly?
thanx..
------------------
Hima
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hima... I'll try to do my best in the first 4...

Originally posted by Hima Mangal:
1. if( "String".endsWith(""))
System.out.println("True");
else
System.out.println("False");
The code produces True


Let's think of a String as being ""+String+"". No, these are not white spaces... they are empty strings, so basically you are asking: If the word "String" has an empty string at the very end, do something.. That is always true.
Same thing for #2.


3. if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
The code prints "Not Equal".


Think you are holding to objects: each in one hand. The == operator is the Java equivalent of "Are you two the same object?" and not "Are you two equal?". Now if you want to compare the objects you have in both hands, you would probably ask "Are you two equal?" as opposed to "Are you two the same?", because you know they are not the same. They are darn similar, but the fact that one is in your right hand and the other in your left, makes them two different objects.
For the String object, the method to ask "Are you two equal?" is the "equals(String x)". "String".equals("String") is true. "String" == "String" is false.
does it make sense?


4. if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
The code prints "Not Equal".


Following the same example as above, the fact that you trim the object on your right hand does not convert it in your left hand's object.It makes it look like the other one, but is not the same object. They are still in different hands, as different objects. They have the same string, but the fact that their location is different should tell you they are not the same.
Hope I was of help.
Francisco.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hima,
Just to add to Francisco's answer; for questions 3 and 4, whenever a String method such as <code>replace()</code> or <code>trim()</code> must change the String object in order to comply with it's contract; a new String object is returned.
For example, in Question 3 <code>"String".replace('g','G') == "String".replace('g','G')</code>, in both cases the <code>replace()</code> finds a small 'g' and replaces it with a capital 'G'. In both cases a seperate String object is created, with the same value StrinG. Java does not check the literal string pool in these situations. It always creates a new String object.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Hima Mangal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all..
Thanx a lot Francisco and Jane.. my doubts have been cleared beyond doubt.. :-) .. thanks for bailing me out..

------------------
Hima
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic