Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Some clarification on boolean data type  RSS feed

 
Shivv Krishna
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

While practicing java i have came across boolean data type. i have executed below program but i am not sure how i got output of the program like below:
10 > 9 is true

Here is my program:



My question is: how this program will come to know that 10>9 true or false.. how bolean data type used in this program.

Thanks
Shivv
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't you have to do a check using an if statement to see if one number was bigger than the other? Then print out the message to console if 10 is more than 9 of something like "ten is bigger than nine"..


Also you need to put your code in tags - makes it easier to view
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added the tags. Doesn't it look better!

There are circuits in the chip which evaluate that inequality. I think they work by subtracting the two numbers and then looking for negative flags or zero flags, but I am not certain. The + operator is overloaded to operate on Strings. If you have a String and something else (in fact anything can be used), the + operator converts that something else to a String and joins the two Strings together. In the case of a boolean it turns into “false” or “true”.
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To clarify a little more, the

is an actual condition that is being evaluated, which, as Campbell described, is why true is in the output string...the condition is evaluated in the line
Another example:

This iterates once, and the output is "false". The condition (9 > 10) is evaluated twice even though there is no explicit "if".

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivv Krishna wrote:My question is: how this program will come to know that 10>9 true or false.. how bolean data type used in this program.

It ('(10 > 9)') is called an "expression", and Java is full of them.

Some of them, eg '10 > 9', are logical, "10 is greater than 9", so they return either true (if 10 is greater than 9) or false (if 10 is not greater than 9) , which Java returns as a boolean.
In this case, the expression can't return anything but true, since we know that 10 is > 9, but the things on either side of the '>' (called 'operands') don't have to be actual numbers, they can be variables containing numbers, so you can just as easily write:
int x = 10;
if (x > 9) ...
but x could just as easily be 3 or 60, or -37, so it may not be so obvious. Regardless, it will always return the result of "x is greater than 9".

Others, eg '10 * 9', are numeric, "10 times 9", so they return a number, in this case 90. And the same rules apply, the operands don't have to be actual numbers, they can be variables, eg 'x * y'.

There's a lot to know about expressions, so I suggest you read the tutorials carefully.

HIH

Winston
 
Shivv Krishna
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for all your wonderful explenations.. Now it's very clear to me...
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!