• 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

again String anyone??

 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
here is question from http://www.geocities.com/sun_guoqiao/scjp/mockexam2answer.html
Question mock-exam-02-12:
What is the output of trying to compile and run the following code?
(Select one correct answer)
-----------------------------------------------------------------------
<pre>
public class Test012
{
public static void main(String args[])
{
String s0 = new String("Java");
String s1 = s0.trim();
String s2 = s0.substring(0, 4);
String s3 = s0.toString();
System.out.println(s0 == s1);
System.out.println(s0 == s2);
System.out.println(s0 == s3);
}
}
</pre>
-----------------------------------------------------------------------
A: The output is:
true
false
true
B: The output is:
true
true
false
C: The output is:
true
true
true
D: The output is:
true
false
false


answer is :
Answers:
: C

Explanation:
: Those method will return the String itself under
the circumstance given by the question.


I was thinking the answer will be
false
false
false
after seeing the answer I run the program and answer is correct.
My question is this. If as per java doc http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html#substring(int,%20int)
substring() method of String class returns new String object then how come s0 , s2 pointing to same String litral.
(s3 == s0) true agreed.// as per Docs
(s1 == s0) true agreed (as per docs trim() method Returns:
this string, with white space removed from the front and end.
)
but with question. Then how can we say that String is immutable ?
OK let us say ... yes, inernally JVM finds the String available in String pool so it just made other references to point to that litral and hence answer is true for all.
If this is the case then
<pre>
public class Test011128_012
{
public static void main(String args[])
{
String s0 = new String("java");
String y = new String("ava");
String s4 = s0.substring(1,4); // s4 will refer object containing "ava"
String s1 = s0.trim();
String s2 = s0.substring(0,4);
String s3 = s0.toString();
System.out.println("y == s4 : " + (y == s4) +" : s4 = "+ s4 + " ,y = "+ y +" \n");

System.out.println(s0 == s1);
System.out.println(s0 == s2);
System.out.println(s0 == s3);
}
}
</pre>
in above code why (y == s4) is not true as both string references should be pointing to same string object ??
So now I have two Questions:
1. How can we say that String object are immutable as trim returns *this* after removing spaces from string object.(contents of string being changed)
2. If substring() method is suppose to return new String object then how came (s2==s0) is true ?
Thanks in advance
------------------
Regards
Ravish

[This message has been edited by ravish kumar (edited November 28, 2001).]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explanation is that when a String manipulation method (trim(), substring(), etc.) is such that it performs no change to the object String, the object String is returned. I had trouble dealing with this concept as well. It seems to me to be an inconsistency in the language. But that's the way it is; learn it, remember it.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it is an inconsistency but more of an optimization.
If the method does not change anything in the String on which it is invoked there is no need to create a new String with the same content which would only waste memory. This is the same idea as with String literals which are stored in a String pool and shared among every classes... This is for optimization purposes. The less Objects you create the faster your application runs the more memory you have at your disposal....
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valentin,
It means it is implementation dependent.
But if you see JSDK2 doc says about substring() method that it returns *new* string object. http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html#substring(int,%20int)
copy & paste from the abouve URL(made Bold *new*):


Returns a new string that is a substring of this string.


If it is new object then how come (s2==s0) be true.
About trim() I was wrong ... if it does not changes the string then it returns this String object.
from Doc:


If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.


But what about substring()??? No where doc says that it will return this String.
Is it implementation dependent ?
help me..
TIA (Thanx In Advance)
------------------
Regards
Ravish
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i just read ur problem
the given answer is correct if u think this way.
1. String s0 = new String("java") creates a string object in a memory
2. String s1 also get the result as "java" only which already resides in memory so system does not create new object for that.
3. String s2 same as 2.
4. String s3 same as 2.
though u may thought that changing the value of s0 causes change in others but it wont happen as strings r immutable
in case of ur example given below ur talking about "ava" but here the object generated by substring "java" is different.JVM checks the existance of the object only with related objects and not with total memory.
rgds
vishal
[This message has been edited by vishal avad (edited November 29, 2001).]
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vishal
My question is : JSDK2 doc says that substring() method returns new string object.
in case of trim() doc says that if it can not be trimed then it returns this String.
in case of toString() doc says that it returns this String.
but no where doc says that substring() will return this String. It says: substring() Returns a new string that is a substring of this string.
So that's what I want to know, is it implementation dependant??
------------------
Regards
Ravish
 
vishal avad
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ravish
i m not saying it is implementation dependent.
but it is depend on the object which u r going to use for applying substring()
if the function is not causing any change in the object then it is going to refer the same object so it returns true
rgds
vishal
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravish,
Concerning substring, let's dig into the source code of String.java:

You can see that this String is returned in case the substring asked is the same as this String (and not a new String). In this respect the Java doc API is outdated !
This answers why s0==s2 returns true !
My advice: When you don't know, dig into the source code and get your hands dirty! It feels so good after!
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 29, 2001).]
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Val
Thanks a lot . My fault was that I was believing in docs. Though I also generally try to dig but sometime I become lazy.
Thanks a lot again. Now I will try to dig first and then come to you.

Originally posted by Valentin Crettaz:

In this respect the Java doc API is outdated !


Now I will remember this.

Originally posted by Valentin Crettaz:

My advice: When you don't know, dig into the source code and get your hands dirty! It feels so good after!


Thanks again..
I was just giving sun_guoqiao mock test and now I will come after one week. Shame on me I got only 50%.
Thanks a lot for every thing, going to prepare more. I think I need lot of preparation. and when I will come I will come with lot of doubts and questions and yes obviously with lot of dirts too.
------------------
Regards
Ravish
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good luck in your preparation ravish

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic