File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes String and boolean memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String and boolean memory" Watch "String and boolean memory" New topic

String and boolean memory

Amr Khan

Joined: Jul 27, 2001
Posts: 6
Q1) How do I know that the String text below occupies how much memory?
String text=" My name is Tom. ";
Q2) What is the memory occupied by a boolean?

Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
If you actuallu meant memory, I don't know how much or why you would need to know it anyway. As for size, use the length() method on the string. I believe a boolean is 1 bit in size.


So much trouble in the world -- Bob Marley
Jason Ford
Ranch Hand

Joined: Aug 02, 2001
Posts: 54
Actually, a boolean is probably stored as a byte. Though this is an inefficient use of memory, the complications involved in addressing a single bit of memory more than justify this implementation.

Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
I believe each character in a String uses two bytes of memory, even whitespace characters.
Noorulain Khan

Joined: Jul 31, 2001
Posts: 28
is a Non-primitive data type and in actual is array.
Array is like we stack stuff somewhere with Last in First Out method. Likewise the data is stored in memory with the same method Byte by Byte. Therefore the size will vary depending upon the characters are given in a string.
However if you would want to know about the size of the string variable (veryuseful in programming as well) you can use
length() method.
In java the length and code both are not described anywhere and
at all.
But through studies i guess it occupies the least memory of rest of the datatypes.
I agree. Here's the link:
subject: String and boolean memory
It's not a secret anymore!