Solved

How to get the size of string in Java

Posted on 2011-03-17
11
216 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

829 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