• Post Reply Bookmark Topic Watch Topic
  • New Topic

just confuse when to use "==","=",".equals","value.match" on if statement  RSS feed

 
chrstian ferma
Ranch Hand
Posts: 72
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day im just confused every time that i make an if statement i just try to use all if what fits that is what i only use thank you


if(a=b)
if(a==b) this one is i used in comparing reference in values (a=="test")
if(a().equals(b))
if(a.value.match(b)) i usually used this in java script


just confused sometimes what to used mostly when i use if statemnt on string and integer thank you
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chrstian ferma wrote:if(a=b)
Never use this.


chrstian ferma wrote:if(a==b) this one is i used in comparing reference in values (a=="test")
Use == operator when comparing primitive types or you want to explicitly compare object's identity (for example inside equals method). Do not use it to compare Strings! Use if (a.equals("test")) instead.
Or if you want to be protected against nulls you can use if("test".equals(a)).


chrstian ferma wrote:if(a().equals(b))
When a() is a method that returns an instance of some object and you want to compare its result to object b. Did you mean if(a.equals(b)) ?


chrstian ferma wrote:if(a.value.match(b)) i usually used this in java script
This is not a standard idiom for comparing in Java. You would need a class that has a field value that has a method match with parameter matching b in order to successfully compile that.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chrstian ferma wrote:if(a.value.match(b)) i usually used this in java script

Java is totally different from JavaScript, so don't expect that whatever works in JavaScript will work in the same way in Java.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!