File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes underlying value of boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "underlying value of boolean "false", "true"" Watch "underlying value of boolean "false", "true"" New topic

underlying value of boolean "false", "true"

tod murphey

Joined: Jan 13, 2001
Posts: 22
Hello again,
Based on the answer to this question... it appears that '1' represents false, does this seem correct?
1. class X {
2. public static void main(String[] args) {
3. boolean[] b = new boolean[1];
4. flip(b);
5. System.out.println(b[0]);
6. }
8. static boolean flip(boolean[] b) { return (b[0] = !b[0]); }
9. }

sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
i cannot understand what r u trying to say?
it prints "true " only which is obviously the result.
bcos ! converts the value of boolean expression.
"Winners don't do different things
They do things differently"

"Winners don't do different things<br /> They do things differently"
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
when u create an array it will be given default values and bolean is given default value fale that is the reason why u r getiing false .
please dont confuse the new operator used with a constructor u r just alloting space for the array it means that ur array contains 1 element
it will return true from the method bcoz asingnment is done first
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Just to give you a full explanation of your example.
boolean[] b = new boolean[1];
Creates an array of boolean values. Java automatically initializes array so the result is that we have a one element array that is initialized to 'false'.
Java passes by value, therefore passing arrays to methods allow us to change any of the values. The flip method performs unary operator '!' which alters the 'false' to 'true'.
Gives use the expected result of 'true'.
This example shows that you can use arrays if you need to alter primitive values in methods and have the changed result returned to the calling method. For example if arrays were not used:
boolean b = false;
The flip method change would not be reflected at all because it would be passed a copy of b instead of b so changing it would have no effect on b!
Knowledge is good,
I agree. Here's the link:
subject: underlying value of boolean "false", "true"
It's not a secret anymore!