Solved

Simple java ques

Posted on 2000-04-28
5
417 Views
Last Modified: 2008-01-16
The arithematic operators cannot be used on Boolean Type but can be used on Char type-   Why
0
Comment
Question by:ashi01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Expert Comment

by:kylar
ID: 2759923
are you talking about a Boolean class or a boolean primitive? I can't find a class called Char so I assume you are talking about a primitive data type? A boolean is only a single bit. there is no manipulation involved, with the exception of logical functions (ANDing ORing bits) etc. A char actually contains a value that has numeric meaning in Java. A boolean in Java doesn't correspond to 1 or 0. it only corresponds to TRUE or FALSE. so if you try to do any addition to a boolean, ie:

True      False
+  5      -   19
-----     -------

It has no meaning. I hope this helps ;)

Cheers,
Kylar




0
 
LVL 16

Expert Comment

by:imladris
ID: 2760137
Note: unlike C, Java has an explicit boolean type. In C, integers can evaluate to true or false depending on whether they are 0 or not, and the result of comparisons is an integer which is 0 or 1. And so, of course, since the underlying data type is actually integer, you can do arithmetic with it.
In Java, boolean is an actual type that is treated completely separately.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 20 total points
ID: 2760163
The Java language specifies that a boolean primitive is not a numeric data type and cannot be cast to a numeric data type. Numeric data types are byte, short, int, long, float, and double. The primitive type char is technically classified as a character data type but practically acts like a numeric. Because the arithmetic operators all require numeric or character data types as operands, booleans cannot be used in arithmetic expressions. Likewise, the logical operators all required booleans as operands. That is why you cannot write conditional expressions in Java the same way as you would in C or C++.

The following is valid in C/C++ but illegal in Java:
    int i = 0;
    if (i) {
        /* do something */
    }

Simplicity is one of Java's overriding design goals. Simplicity and removal of many "features" of dubious worth from its C and C++ ancestors keep Java relatively small and reduce the programmer's burden in producing reliable applications. The ambiguous nature of data types and operators in these predecessor languages was a cause of programming error. Automatic type conversions, even in the face of lost precision, and mixing of numeric and logical operations across all types was a source of many easy to introduce but difficult to diagnose defects. For these reasons, data types, operators, and conversion rules, have been clarified and simplified to make it harder to write defective programs. Sometimes, it makes it a little harder to write correct programs until we internalize the new rules :-)

Best regards,
Jim Cakalic
0
 

Expert Comment

by:bhardwajanjali
ID: 2761862
Hi,
Actually, java has a simple type called boolean or logical values. it can have only one of the two values true or false.  Unlike C, boolean values cannot be represented by 0 r 1 in java.  So there is no point of using arithmetic operators in boolean type.  But you can very well use arithmetic operator on char type as java uses unicode to represent characters in java which is agin a numeric value so arithmetic operations can be carried out on character typs.
Hope is helps you.
All the best,
Anjali
0
 
LVL 4

Expert Comment

by:kylar
ID: 2766277
Heh, all 4 of us said the same thing, think he'll read it?

Kylar
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question