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 Objects (Retired) Harnessing the Power of Objects Helper Methods and Conditionals

James Barrett
James Barrett
13,253 Points

Why is the '!' symbol used next to the object reference?

It might sound silly but I am intrigued to know why does the program crash if the NOT inverter is used next the method name?

Benjamin Gooch
Benjamin Gooch
20,367 Points

What do you mean "the program crashes"? I'm not sure what you mean by if you use the inverter next to the method name, either. That's where it goes, right before what you want to invert.

Could you please clarify?

1 Answer

the "!" is saying if an object evaluates to false. take for example u are checking if a file exist. and u write a code like this. File file = new File("test.txt"); file.createNewFile(); if (!file.exist()) { // do something }

the block of code in the if statement will not run because the file EXIST and what you are checking for is "IF FILE NOT EXIST" do something.