what's the deference between str=null and str=" " ????????

D

David

2 definitions:
String str1=null;
String str2="";
what's the deference??

i use the first definition, and then give str1 a value: str1="myName";
however, when i send this string from server to client, it displays:
nullmyName.
 
J

Joona I Palaste

David said:
2 definitions:
String str1=null;
String str2="";
what's the deference??

There's a pretty big difference. The first makes str1 refer to no String
object at all. The second makes str2 to refer to a valid String object,
which is exactly 0 characters long.
i use the first definition, and then give str1 a value: str1="myName";
however, when i send this string from server to client, it displays:
nullmyName.

That's because most of the java.io.* classes treat a String value of
null as a valid String, consisting of the characters 'n' 'u' 'l' 'l'.
This does *NOT* mean that it *is* that String value.

See for yourself:

System.out.println(str2.length()); // prints 0
System.out.println(str1.length()); // throws a NullPointerException

You could benefit from reading a textbook about Java programming.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"There's no business like slow business."
- Tailgunner
 
T

Thomas G. Marshall

David said:
2 definitions:
String str1=null;
String str2="";
what's the deference??

i use the first definition, and then give str1 a value: str1="myName";
however, when i send this string from server to client, it displays:
nullmyName.


Like you're likely to hear from 100 others, str1 is a null reference (think
/pointer to no object/) and str2 is a reference to an empty string.

But as you learn about strings, you're gonna come face to face with another
issue. Because strings are immutable (once created, you cannot change them
in /any/ way, neither length nor content), the compiler is free (and
required) to share data whenever possible. Odd? Not really, here:

String str1 = "hello";
String str2 = "hello";

Both of these references point to the /exact/ same string. Same 1 object!
The compiler shared the data between them. That means that

(str1 == str2) is true ...and...
str1.equals(str2) is true

This can get confusing for a newbie (sorry if you aren't one). Take for
example the following:

String str1 = "hello";
String str2 = "hell";
str2 += "o";

They are two /different/ strings, both containing the same value. So:

(str1 == str2) is false ...and...
str1.equals(str2) is true

Sorry if this explanation was beneath you: it seemed like something worth
saying.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top