my dog learned polymorphism*
The moose likes Beginning Java and the fly likes size of string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "size of string" Watch "size of string" New topic
Author

size of string

Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Hi,
I want to send 1K bytes string data through the socket. Just wondering, for a string as "abc", is its size 6 bytes? I know one char is 16 bits but I would like to confirm it.
Thanks
Chris
Jyothi Lature
Ranch Hand

Joined: Oct 31, 2001
Posts: 60
Hi Chris,
You are right. In java, charis 2 bytes size. So String "abc" is of size 6bytes.
Have fun,
Jyothi


Jyothi<br />Sun Certified Java Programmer<br />Brainbench Certified for Javascript
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Thanks. May I ask what exactly is happening if I send a string as "123" or ")|("? I think they are 6 bytes, too, but they are not chars, aren't they?
Chris
Jyothi Lature
Ranch Hand

Joined: Oct 31, 2001
Posts: 60
Hi Chris,
In java,
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
So, the size of this string variable will be 6 bytes.
Similarly, String "123" or ")|(" is again an array of chars whose size will be 6 bytes.
Hope its clear.
Happy coding.
-Jyothi
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
I think I was dumb .
thanks a have a good weekend
Chris
 
 
subject: size of string
 
Similar Threads
size of a string
Writing and reading string to/from a file
ASCII characters AND Java characters
File conversion program...
How to tell what is the size of an object (Map) in terms of kilo bytes.