• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

string assignment

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this qusetion is from danchisholm mock questions....


class MWC114 {
public static void main(String[] s) {
String s1 = new String(ABCDEFG), s2 = new String(EFGHIJ);
String s3 = s1.substring(4,7), s4 = s2.substring(0,3);
System.out.println((s3 == s4) + (s3 + s4).equals(s4 + s3));
}}

What is the result of attempting to compile and run the program?
a. Prints: false,false
b. Prints: false,true
c. Prints: true,false
d. Prints: true,true
e. Compile-time error
f. Run-time error
g. None of the above

i have doubt that s3==s4 should return true

as s3= EFG
S4=EFG

therefore one obj is created in heap that is s3 and as per string pool concept..s4 should point to s3 in heap so ans should be true...please rectify if i m wrong as ans given is false true..
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rohit, welcome to javaranch.

Well rohit your display name doesn't comply with the javaranch naming policy so please change it.

Secondly at javaranch if you post a question, then you have to Quote the Source of the question. So please tell us from where did you get this question so that we can help you...
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From wherever you get this stuff. Checking the source out, of substring() method would give you a good hint.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And why not run this code, to assure yourself that you are wrong.
 
rohit surve
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey this question is from danchisholm mock question...

i have checked the substring issue its correct both s3 and s4 have same string value....please gimme explanation for it...thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know the difference between == and equals() when used on objects (such as String objects)?

== does not check if the contents of two strings are the same - it only checks if both arguments refer to the exact same object. If you have two String objects, this will return false, even when the content of those String objects is the same.

Why do you think that s3 and s4 are referring to the same String object? (They're not).
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now try this.

class MWC114 {
public static void main(String[] s) {
String s1 = "ABCDEFG", s2 = "ABCDEFG";
String s3 = s1.substring(0,7), s4 = s2.substring(0,7);
System.out.println((s3 == s4) +", "+ (s3 + s4).equals(s4 + s3));
}}

This will give you true, true.

class MWC114 {
public static void main(String[] s) {
String s1 = new String("ABCDEFG"), s2 = new String("ABCDEFG");
String s3 = s1.substring(0,7), s4 = s2.substring(0,7);
System.out.println((s3 == s4) + ", " + (s3 + s4).equals(s4 + s3));
}}

This will give you false, true.

The whole point is the difference between,

String s = "";
String s = new String();

The String pool applies to the former. To clarify on what Jesper said, in my first example the s1 and s2 pointing to the same String object.
 
rohit surve
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya..i know == will return true only if two reference points to same object

say ,

String s1= new String("hi");
String s2=s1

in this case it will return true...

but my question is String s3= "EFG";
String s4= "EFG;

now as per string pool concept, value "EFG" is passed to the string pool and object will be created in heap so s3 will point to string instance with value "EFG"...and when JVM encounters same literal again it does not create a new object but will point the it reference to the existing object(to which s3 points).please rectify if i am wrong with explanation.thanks
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rohit surve wrote:..and when JVM encounters same literal again it does not create a new object but will point the it reference to the existing object



Correct. Now notice you use the word literal. Its not the case with String objects what we create using constructor. I hope, now the thing is clear.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The given example will not compile. There are several Reasons:

Here the example posted above:




Line 3 won't work, unless you declare ABCDEFG and EFGHIJ as constants or local Strings.



But this would work:



Second compiletime error: Line 5 will not compile:



This is because the "+"-Operator is not defined for arguments of type bool

But this would work:


In this case the programm changed to this:



will produce the following OutPut:

falsetrue

 
rohit surve
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey adeel...

but as strings are immutable...therefore watever method you invoke on it its of no use unless you assign a reference to it...so here s3 is just a reference to new string instance with value ABCDEFG....but second time when you assign s4 to same value wont jvm assign s4 to the existing object with same value as per string pool...i know this a basic stuff and i am getting confuse in it...sorry for the trouble.
 
rohit surve
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks adeel and sebastian(sorry that was printing mistake)...i got it...
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interesting question for the exam would be:

"How many object were created and how many objects are eligible for garbage collection, when this little main-method completes?"
reply
    Bookmark Topic Watch Topic
  • New Topic