0

I'm new to java and I'm encountering a problem that doesn't make sense for me. I have a list view that launches an activity and the activity has to read some intent values in order to display the correct information.

    // getting intent data
    Intent in = getIntent();
    // Get name
    final String catName = in.getStringExtra("category").toString();
    //Textview
    TextView categorytw= (TextView)findViewById(R.id.category_name);
    categorytw.setText(catName);

    // Category URL
    String catUrl = "";

    if( catName =="Italy" ){
        catUrl = "http://url .com/italy.php";
    }
    else if( catName =="Belgium"){
        catUrl = "http://url .com/italy.php";
    }
    else if( catName =="France"){
        catUrl = "http://url .com/france.php";
    }

As You can see I get the intent value "category" and assign it to catName variable, and then I update the title textview categorytw

So far everything is good, categorytw is updated according to the value of catName.

Now I need to fetch a particular url based on the name of the category, that is why i initialize catUrl, which is empty at the beginning. Based on the value of catName, catUrl will have a different value.

This value/url is later used when I fetch some data from the url (catUrl) :

try{
XMLParser parser = new XMLParser();
String xml   = parser.getXmlFromUrl(catUrl );
}

the problem is that catUrl is always empty. I double checked catName values.

Does anybody know what might be wrong here?

inrob
  • 4,969
  • 11
  • 38
  • 51

5 Answers5

6

== in Java compares Strings by reference (i.e. are the variables pointing to the same location in memory), not by value. Use catName.equals("Italy") to compare by value.

Khaelex
  • 742
  • 5
  • 15
  • Thanks Khaelid. I never would have thought that. I have used == in another project and it worked properly... Strange. – inrob Mar 12 '14 at 15:06
  • 2
    @bornie it works for integers and other primitive data types, but for strings and objects it won't compare by value. In some cases, using `==` to compare strings will work. For example, if you did `String a = "cat"; String b = a; if (b==a)` it would work, because they are the same location in memory. – Khaelex Mar 12 '14 at 15:08
  • 1
    @bornie look for String pool. That's probably the case where you compared successfully strings with `==` – Blackbelt Mar 12 '14 at 15:13
0

Use .equals to compare strings and not ==.

== compares reference equality.

.equals compares value equality.

In your case, as "Italy", "Belgium", "France" and catName are pointing to different objects. None of your conditions would ever match. Hence, catUrl is always empty.

Update your code as below:

   if( "Italy".equals(catName)){
        catUrl = "http://url.com/italy.php"; ## Remove the space between url and .com
    }
    else if("Belgium".equals(catName)){
        catUrl = "http://url.com/italy.php";
    }
    else if( "France".equals(catName)){
        catUrl = "http://url.com/france.php";
    }
Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108
  • 1
    To elaborate on that, `==` checks if the references on either side are pointing to the same object. `.equals()` is your implementation to see whether two different objects are "equal". – slider Mar 12 '14 at 15:09
  • @slider Added that to the answer. :) – Kirti Thorat Mar 12 '14 at 15:12
0

Strings cant be compared like primitive variables in java. The reason is that == only compares the references. So if you check if (1 == 1), it is evaluated as true as the jvm reuses primitives instead of allocating new memory for it.

String work different. In Java, strings are no primitive variables which is why every time you write "this is a string", a new object is generated and space is allocated in memory, although its the same text in the quotes. You can think of that as if you would do something like new String(// all the characters of a string) if you want. This is qhy you have to use "some string".equals("another string") to compare string. The check with equals does not only compare the reference. It checks if the object has the same value, which is why this will work.

So the correct way would be:

if( catName.equals("Italy") ){
    catUrl = "http://url .com/italy.php";
}
danijoo
  • 2,823
  • 4
  • 23
  • 43
0

== in Java programming language means that it will compare memory address with the other object. In your snippet, when you call toString(), it will generate a new String object from heap as return. The logic in if-else statment it uses a String that from Constant pool differs with in heap object. You should use equals() for comparing string values, see JDK source code for details.

richie
  • 199
  • 3
0

Declare catUrl as StringBuilder, use StringBuilder.appen() to add new values to it.

Gopi
  • 689
  • 1
  • 8
  • 17