0

Possible Duplicate:
String comparison in Java

heres my script:

public class euler4 {

    /**
     * a palindromatic number reads the same both ways. the largest palindrome
     * made from the product of 2-digit numbers is 9009 = 91 * 99.
     * 
     * find the largest palindrome made from the product of two 3-digit numbers.
     */
    public static void main(String[] args) {
        /*for(int t = 100; t < 200; t++) {
            for(int l = 100; l < 100; l++) {
                String milah = "" + t * l;
                String result = palin(milah);
                if(result != "no_pali") {
                    System.out.println(milah);
                }
            }
        } */
        String result = palin("abba");
        System.out.println(result);
    }

    public static String palin(String the_num) {
        int orech = the_num.length();
        String back_word = "";
        /**
         * the for loop has the counter starting at the orech of the number, then
         * decrementing till 0 (or the 0'th index)
         */
        for(int i = orech - 1; i >= 0; i--) {
            back_word = back_word + the_num.charAt(i);
        }
        System.out.println(the_num);
        System.out.println(back_word);
        System.out.println(back_word == "abba");
        if(back_word == the_num) {
            return back_word;
        } else {
            return "no_pali";
        }

    }
}

at line 34 it prints "abba" like it should

at line 35 it prints an exact copy of "abba" like it should

it gets weird at line 36 which is:

System.out.println(back_word == "abba");

and that prints false.....??

it printed exact copy of the words, but when i compare them, it gives false!?

and since they dont equal (when they really do) its returning the wrong thing

Community
  • 1
  • 1
bzupnick
  • 2,646
  • 4
  • 25
  • 34
  • its a duplicate once i knew the answer. before i knew the answer i had no clue what i was looking for. – bzupnick Sep 14 '11 at 02:34
  • Please don't put stuff into sites like pastebin and then link to it. The SO code rendering engine is more than up to the task. Questions and answers on SO should be useful _even if every other site on the internet disappears!_ Links for clarification or expansion are okay, but Q&As should stand alone as much as possible. – paxdiablo Sep 14 '11 at 02:45
  • 2
    As soon as you finish typing a title for a question on SO, a box pops up with similarly-titled questions. It's worth taking a quick look for things that really are duplicates. – Karl Knechtel Sep 14 '11 at 02:58

6 Answers6

28

In java the == operator compares the address of the object.

To compare if two strings are equals you need to use the .equals() function.

back_word.equals("abba");
Drahakar
  • 5,986
  • 6
  • 43
  • 58
5

Use back_word.equals("abba"); instead.

== in Java tests whethere they are the same object so back_word == back_word would return true.

The String Class's .equals method however checks

if the given object represents a String equivalent to this string, false otherwise

Taken from: String's Javadoc

Bobby
  • 18,217
  • 15
  • 74
  • 89
4

You need to use String.equals() instead of comparing pointers.

back_word.equals("abba");

(See also String.equalsIgnoreCase() if that's applicable)

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • what do you mean? what is that line of code replacing? – bzupnick Sep 14 '11 at 02:31
  • The other answers here covered it, variables in Java are pointers to an address in memory where that Object is stored, with the exception of simple types like `int`, `byte`, `boolean`. So when you use the `==` operator on Objects you are comparing 2 addresses and not the Objects themselves. If you want to test equality between 2 Objects (in this case 2 Strings), use the `.equals()` method. – Jon Lin Sep 14 '11 at 02:38
4

Because == compares that they're the same object in memory, which they aren't. They represent the same data, but they aren't the same object.

You need back_word.equals("abba") instead.

corsiKa
  • 81,495
  • 25
  • 153
  • 204
3

The == operator in Java performs object equality not value equality. back_word and "abba" are two different objects but they may have the same value.

What you are looking for is the equals method:

System.out.println(back_word.equals("abba"));
Chris Dail
  • 25,715
  • 9
  • 65
  • 74
1

In the context of the String type, using == compares by reference (memory location).

Unless you compare a string against its exact self, it will return false.

Try using the method equals(), as it compares by value, and you should get what you need.

jdawg
  • 199
  • 1
  • 3
  • 11