Francisco Romero
Francisco Romero

Reputation: 13199

Different null comparison?

What it's the difference between this two codes?:

Code 1

In my first code I have a variable in which I put directly that it is null.

String prove = null;

Toast.makeText(getApplicationContext(), prove, Toast.LENGTH_LONG).show(); 

if(prove == null)
{
  Toast.makeText(getApplicationContext(), "correct", Toast.LENGTH_LONG).show();
}
else
{
  Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}

Result 1

null
correct

Code 2

In my second code I have a class named Car with his GET and SET methods but the method that have to return the String return null.

public class Car {

    private int idCar;
    private String name;

    public Car(){};

    public Car(int idCar, String name)
    {
        this.idCar = idCar;
        this.name = name;
    }

    //Here the rest of GET and SET methods

    public String getName()
    {
        return name;
    }
}

And in my MainActivity.java I have an ArrayList of Cars:

ArrayList<Car> cars = new ArrayList<Car>();

That I use on my CustomAdapter, as follows:

Toast.makeText(getApplicationContext(), cars.get(position).getName(), Toast.LENGTH_LONG).show();

if(cars.get(position).getName() == null)
{
   Toast.makeText(getApplicationContext(), "correct", Toast.LENGTH_LONG).show();
}
else
{
   Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}

Result 2

null
incorrect

What is the difference between both codes? Apparently they are similar because in both of them I compare a String that it's null vs null but their behaviour it's different.

Thanks in advance!

Upvotes: 0

Views: 76

Answers (1)

Makoto
Makoto

Reputation: 106460

These two codes are worlds apart in what they're doing.

In your first example, you explicitly set a variable to null and are comparing against null. This can be reasoned with; the code path that you exercise doesn't directly change the value to something that's non-null, so it would make perfect sense that you see your Toast with "correct".

In your second example, you do two different things:

  • You create a Toast right off the bat with text from cars(position).getName()
  • You then check if cars(position).getName() is null or not. The likely scenario is that it isn't and shows the other path.

If you wanted to make them at least similar, you would do well to move cars(position).getName() to a variable...

String prove = cars(position).getName();

if(prove == null) {
   Toast.makeText(getApplicationContext(), "correct", Toast.LENGTH_LONG).show();
} else {
   Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}

I did a quick dive into the source code for Toast, and if the string you provide it is null it will supply you with an empty string instead (or a toast that's blank). You haven't indicated that you're not seeing text in the second toast that you provided, so I would presume that the value you're getting for cars(position).getName() is not null.

Upvotes: 2

Related Questions