0

In this program below:

public class medianTemp {
public static void main(String[] args){
    int length = args.length;
    int[] n = new int[length];
    n[0] = Integer.parseInt(args[0]);


    System.out.print(n[0] + " ");

    for (int i = 1; i < length; i++ ){
        String c = args[i];
        if (c.equals(".")){
            n[i] = n[i-1] + 0;
            System.out.print(n[i] + " ");
            }
        if (c.equals("+")){
            n[i] = n[i-1] + 1;
            System.out.print(n[i] + " ");
            }
        if (c.equals("-")){
            n[i] = n[i-1] - 1;
            System.out.print(n[i] + " ");
            }

Inside the for loop and inside the if statements. If I use for example args[i] == "." (instead of converting args[i] to string), the code above doesn't work and only the initial integer is displayed. Can someone please tell me why this happens?

atamanroman
  • 11,607
  • 7
  • 57
  • 81
JavaLearner
  • 45
  • 1
  • 9
  • 4
    `==` is for raw data types (`int`, `float`, `double`, ...). `equals()` is for objects (`String`, ...). Using `==` on objects compares referencing address. – AntonH May 09 '14 at 22:10

4 Answers4

4

== compares objects based on their memory location when they are not primitives. Strings are not primitives, so while the content of 2 String objects may be equal the address of each one in memory is different and == returns false.

tempoc
  • 377
  • 3
  • 7
0

In java, String objects (and nearly all objects) need to be compared with equals(). Two String objects may have the same value but be different objects (i.e. duplicates in memory a la new String()). The == comparison compares references.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

You just need to use equals for string comparison.

Example from this website : http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/

String a = new String ("a");
String b = new String ("a");
System.out.println (a == b);

It returns false, while the following code returns true.

String a = new String ("a");
String b = new String ("a");
System.out.println (a.equals(b));
Dev'Hamz
  • 478
  • 4
  • 9
-2

== is a relational operator, referring to the relationships that values can have with one another.

Also, the == operator obviously means "Equal To" and only works for raw data types.

These types include double, int, and float. However, the == operator will not work in a boolean expression (only true/false works).

So in sum, it's really how you put your program together to get this operator to work. I recommend reading JAVA programming books such as the "JAVA 2" Series. Hope this helped!

  • 1
    What do you mean by `However, the == operator will not work in a boolean expression`? – Sotirios Delimanolis May 09 '14 at 22:32
  • 1
    == works for objects too. It just means something different to what you think it means. It means honestly and faithfully the same object, not "looks the same" which is what `equals()` typically means – Richard Tingle May 09 '14 at 22:38