aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string assignment" Watch "string assignment" New topic
Author

string assignment

rohit surve
Greenhorn

Joined: Mar 19, 2009
Posts: 17
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..
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9317
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
From wherever you get this stuff. Checking the source out, of substring() method would give you a good hint.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
And why not run this code, to assure yourself that you are wrong.
rohit surve
Greenhorn

Joined: Mar 19, 2009
Posts: 17
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Mar 19, 2009
Posts: 17
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

Joined: Aug 15, 2004
Posts: 2874
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.
sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
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


Power from within.

Failed SCJP 2 times :-(
rohit surve
Greenhorn

Joined: Mar 19, 2009
Posts: 17
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

Joined: Mar 19, 2009
Posts: 17
hey thanks adeel and sebastian(sorry that was printing mistake)...i got it...
sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
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?"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string assignment