Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boolean confusion

 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Mala Gupta's book, twist in the table 2.2, says

boolean b5 = 'false' is a correct statement.

Well I looked into the errata and couldn't find anything discussed there so I assume I have understood incorrectly. I thought that we can't assign a boolean other than true or false. And we can assign char only with single quotes. Please let me know if my understanding is right. I use JDK version 6 and it won't compile, did it change in 7 or what?

Thanks Aakash
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's still not valid Java code.
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul
 
Mala Gupta
Author
Ranch Hand
Posts: 363
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aakash,

Take a note of the questions that ask you to select incorrect statements. These can be tricky to answer.

The question that you have mentioned, asks you to select incorrect statements. This question includes the following answer option. Do you think it is a correct statement or an incorrect statement.

(d) Can’t assign 'false' to a boolean variable.


I wish you Good Luck with your exam.

With respect,
Mala
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry that I misread, will be careful .

Regards,
Aakash
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aki Mohan wrote:In Mala Gupta's book, twist in the table 2.2, says

boolean b5 = 'false' is a correct statement.

Well I looked into the errata and couldn't find anything discussed there so I assume I have understood incorrectly. I thought that we can't assign a boolean other than true or false. And we can assign char only with single quotes. Please let me know if my understanding is right. I use JDK version 6 and it won't compile, did it change in 7 or what?

Thanks Aakash


Aakash,

Remember chars are unsigned ints according to the java programming language so literal int values can also be assigned to a primitive char variable (assuming they are in the range). Primitive int variables declared final and initialized on the same line are compile time constants and can also be assigned to a primitive char variable if the value is in range.

The range for a primitive char is 2 to the 16th power minus 1 to account for zero. This is 2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2 which equals 65,536. Subtract 1 for zero, the starting point and you get the exact range. So the range is 0 to 65,535. This is a tricky part of the char data type. Finally, chars are initialized to zero also like a byte, short, int, or long if not given an explicit value outside of a method (local variables need to be explicitly initialized).

Click this hyperlink for information directly from Oracle about java primitive data types

I hope this helps you.

Also, yes the boolean data type only takes the literal value true or the literal value false. Please, check-out this page that shows this simply (apparently these are considered reserved words): boolean - literal values - true - false

Respectfully,

Ted
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ted
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aki Mohan wrote:Thanks Ted


You bet. Thank-you for the positive feedback. I edited up my post some to make some of my points clearer. I hope this helps you pass the exam.

Regards,

Ted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic