Solved

How to get the size of string in Java

Posted on 2011-03-17
11
209 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java virtual machine monitoring tools 3 39
sumHeights2  challenge 7 76
countHi challenge 25 84
maven example error 3 35
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now