Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Splitting Strings

question regarding ! and integers

Can someone explain this

public static void main(String[] args){ int i = 7; int j = 12; if(!(i == 7 || j == 7)){ System.out.println("Fire"); } else { System.out.println("Water"); } } }

why is the outcome Water and not fire?

1 Answer

Damien Watson
Damien Watson
27,419 Points

Hey, the ! symbol means 'not', so if it is !(int i == 7) then it means if 'i' is not equal to 7, which it is, so it fails and shows water.

public static void main(String[] args){
  int i = 7;
  int j = 12;
  // if not (i==7 OR j==7) but i does == 7, so show Water
  if(!(i == 7 || j == 7)){
    System.out.println("Fire");
  } else {
    System.out.println("Water");
  }
}