Solved

How to get the size of string in Java

Posted on 2011-03-17
11
217 Views
Last Modified: 2012-05-11
Hi,
In my Java code I have string. And I get this string as the following:

java.lang.String rcm = g.getRcm();

Open in new window


Now, I would like to get the size of the string rcm in terms of KB?

Thanks,
0
Comment
Question by:Tolgar
11 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 35157563
That would be something like
double kb = rcm.length() * 2 / 1024.0;

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35157574
Each char is one byte so you can read the length of your string and the result will be the number of bytes.
0
 
LVL 10

Assisted Solution

by:ALaRiva
ALaRiva earned 50 total points
ID: 35157632
From my understanding, the encoding and other factors will determine the calculation, it is not just 1 to 1 as JimyX stated.

CEHJ has good knowledge, so I'm assuming he's been down this road before, and maybe that's the closest representation to it's size or the default encoding of java.

Here's a decent thread discussing it - http://stackoverflow.com/questions/409765/does-a-strings-length-equal-the-byte-size

hth

- Anthony
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 35157654
>>Each char is one byte

Each char in a java string is *two* bytes - it effectively uses UTF16 encoding
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 35157702
how abt converting to byte array and use its length to calculate actual size?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35157736
>>how abt converting to byte array

Why do that?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 35157750
>>Why do that?

not to worry abt enconding?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35157806
>>not to worry abt enconding?

Well,

a. there are no encoding issues: a String is always UTF-16 (essentially)
b. turning it from a String into byte[] would bring up encoding issues, meaning the in-memory representation would vary depending on the encoding used
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 35157852
>>a String is always UTF-16

isn't just by default and not always?

>>meaning the in-memory representation would vary depending on the encoding use

isn't( in memory ) the KB represent?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 35157880
looks like there is a tool... http://www.javamex.com/classmexer/
0
 
LVL 92

Expert Comment

by:objects
ID: 35160694
its not as simple as that :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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
iterator/ListIterator approach 17 50
how to debug htl and js pages 8 53
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 40
hashmap order 17 40
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

679 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