*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes simple question regarding strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "simple question regarding strings" Watch "simple question regarding strings" New topic
Author

simple question regarding strings

Chaitali Deshpande
Greenhorn

Joined: Jan 17, 2001
Posts: 26
from marcus mock exam#1
Given the following code, what test would you need to put in place of the comment line?
//place test here
to result in an output of
Equal
public class EqTest{
public static void main(String argv[]){
EqTest e=new EqTest();
}
EqTest(){
String s="Java";
String s2="java";
//place test here {
System.out.println("Equal");
}else
{
System.out.println("Not equal");
}
}
}
1) if(s==s2)
2) if(s.equals(s2)
3) if(s.equalsIgnoreCase(s2))
4)if(s.noCaseMatch(s2))
If i use if(s==s2) i get o/p as Not Equal
my query is, == operator checks the memory reference so the
o/p should be Equals.
Does it mean that it also checks the value in strings
or is there any other explanation.
Thanks in advance
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
use equalsIgnoreCase method .


Val SCJP <BR>going for SCJD
Chaitali Deshpande
Greenhorn

Joined: Jan 17, 2001
Posts: 26
Thanks Val Dra for ur reply but my query has not been resolved
does == sign also check contents of string
Tom Pridham
Ranch Hand

Joined: Feb 06, 2001
Posts: 92
== checks to see if the two references point to the same object:
String a = "Hello";
String b = a;
if(a == b)
{
// these two references point to the same string literal
}
.equals checks the contents of the strings:
String a = new String("Hello");
String b = new String("Hello");
if(a.equals(b))
{
// this is true, both string contents are the same
}
if(a == b)
{
// this is false because the references point to
// different objects
}
Hope this helps....
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Chaitali,
You have two different String objects. One with an uppercase 'J' for "Java" and the other a lowercase 'j' for "java". This will create two different String objects in the String pool, so using == will give you "Not Equal".
-Peter
Sandeep Nachane
Ranch Hand

Joined: Dec 06, 2000
Posts: 57
Hello,
I have a set of self review questions on object equivalence concept on my homepages at the following link. http://www.tipsmart.com/javacert/selfreview/objequiv.htm
The questions mainly deal with the concept of "referenec equivalence" and "reference identity" .
You may find it useful !!
Thanks
Sandeep Nachane


<A HREF="http://www.tipsmart.com" TARGET=_blank rel="nofollow">www.tipsmart.com</A>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple question regarding strings
 
Similar Threads
difference between string equals.
String equals
Can any one solve this == &(==)
ambiguity in compilation error
How many objects are eligible for garbage collection