Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Boolean Conundrum

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While working on an issue of code that didn't run as I'd expected, I decided to build in a simple design time test.
This was to use a bool to check if a particular method had been previously run or not.

However I hit a problem that the bool seems to be always set to true  -  even after I specifically set it to false.
I know this can't be so, but  :-

This bool variable is declared in MainActivity:
Boolean vmWordsHaveBeenPopulated = false;

This code running from a button click:


Even using the  sledgehammer approach of resetting the boolean to false in that first line right before testing it doesn't seem to work because the result is always a text display of "Populated already".

That bool variable is not accessed anywhere else in the MainActivity (or any other) code.

It has me stumped.  Why does it always return true?



 
Rancher
Posts: 116
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You used = instead of ==
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darn it!   My VB background has tripped me up again!

Thanks Adrian.
 
Marshal
Posts: 14367
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is why you should avoid trying to compare a Boolean variable to true or false. It's redundant and it can lead to bugs like that.
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Which is why you should avoid trying to compare a Boolean variable to true or false. It's redundant and it can lead to bugs like that.



OK, thanks.  I'll take that on board.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!