File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "simple question regarding strings" Watch "simple question regarding strings" New topic

simple question regarding strings

Chaitali Deshpande

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
public class EqTest{
public static void main(String argv[]){
EqTest e=new EqTest();
String s="Java";
String s2="java";
//place test here {
System.out.println("Not equal");
1) if(s==s2)
2) if(s.equals(s2)
3) if(s.equalsIgnoreCase(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

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");
// 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

Joined: Jan 02, 2001
Posts: 783
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".
Sandeep Nachane
Ranch Hand

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

<A HREF="" TARGET=_blank rel="nofollow"></A>
I agree. Here's the link:
subject: simple question regarding strings
It's not a secret anymore!