a string or char

Bacardi151

Senior member
Dec 15, 2003
540
0
0
a string or a char..how big are they in java?

is a char 1 byte? and is each character in a string 1byte as well?

i.e.

public String myString = "hello";

is that myString 5bytes big?


and on another note, if i were to try to send 200KB message from a Server to a Client, could i just make an array big enough to hold about 40,000 myString variables to make the new array equal to 200KB and send that array? the purpose of that is to calculate the bandwidth by sending a package of 200KB from a server to a client.

thanks in advance for any feedback
 

element

Diamond Member
Oct 9, 1999
4,635
0
0
Originally posted by: Bacardi151
a string or a char..how big are they in java?

is a char 1 byte? and is each character in a string 1byte as well?

i.e.

public String myString = "hello";

is that myString 5bytes big?


and on another note, if i were to try to send 200KB message from a Server to a Client, could i just make an array big enough to hold about 40,000 myString variables to make the new array equal to 200KB and send that array? the purpose of that is to calculate the bandwidth by sending a package of 200KB from a server to a client.

thanks in advance for any feedback

No it's not 5 it would be 6, there is a null character at the end. But yes each character is 1 byte.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Java uses unicode characters, they're two bytes each. Each character in a String is also two bytes. If you want to use bytes, use the type "byte".
 

element

Diamond Member
Oct 9, 1999
4,635
0
0
Originally posted by: notfred
Java uses unicode characters, they're two bytes each. Each character in a String is also two bytes. If you want to use bytes, use the type "byte".

Oh oops I thought it would be the same as in C. my bad. no wonder java is bloatware.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: element
Originally posted by: notfred
Java uses unicode characters, they're two bytes each. Each character in a String is also two bytes. If you want to use bytes, use the type "byte".

Oh oops I thought it would be the same as in C. my bad. no wonder java is bloatware.

Ah yes, because multibyte characters only exist in Java.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: BingBongWongFooey
Originally posted by: element
Originally posted by: notfred
Java uses unicode characters, they're two bytes each. Each character in a String is also two bytes. If you want to use bytes, use the type "byte".

Oh oops I thought it would be the same as in C. my bad. no wonder java is bloatware.

Ah yes, because multibyte characters only exist in Java.

Of course, and they also serve no purpose other than bloat.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: BingBongWongFooey
Originally posted by: element
Originally posted by: notfred
Java uses unicode characters, they're two bytes each. Each character in a String is also two bytes. If you want to use bytes, use the type "byte".

Oh oops I thought it would be the same as in C. my bad. no wonder java is bloatware.

Ah yes, because multibyte characters only exist in Java.

pwned
 

ggavinmoss

Diamond Member
Apr 20, 2001
4,798
1
0
Originally posted by: element
Originally posted by: notfred
Java uses unicode characters, they're two bytes each. Each character in a String is also two bytes. If you want to use bytes, use the type "byte".

Oh oops I thought it would be the same as in C. my bad. no wonder java is bloatware.

Wow...

-geoff
 

element

Diamond Member
Oct 9, 1999
4,635
0
0
Originally posted by: BingBongWongFooey
Originally posted by: element
Originally posted by: notfred
Java uses unicode characters, they're two bytes each. Each character in a String is also two bytes. If you want to use bytes, use the type "byte".

Oh oops I thought it would be the same as in C. my bad. no wonder java is bloatware.

Ah yes, because multibyte characters only exist in Java.

doh! pwned again! well in c++ you use type wchar_t for unicode 2 byte long characters, but the regular char type is still 1 byte long am I right?
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: element
Originally posted by: BingBongWongFooey
Originally posted by: element
Originally posted by: notfred
Java uses unicode characters, they're two bytes each. Each character in a String is also two bytes. If you want to use bytes, use the type "byte".

Oh oops I thought it would be the same as in C. my bad. no wonder java is bloatware.

Ah yes, because multibyte characters only exist in Java.

doh! pwned again! well in c++ you use type wchar_t for unicode 2 byte long characters, but the regular char type is still 1 byte long am I right?

http://publib.boulder.ibm.com/...db.doc/ad/c0005954.htm
http://www.jchq.net/certkey/0406certkey.htm
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
interesting website amdfanboy! thanks!

i'm terribly sorry about posting in this forum, i just need an answer quick and the tech seems to be dead, but i usually get my questions addressed here, so i hope nobody minds.

so here's the deal, i'm trying to make a 200KB size of a variable that i can send from a server to a client (server and client is written in java and i'm just testing the transmission)

anyways, so if i were to use the String myString = "hello" it is actually 6 chars which in turn is 16bits/2bytes each which makes myString 12bytes?

if that's the case, and im trying to make a 200KB variable...out of an array...can i just fill the array's slots 200K/12 times through a loop?
 

element

Diamond Member
Oct 9, 1999
4,635
0
0
Originally posted by: Bacardi151
interesting website amdfanboy! thanks!

i'm terribly sorry about posting in this forum, i just need an answer quick and the tech seems to be dead, but i usually get my questions addressed here, so i hope nobody minds.

so here's the deal, i'm trying to make a 200KB size of a variable that i can send from a server to a client (server and client is written in java and i'm just testing the transmission)

anyways, so if i were to use the String myString = "hello" it is actually 6 chars which in turn is 16bits/2bytes each which makes myString 12bytes?

if that's the case, and im trying to make a 200KB variable...out of an array...can i just fill the array's slots 200K/12 times through a loop?

What about the tcp/ip headers, isn't there some overhead with that?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Bacardi151
interesting website amdfanboy! thanks!

i'm terribly sorry about posting in this forum, i just need an answer quick and the tech seems to be dead, but i usually get my questions addressed here, so i hope nobody minds.

so here's the deal, i'm trying to make a 200KB size of a variable that i can send from a server to a client (server and client is written in java and i'm just testing the transmission)

anyways, so if i were to use the String myString = "hello" it is actually 6 chars which in turn is 16bits/2bytes each which makes myString 12bytes?

if that's the case, and im trying to make a 200KB variable...out of an array...can i just fill the array's slots 200K/12 times through a loop?

byte [] kb200 = new byte [200000];

Fill with data -> send.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: Bacardi151
interesting website amdfanboy! thanks!

i'm terribly sorry about posting in this forum, i just need an answer quick and the tech seems to be dead, but i usually get my questions addressed here, so i hope nobody minds.

so here's the deal, i'm trying to make a 200KB size of a variable that i can send from a server to a client (server and client is written in java and i'm just testing the transmission)

anyways, so if i were to use the String myString = "hello" it is actually 6 chars which in turn is 16bits/2bytes each which makes myString 12bytes?

if that's the case, and im trying to make a 200KB variable...out of an array...can i just fill the array's slots 200K/12 times through a loop?

Well, you could just use the String classes getBytes() method to get the string as a byte array. Then you would write it using OutputSteam.write(byte[]). You would then read this on the client side by using an InputSteam and the string constuctor String(byte[]). You will then need to calling trim on it and get the resulting string.

That is the quick and dirty method.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: element
Originally posted by: Bacardi151
interesting website amdfanboy! thanks!

i'm terribly sorry about posting in this forum, i just need an answer quick and the tech seems to be dead, but i usually get my questions addressed here, so i hope nobody minds.

so here's the deal, i'm trying to make a 200KB size of a variable that i can send from a server to a client (server and client is written in java and i'm just testing the transmission)

anyways, so if i were to use the String myString = "hello" it is actually 6 chars which in turn is 16bits/2bytes each which makes myString 12bytes?

if that's the case, and im trying to make a 200KB variable...out of an array...can i just fill the array's slots 200K/12 times through a loop?

What about the tcp/ip headers, isn't there some overhead with that?

All handled in Java. :p
 

element

Diamond Member
Oct 9, 1999
4,635
0
0
"When a TCP implementation decides to send a packet of data to the remote peer, it first wraps the data with 20-plus bytes of data called the "header". Headers are an essential part of network protocols, because they enable the participants in the network make decisions regarding the data flowing over it. Every protocol adds headers (and sometimes trailers) to your data."

from: winsock programmers FAQ