• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Some clarification on boolean data type

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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”.
 
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic