• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

String interview question

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


Please see below example of String and can you answer how many objects, instances and references are created there?



Please explain your answer...

According to me ....

a b c a1 --> 4 references
"abc", "def", "abcdef", "abcdefabcdef" --> 4 Objects
and 4 instances...

I think I am wrong...
Can you elobrate the answer?
 
Ranch Hand
Posts: 37
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your answer is correct.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds alright. Is there a difference between objects and instances?

John
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Stark wrote:Is there a difference between objects and instances? John


There might be certain situations where you would use one instead of the other (although I can't think of an example off the top of my head), but in general they are interchangeable.
 
ket bhav
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

John Stark wrote:Is there a difference between objects and instances? John


There might be certain situations where you would use one instead of the other (although I can't think of an example off the top of my head), but in general they are interchangeable.




Did you mean by Inheritance and changing object and instance?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ket bhav wrote:Did you mean by Inheritance and changing object and instance?


Sorry. I don't understand what you're asking.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it might be five String objects, because on the last line, a + b + a1, first there will be a String for "a + b", and then another that adds that String to 'a1'.

What does everyone think?

cheers, john
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will be dependent on which version of Java you are using but with Oracle Java 7.01 it's 6. If you want to see why and what those objects are, put that code in a class, compile it and then look at the bytecode with javap.
 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Wintermute wrote:I think it might be five String objects, because on the last line, a + b + a1, first there will be a String for "a + b", and then another that adds that String to 'a1'.

What does everyone think?


I am not certain about it but if in string a+b+a1, at first object is to be created for a+b then it should not be a new object. Since a+b is already an object in the string pool.

What do you say? Correct me if I am wrong.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Astha Sharma wrote:

John Wintermute wrote:I think it might be five String objects, because on the last line, a + b + a1, first there will be a String for "a + b", and then another that adds that String to 'a1'.

What does everyone think?


I am not certain about it but if in string a+b+a1, at first object is to be created for a+b then it should not be a new object. Since a+b is already an object in the string pool.

What do you say? Correct me if I am wrong.


In this case you are wrong. However, if a and b were declared final then that would be true because the compiler would know that a + b would have a fixed value and could treat it as a String literal and add it to the String pool.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:In this case you are wrong. However, if a and b were declared final then that would be true because the compiler would know that a + b would have a fixed value and could treat it as a String literal and add it to the String pool.


Not getting you
If we write code like this

there are two references al and al2 referring to the same object that is a string literal. Then why is it wrong in above case?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might appear that a + b represents a String literal but the compiler cannot make this assumption.
It can only make that assumption if a and b are declared as final - meaning they cannot be changed.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh...now i got it
compiler cannot make assumption that a and b represents the same string literals in both the lines
thanks for clearing my doubt
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"How many objects" kind of questions are very, very tricky. This has been discussed in JavaRanch forums many times before. In general, "How many String objects" would be a better question.

The codewill be perceived asIf you ask a generic question "How many objects were created", then you have to think about the StringBuilder objects getting created, and not to forget, an object of char[] inside every (well, mostly) String object. It's a deep hole you don't want to go into.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write your code, compile it, then use "javap" with command "-c" to disassemble your java bytecode. You will see what java do with your String objects and how many are created.

Example:
javap -cp c:\myjava\stringtest\ StringTest

where c:\myjava\stringtest\ is classpath location, and StringTest is class name.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aditya Jha wrote:then you have to think about the StringBuilder objects getting created


And that only applies to versions of the JVM that use this particular optimisation. Versions of the JVM before StringBuilder were added obviously couldn't use it and there is no requirement for future versions to use it. That is why you should always use + to concatenate Strings. It allows the compiler to use whatever optimisation it wants.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. My description is regarding the JVM that is covered by this exam as of now.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aditya Jha wrote:Agreed. My description is regarding the JVM that is covered by this exam as of now.


Okay. I'd forgotten this thread was in the SCJP/OCPJP forum.
 
reply
    Bookmark Topic Watch Topic
  • New Topic