• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

String handling.

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects are created if i write the following codes in java 1.6 :

String str1="abc";// case 1

String str2=new String("abc");//case 2

Does str1 will also be stored in the some string literal pool or only str2 would be stored in the pool ?
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ayush raj wrote:How many objects are created if i write the following codes


Well, the easiest method to check if two objects are getting created is via reference equality: instead of

Secondly, hard-coded String literals go to String pool. In above code, str1 will go to String pool, whereas str2 won't.
 
ayush raj
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Anayonkar Shivalkar : str1==str2 would compare the base addresses of the two objects from heap , while the equals method would check for the contents of the two strings created . Am i right ? But then how could i know if two objects have been created or only one object is created ? Please clarify .
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ayush raj wrote:How many objects are created if i write the following codes in java 1.6 :

String str1="abc";// case 1

String str2=new String("abc");//case 2

Does str1 will also be stored in the some string literal pool or only str2 would be stored in the pool ?



If the 2 cases are taken separately, then

case 1: only one object will be created in the string constant pool and str1 will refer to it

case 2: two objects will be created as we use the 'new' keyword, one on the heap(non-pool memory) and str2 will refer to it and other object in string constant pool - assuming that "abc" is not already present in the pool

ayush raj wrote:But then how could i know if two objects have been created or only one object is created ?



== will tell you if the two references refer to the same object on the heap. In above case (str1==str2), the result will be false.


Correct me if I am wrong.

 
ayush raj
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@C Halbe : And what about equals method? How does it manipulate the strings equality? For the case 1 : does any object exist on the heap ? Totally confused relating to String constant pool and the heap .
 
C Halbe
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ayush raj wrote:@C Halbe : And what about equals method? How does it manipulate the strings equality? For the case 1 : does any object exist on the heap ?



equals() does not manipulate the strings. It will only compare the contents of the the two string objects and tell you if they are meaningfully equal.

str1.equals(str2) will be true because in literal sense it doing something like "abc".equals("abc");

ayush raj wrote:Totally confused relating to String constant pool and the heap .



String constant pool is a part of the heap, but separately allocated only for String because of the way strings behave in Java. So when you say "in the constant pool" , it is on the heap but inside the string constant pool.

Whenever we create a string, eg.
String s = "abc";
JVM will check the constant pool if "abc" is already there in the pool, if not, it will create "abc", but if it already exists then the existing "abc" object will be allocated to s reference variable.

In case of
String s2 = new String("xyz");
JVM will create a String object in the heap (outside of the pool) because of the 'new' keyword. It will also put "xyz" in the constant pool in the manner I stated above. That is how the JVM works.

To clear your confusing try running this simple code below and checkout the results!



 
Ranch Hand
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all of your answer.
Quiz time:


public class StringTest {
public static void main(String[] args) {
String str1 = "abc";
String str2 = new String("abc");
System.out.println(" str1 == str2"+ str1 == str2);
}
}


What is the output?
A. true
B. false
C. str1==str2 true

The answer is .......B !!!
Why ? The first string in the print statement is "str1 == str2 abc " and str2 = "abc". Therefore the first string is not equals to the second one.

Have fun ?

I think Miss Kathy Sierra and Mr. Bert Bates will put this on the real exam !
Want to laugh?

 
C Halbe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you have 100% marks!! false is the answer...oh helen, there is no greater java scholar on this planet than you!!

Anyways, i just wanted Mr. ayush raj to have a clear understanding of where the two "abc" objects will be created. And I think I have proved the point!
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made a lot of mistakes in the practice exams .... I am nervous because I will take the exam in 2 weeks. That will be my first attempt to take it.

Besides K&B's book, I also read Mughal & Rasmussen's SCJP study guide, the third edition.
This is also a good book with a lot of details.

My tip about taking the exam is to modify the questions in the practice exam questions, just like what I did with your test string example and test myself.
But I really enjoy to type up my thought and logic on the Java Ranch to enhance my study.
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope you guys don't take my previous post about the System.out.println("str1 == str2 + str1 == str2 ) personal.

I think Miss Sierra and Mr Bates are very smart to write tricky exams so that we will all work hard.

But I have a feeling that that is a very possible exam question.
 
ayush raj
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Helen Ma : I could not understand the output of your code . I mean how come

System.out.println("str1==str2"+str1==str2); // this line of code displays only false as the output!! According to me , it should have at least displayed : str1==str2false

How does does str1==str2 is ignored while displaying the output?
 
ayush raj
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ C Halbe : Thanks for your help in clearing my String concepts . A good explanation was provided by you in this regard . Thanks for the same ..
 
Helen Ma
Ranch Hand
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
the print statement is interpreted in this way : the first string is "str1 == str2 "+ str1 which is "str1 == str2 123". The second string is str2 which is 123.
str1 == str2 123 is not equal to 123 . So, the answer is false.

In one of the mock exam, the writer of the exam tests how we understand the exam:

Suppose we have an int array like this : int array[] = {1,2,3}
case 1 :System.out.println(array[0]+ array[1]+ array[2]) ; //The output is 6 , which is printing the sum

case 2: System.out.println("a"+ array[0]+ array[1]+ array[2]) ; // The output is a123

case 3: System.out.println(array[0]+ array[1]+ array[2]+ "a") ; //The output is 6a instead of 123a.

The reason is that in the println method, when three numerical values comes first , the compiler adds all those numerical values and print it out as a string output. Therefore, the output in case 1 and 3 are 6 and 6a.
In case 2, the compiler sees a string "a" and then treats the rest of the numerical values as string. It concatenate all the strings.

I have a feeling that this type of question will appear in the exam.

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

Helen Ma :the print statement is interpreted in this way : the first string is "str1 == str2 "+ str1 which is "str1 == str2 123". The second string is str2 which is 123.
str1 == str2 123 is not equal to 123 . So, the answer is false.



A very good explanation indeed . The example of array you gave was clear to me . Where from you think such type of questions ? I mean they are pretty impressive for the exam point of view !!
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That print statement questions come from the CD that comes with the book.
But in KB's book, they specify that printing is important for the exam. The example is one of them.
 
ayush raj
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok . I havent seen the cd yet ! Thanks for helping .
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C Halbe @

Great Explanation!!!I wonder if this could be explained in any better way!!!Thanks a lot!!..
 
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Precedence of == is much less than +

Link
 
Always look on the bright side of life. At least this ad is really tiny:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic