-1

I have started learning java. I am learning Boolean in java. Boolean has two types, true & false. In C there was nothing like this. We have either 0 or 1. So, I want to know how does true and false work? How is it stored in memory? Does it require 1 bit space or more? Is it some type of string? For example,

class Test {
public static void main(String[] args) {
    System.out.println( 5 > 6 );
}

}

I get :

false

So, what is the size of false? It looks like a string in java.

learner
  • 4,614
  • 7
  • 54
  • 98
  • 5
    http://stackoverflow.com/questions/383551/what-is-the-size-of-a-boolean-variable-in-java – cool Oct 04 '15 at 09:45
  • Ok, what about false and true? – learner Oct 04 '15 at 09:46
  • what do you mean by that? true and false are primitive boolean types. – cool Oct 04 '15 at 09:49
  • 'primitive boolean type'. I want to know, what about what is true and false actually? There is nothing like this with int, char etc. – learner Oct 04 '15 at 09:50
  • 3
    "So, what is the size of `false`? It looks like a string in java." --- Compare that to an `int` which is a 32-bit value stored in 4 bytes, but "So, what is the size of `123456`? It looks like a string in java.". Do you see the fallacy of your question? – Andreas Oct 04 '15 at 09:53
  • @learner , true and false is a representation of that 8 bytes. actually , size is approx 1 byte. Size of the boolean in java is virtual machine dependent. but Any Java object is aligned to an 8 bytes granularity. A Boolean has 8 bytes of header, plus 1 byte of payload, for a total of 9 bytes of information. The JVM then rounds it up to the next multiple of 8. also , you can simply say that , true has 8 byte representation and false has another. and for user's view , they have given keyword !!! actually it does not store string or character array. – KrunalParmar Oct 04 '15 at 09:53
  • @Andreas I disagree. 12345 is an integer. false may be string also. – learner Oct 04 '15 at 09:55
  • 1
    @KrunalParmar Wow, are you confusing the issue. `true` does *not* have an 8 byte representation. `Boolean.TRUE` might, but that's not what you're saying. – Andreas Oct 04 '15 at 09:56
  • 1
    @learner The number stored as the 32-bit value `00000000000000011110001001000000` (hex `0001E240`) will print as the string `"123456"`, same as the boolean value `false` will print as the string `"false"`. If you assume a boolean is stored as a string just because it has a string representation when printed, you'd have to make the same (flawed) assumption for `int` values. – Andreas Oct 04 '15 at 09:59
  • @Andreas yes this is what I was missing, http://stackoverflow.com/a/32932220/3397298 solves my problem – learner Oct 04 '15 at 10:01
  • @cool No they aren't. They are values. – user207421 Oct 04 '15 at 10:08

2 Answers2

2

The answer to why you see true and false when calling System.out.println() is because the println method is overloaded. One of the methods takes an argument of boolean

public void println(boolean x) {
    synchronized (this) {
        print(x);
        newLine();
    }
}

and it calls one of the print() methods (which is also overloaded) which is defined as.

public void print(boolean b) {
    write(b ? "true" : "false");
}

This means that when you call println with a boolean the jvm will call the shown methods and will print "true" or "false"

If you set up your IDE correctly you can explore the source code of the java libraries so you can see the methods I posted above.

As for the size, as others have posted, the answer is machine dependent as shown here What is the size of a boolean variable in Java?

Community
  • 1
  • 1
triadiktyo
  • 479
  • 2
  • 9
  • @learner Good answer, but if this answers your question, you didn't really want to know about the internal size or representation at all. – user207421 Oct 04 '15 at 10:11
  • Also make sure you understand the difference between Boolean and boolean. Boolean is an object but boolean is primitive. In your example you are actually dealing with a boolean which would be the output of 5 > 6. – triadiktyo Oct 04 '15 at 10:12
  • @EJP yeah, I wanted to know about why false is printed as well as size and internal representation. Your answer gives some part of my answer, and also Subdivision's. That's why I accepted this answer. – learner Oct 04 '15 at 11:04
1

Boolean has two types, true & false.

No. Boolean is a type, and it has two values, true and false.

how does true and false work? How is it stored in memory? Does it require 1 bit space or more?

It isn't defined, but the wording in the JVM spec mostly supports storage as a 0- or 1-valued byte.

Is it some type of string?

No.

So, what is the size of false?

It isn't defined, and there is no way you can discover it.

It looks like a string in java.

Not on the evidence you've presented so far.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thank you for the answer.What about "false" output. Does it not look like a string? How boolean is processed internally? – learner Oct 04 '15 at 09:51
  • 1
    @learner The output text 'false' arises out of the behaviour of `System.out.println(boolean)`. I suggest you look it up. It doesn't prove anything about the internal representation. – user207421 Oct 04 '15 at 09:53
  • 1
    @learner When you print it, it's printed as text "true" or "false". That doesn't mean that it is some kind of string, or that it is stored as a string. The only thing you need to know is that it has only two possible values. How it's stored in memory is up to the Java virtual machine, and it can be different in different implementations of the JVM. – Jesper Oct 04 '15 at 09:55