[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to get the size of string in Java

Posted on 2011-03-17
11
Medium Priority
?
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 800 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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 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 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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

649 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