wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Sizeof For Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sizeof For Java" Watch "Sizeof For Java" New topic
Author

Sizeof For Java

rini john
Greenhorn

Joined: Mar 13, 2011
Posts: 7
Hi,

I would like to know if in java is it possible to know the size of a string as in java there is no sizeof() operater....

Thanks
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

The short answer is 'no' but you might get a different answer if you explain why you think you need this.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

String.length(). However it will depend on the encoding you're using what the actual number of bytes is (that get written to a stream).


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
rini john
Greenhorn

Joined: Mar 13, 2011
Posts: 7
In our application we want process a message which is a string but application can not process message which are more than 32 KB size.So for this we need to check the size of the message.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

rini john wrote:In our application we want process a message which is a string but application can not process message which are more than 32 KB size.So for this we need to check the size of the message.


may be what you need. I would suggest that you use "utf-8" for the encoding but depending on the language you are processing you may find another encoding gives you a shorter resulting set of bytes.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

String has a few getBytes() methods, that will return a byte array given a character set. You can then get the length of it.

Edit: or what James said .
rini john
Greenhorn

Joined: Mar 13, 2011
Posts: 7
thanks......
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sizeof For Java
 
Similar Threads
Find the data type of a variable
Size of an Object
difference between exe file and batch file
memory allocation
size-of primitive in java