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 Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

Greg Kitchin
Greg Kitchin
31,522 Points

Boolean variables inside brackets?

In line 32 of Craig's code, he has while(isInvalidWord);

instead of a typical check, like while isInvalidWord == true;

Line 29 is very similar as well.

I've not seen Boolean variable declarations inside brackets before. Is this a form of shorthand to check if a Boolean variable is true?

3 Answers

A while loop will execute when a condition is true.

while (true) {
}

while (isInvalidWord) {
}

// Same thing
while (isInvalidWord == true) {
}

Since isInvalidWord returns true or false, we only need to provide the boolean and it will return true or false. No need to check on the equality of the boolean.

So... it is key that 'isValidWord' has already been defined as a boolean method() , right?

The while loop, just like an if statement already checks to see if something is true. You can think of it as "while something is true, do this". When the while loop runs it will look at the isInvalidWord variable and will check if isInvalidWord is true. So the while loop is already internally checking:

isInvalidWord == true

So the second loop restates the obvious. Almost like asking "is it true that isInvalidWord is true?".

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Yes, well that is if it is like what JavaScript does. Because JavaScript is based off of Java it is probably the reason. Hope this answers your question!

Caleb,

I know it may be a little confusing, but JavaScript and Java are not at all related. Both languages are very different and were developed by different companies.

Here is an excerpt from the JavaScript wiki page:

*JavaScript was originally developed by Brendan Eich, while he was working for Netscape Communications Corporation. Indeed, while competing with Microsoft for user adoption of web technologies and platforms, Netscape considered their client-server offering a distributed OS with a portable version of Sun Microsystems' Java providing an environment in which applets could be run.[citation needed] Because Java was a competitor of C++ and aimed at professional programmers, Netscape also wanted a lightweight interpreted language that would complement Java by appealing to nonprofessional programmers, like Microsoft's Visual Basic.

Although it was developed under the name Mocha, the language was officially called LiveScript when it first shipped in beta releases of Netscape Navigator 2.0 in September 1995, but it was renamed JavaScript[11] when it was deployed in the Netscape browser version 2.0B3.[12]*

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

I know it's not the same, but from what you posted from Wikipedia it looks like JavaScript is a light weight version of Java. What am I missing? Because I heard that Java use to also do what JavaScript does know.

JavaScript and Java are two completely different languages and do not have a lot in common. Java was developed to run on multiple devices regardless of what type of processor that device used. Java is a statically typed language that needs to be compiled whereas Javascript is a scripting language that was developed for browsers. Java in the early days did enhance web pages, but it was not developed specifically for the web whereas JavaScript was. Plus Java never really dealt with HTML whereas JavaScript was designed to manipulate HTML.

JavaScript was originally named Mocha, then LiveScript, then the companies Netscape and Sun got together and thought it would be a good idea to ride the wave of popularity for Java and rename it to JavaScript. Both languages share some common syntax from C++, like many other languages such as PHP. But knowing JavaScript is not going to help you very much when learning Java and vice versa.

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

True. Okay, that makes sense now. Thanks for all the clarifications!