Solved

Simple java ques

Posted on 2000-04-28
5
376 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
5 Comments
 
LVL 4

Expert Comment

by:kylar
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Heh, all 4 of us said the same thing, think he'll read it?

Kylar
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now