Internally in Java primitive booleans are treated like -1?

The other day in a forum I was having a discusion about primitive booleans data types. And one guy said that in ALL languages true internally (or natively) is treated like -1. Is this really the case?

PD: We were specially talking about Java

Upvotes: 1

Views: 1125

Answers (7)

Max
Max

Reputation: 1670

No, this is not true. In C, any non-zero value will be interpreted as true in a boolean context, but operations like == and && return 1 for true. In Python, True is distinct from any integer, but True == 1 returns True; in older versions, 1 was used. I think -1 is traditional in the BASIC family; certainly it was the case in QBasic and pre-.Net Visual Basic.

In Java, true is not "treated like" -1 ... booleans and ints are separate types that can't be compared. The internal representation depends on the implementation.

Upvotes: 3

Voo
Voo

Reputation: 30216

Actually the only language I'm aware of that used -1 as true was VARIANT_BOOL by the Visual Basic folks. And they only did that because of limitations in the language (only having bitwise logical operations didn't turn out to be such a good idea I'd wager).

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500185

This is actually part of the JVM specification, at least in certain terms - and it's not -1. Section 3.3.4 of the spec has this to say:

Although the Java virtual machine defines a boolean type, it only provides very limited support for it. There are no Java virtual machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java virtual machine int data type.

The Java virtual machine does directly support boolean arrays. Its newarray instruction enables creation of boolean arrays. Arrays of type boolean are accessed and modified using the byte array instructions baload and bastore.2

The Java virtual machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java virtual machine type int, the compilers must use the same encoding.

As an example of this:

public static boolean returnFalse() {
    return false;
}

public static boolean returnTrue() {
    return true;
}

compiles to:

public static boolean returnFalse();
  Code:
     0: iconst_0
     1: ireturn

public static boolean returnTrue();
  Code:
     0: iconst_1
     1: ireturn

Upvotes: 12

Jaco Van Niekerk
Jaco Van Niekerk

Reputation: 4182

The -1 could be as a result of the two's complement value of -1 being all bits set. The actual memory occupied is JVM-dependant and can be anything from a bit to a 32-bit integer. As far as I recall, most JVMs model booleans as ints.

Upvotes: 0

John Haager
John Haager

Reputation: 2115

The low-level internal representations of booleans is a platform specific issue. However, since Java is normally (always?) implemented using C/C++, booleans are most likely represented the same way boolean values are handled in C/C++. Namely, 0 is defined as FALSE and anything else is defined as TRUE.

Upvotes: 0

fmucar
fmucar

Reputation: 14548

It depends on the implementation of the JVM.

Upvotes: 0

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9307

I don't know about ALL languages but in java boolean is distinct from numerical values.

Upvotes: 0

Related Questions