• Post Reply Bookmark Topic Watch Topic
  • New Topic

! operator doubt  RSS feed

 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This boolean invert operator inverts true to false and false to true but I want to know what ! do here with !t1.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read about the bang operator ! in the Java Language Specification.

It does nothing to t1. The member access operator (dot = .) has a higher precedence than bang, so the value of t1.equals(...) is evaluated first. Its result is boolean, so the bang operator is applied to that boolean.
 
Paweł Baczyński
Bartender
Posts: 2076
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthikeyan Pandian wrote:

This boolean invert operator inverts true to false and false to true but I want to know what ! do here with !t1.

The ! operator is not dealing with t1. It is dealing with t1.equals(t2).
The method equals is defined in Object class and it returns boolean so everything is fine .
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:The ! operator is not dealing with t1. It is dealing with t1.equals(t2)...

Yeah, good old operator precedence.

@Karthikeyan: I often find that writing it as:
if (! t1.equals(t2)) ... // note the space
helps to sort out what's going on.

Indeed, liberal use of spaces in code is generally a good thing.

Winston
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys..

I understood clearly ...
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!