0

Possible Duplicate:
Java String.equals versus ==

I am using an if statement in java to determine weather the person is a male or female by asking weather he is a boy or a girl. This is quite a stupid statement but my query is no matter what i input i always get "you are a female!" which is quite annoying. Could you please help? This is the code

import java.util.Scanner;

class ifstatement {

    public static void main( String args[] ) {
        System.out.print( "please enter boy or girl as an input:" );

        Scanner x = new Scanner( System.in );
        String a = x.nextLine();

        if ( a == "boy" ) {
            System.out.print( "You are a male" );
        }
        else {
            System.out.print( "You are a female!" );
        }
     }
 }    
Community
  • 1
  • 1
Aayush Mahajan
  • 3,856
  • 6
  • 25
  • 32
  • When you do `a == "boy"` you are testing whether a is the *exact same instance* as the string `"boy"`, not whether or not the Strings contain the same characters. – Vala Jul 26 '12 at 18:18

5 Answers5

4

Use equals() method to compare String

equals() compares the object

== compares the references value

Use

if ("boy".equals(a)) {

This will compare "boy" instance of String with String instance referred by a


See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
0

== comparison uses object reference, whether the two objects point to the same memory location. .equals() does the same in Object class, however, String class overrides it to do value comparison.

devang
  • 5,376
  • 7
  • 34
  • 49
0

The == operator checks whether the references to the objects are equal. When testing for String equality, this is not enough. A test for reference equality is done within the String.equals() method, among other checks:

 public boolean equals(Object anObject) {
        if (this == anObject) {      // Reference equality
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = count;
            if (n == anotherString.count) {  // Are the strings the same size?
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = offset;
                int j = anotherString.offset;
                while (n-- != 0) {
                    if (v1[i++] != v2[j++])        // Compare each character
                        return false;
                }
                return true;
            }
        }
        return false;
  }
Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
0

If you were to say:

 if ("boy".equals(a)){
 System.out.print("You are a male");
 } else if ("girl".equals(a)){
 System.out.print("you are a female!");
 { else {
 System.out.print("invalid response!");
 }

This would solve your problem. When dealing with strings you should always use .equals() to compare exact values. the "==" operator compares whether two objects point to the same location in memory generally. Since a string is an object they are not the same.

Robert
  • 4,306
  • 11
  • 45
  • 95
0

1. In java objects are compared using .equals() method, and Strings are objects in Java.

2. Use a.equals("boys") and that will give you the correct answer....

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75