Home > java hacks, Uncategorized > .equal() and .compareTo()

.equal() and .compareTo()

Interestingly from [1]. I came to know that sometimes (I am not quite sure when) ‘==’ operator does not work for comparing two integer values.

For example, in one of my program today I found v1==v2 was returning false. where v1 = 129 and v2 = 129.  huh!!

After finding [1] now I now that this also happens. As [1] suggest, the right way of doing this is checking whether

v1.equals(v2)

Similarly for comparing String variable following syntax should be used.

s1.equals(s2) 

 

Reference:

  1. http://stackoverflow.com/questions/17601803/when-comparing-two-equal-integers-in-while-loop-the-equal-to-operator-fails
Advertisements
Categories: java hacks, Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: