Solved

Simple java ques

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Arrays.asList  VS  ArrayList 4 71
use lov values 2 63
difference between sorce folder and folder in eclipise 3 29
hashmap order 17 37
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

828 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