Solved

How to get the size of string in Java

Posted on 2011-03-17
11
214 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 68
What is JNDI datasource in spring 1 50
Strange loading of website behaviour 3 31
VB Script to add site to Java Exception List 4 48
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

919 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