wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ambiguity in compilation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ambiguity in compilation error" Watch "ambiguity in compilation error" New topic
Author

ambiguity in compilation error

Sham Grandhe
Ranch Hand

Joined: Dec 16, 2003
Posts: 73
hai friends,
this one more, the ans is 4 but i answered it as 1

What will happen when you attempt to compile and run the following code
public class StrEq{
public static void main(String argv[]){
StrEq s = new StrEq();
}
private StrEq(){
String s = "Marcus";
String s2 = new String("Marcus");
if(s == s2){
System.out.println("we have a match");
}
else{
System.out.println("Not equal");
}
}
}

1) Compile time error caused by private constructor
2) Output of "we have a match"
3) Output of "Not equal"
4) Compile time error by attempting to compare strings using ==
plz see to it
thanking u
shyam
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Hi Shyam,
I think the answer should be (3), not (4).
First of all, Statement (1) is not correct becos it can compile successfully. Private Constructors only mean, if you have another program, say Test.java, if you want to create an object of type StrEq in Test, it is not allow.
However, it is allowed for an object to create its own instance inside its own methods!!! Becos Private means only the owner class can access the method, but now, it is the owner class to call the method.
Secondly, (s == s2) only means whether object s is the same object as of object s2, and thus, no matter they equal or not, no compliation error will be encountered. And thus, (4) is not correct.
As said, since s is not the same instance of s2, and thus, "NOT EQUAL" will be printed. So, (3) is correct.
Nick.


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi shyam,

I think the answer is 3 as explained by Nick.Coz strings can be compared using the == operator and this will result true when both s and s2 refer to the same object.
But , here in this case as they dont refer to the same object the result of == operator is false and prints "Not Equal"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ambiguity in compilation error
 
Similar Threads
Marcus exam Q30
simple question regarding strings
java
Mock Exam Qn
Private constructor