Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple java ques

Posted on 2000-04-28
5
Medium Priority
?
436 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
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 60 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

963 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