Solved

Simple java ques

Posted on 2000-04-28
5
407 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 74
How do I remove an object from a 3 60
String array comparison 4 55
java mysql insert application 14 66
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

738 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