Frage

Maybe a stupid question but i want to use a if statement in my code, but i won't work.

    String Country = getResources().getConfiguration().locale.getLanguage();

    if (Country = nl);

So i want to use the if statement to do a function when the Country String is "nl" but i don't know how to use the if statement i think.

War es hilfreich?

Lösung

It's

if (Country.equals("nl"))
   ...

Now:

  1. Equality in general is tested with double ==.
  2. String equality should be tested with equals.

Some more info for future use: using == to test equality checks if the references are the same (i.e. they are the same object in memory), which usually is not what you need (of course, using it for primitive values works as expected). For cases when you want to have custom equality checks on types you can override the equals method and place your logic there. This is what happens in case of the String class.

Andere Tipps

Because String's are objects and not primitives, then you can't use ==

But you have to use .equals instead, like Tudor has shown

    code something like this

     String Country = getResources().getConfiguration().locale.getLanguage();
    if(Country!=null)
    {
       if (Country.equals("nl")){ //something } else { }

    }


for string comparing use .equals method instead of == operator because == compare object are same or not instead of object value
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top