-1

I am getting a value as 9999912499 from the database. I have separated it in two parts 99999 and 12499 using substring. Now I want to check whether if the 1st string is equal to 99999 then i do some processing otherwise something other processing. But controls never gets in to the if loop

Following is a snapshot:

String strPscId = Long.toString(pscID);
String convPscID = strPscId.substring(5, strPscId.length());
String checkNine = strPscId.substring(0,5);
BigDecimal jpaIdObj = jeuParam.getJpaIdObj();
Long mod_id  = modele.getModId();

log.info("outstrPscId == " +strPscId);
log.info("outconvPscID == " +convPscID);
log.info("outcheckNine == " +checkNine);
log.info("outjpaIdObj == " +jpaIdObj);
log.info("outmod_id == " +mod_id);

if(checkNine == "99999") { <method-call> }
else { <another - method - call> }
Ravi
  • 30,829
  • 42
  • 119
  • 173

5 Answers5

1

For some reason, the people that make java decided that == shouldn't be used to compare Strings, so you have to use

checkNine.equals("99999");

Look at the following code:

String str1 = "abc";
String str2 = str1;

In the first line, a new string is created and stored in your computer's memory. str1 itself is not that string, but a reference to that string. In the second line, str2 is set to equal str1. str2 is, like str1, only a reference to a place in memory. However, rather than creating an entirely new string, str2 is a reference to the same place in memory that str1 is a reference to. == checks if the references are the same, but .equals() checks if the each character in a string is the same as the corresponding character in the other string.

boolean bool1 = (str1 == str2);
boolean bool2 = str1.equals(str2);

If this code were added to the code above that, both bool1 and bool2 would be true.

String str1 = "abc";
String str2 = new String(str1);
boolean bool1 = (str1 == str2);
boolean bool2 = str1.equals(str2);

In this case bool2 is still true, but bool1 is false. This is because str2 isn't set to equal str1, so it isn't a reference to the same place in memory that str1 is a reference to. Instead, new String(str1) creates an entirely new string that has the value of str1. str1 and str2 are references to two different places in memory. They contain the same value, but are fundamentally different in that they are stored in two different places, and therefore are two different things. If I replaced new String(str1) with "abc" or str1, bool1 would be true, because without the key word new, the JVM only creates a new string to store in memory if absolutely necessary. new forces the JVM to create an entirely new string, whether or not any place in memory already has the same value as the new string being created.

.equals() is slow but generally more useful than ==, which is far faster but often does not always give the desired result. There are many times when == can be used with the same result as .equals(), but it can be difficult to tell when those times are. Unless you a knowledgeable programmer making something where speed is important, I would suggest that you always use .equals().

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
  • 1
    If I had a cent for each answer like that, I would have ∞ cents now. Even more. – Maroun Nov 14 '13 at 15:57
  • 1
    They had a good reason why not to use `==`. – Maroun Nov 14 '13 at 16:00
  • If they implemented value comparison for `==` then you'd need a `ReferenceEquals` as in C# to get that behaviour. It does make more sense though, as value comparison is by far the more common use case. – Zong Nov 14 '13 at 16:01
  • 1
    @RyanCarlson in your last code block, what would happen if you were to have `String str2 = "abc";` instead of `new String("abc");`? Since you're doing all of this, you might as well get this in there too :) – Paul Samsotha Nov 14 '13 at 16:48
  • @MarounMaroun Now how many cents would you have if you got one for every answer like this? – The Guy with The Hat Nov 14 '13 at 17:30
0

You need use equals method, rather than == to compare strings.

Change from

if(checkNine == "99999") 

to

if(checkNine.equals("99999"))
Mengjun
  • 3,159
  • 1
  • 15
  • 21
0

The == operator is used to compare the content of two variables. This works as expected when using primitive types (or even wrapper classes because of auto-boxing). However, when we are using == with a reference to an object (e.g., checkNine), the content is the reference to the object but not the value of the object. This is where equals() method is used.

if("99999".equals(checkNine)){ 
    <method-call>
}
else { 
    <another - method - call> 
}
Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
  • Sorry guys. I pushed space bar when I thought I was typing in the text area. But as I pushed the button, the "Post answer" button got hit. – Kashif Nazar Nov 14 '13 at 16:04
-1
if(checkNine.equals( "99999")) { 
   <method-call> 
}
else { 
     <another - method - call>
}
Matt Penna
  • 117
  • 7
-1
if (strPscId.startsWith("99999"))
{
     bla bla
}
else
{
     sth else than bla bla
}
Artur
  • 7,038
  • 2
  • 25
  • 39