This question already has answers here:
Difference between null and empty ("") Java String
(22 answers)
Closed 5 years ago.
The main reason for asking this question is the particualr differences of how String is perceived in different languages. I am from a C++ background and now working in Java, where String is immutable. So I wanted to know the difference between
String m = "" ;
and
String n = null ;
Would be interesting to know if the pointer of m points to a memory location specially in java?
String m = null ;
does not refer an object.
String m = "" ;
refers an object.
When you initialize it as "", you are assigning a value to the string. However, when you initialize it as null, it actually points to nothing but null. Eventually, string methods can be used on the first (equals, length,replaceAll,...), while you cannot use them on the second.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
Double d1 = null;
Double d2 = null;
System.out.println(d1+d2);//throw NullPointerException
String s1 = null;
String s2 = null;
System.out.println(s1+s2);//doesn't throw any exception prints nullnull
Since both Double and String are of type Object then why Double types throw exception??
Just to elaborate on #Seelenvirtuose's comment.
The + operator on two expressions of type Double is interpreted as mathematical addition. So it tries to extract the numerical value from the objects and add them together. Since the variables in this case are holding null instead of objects, you get a NullPointerException.
The + operator on two expressions of type String is interpreted as concatenation. There are rules about how you deal with null in a concatenation: it's transformed into the string "null". So that operation can be performed without an error.
This question already has answers here:
When is `StringBuilder` is preferred instead appending `String` to `String`? [duplicate]
(2 answers)
Closed 7 years ago.
Say I have 3 String variables:
String firstName = "aaa";
String middleName = "bbb";
String lastName = "cccc";
To append string variables we can use:
String fullName = firstName + "-" + middleName + "-" +lastName ;
Or we can use StringBuilder:
StringBuilder builder = new StringBuilder();
builder.append(firstName);
builder.append("-");
builder.append(middleName);
builder.append("-");
builder.append(lastName);
String fullName = builder.toString();
When to use StringBulider or when to append it directly?
As a programmer we should always care about the readability of our code and the first case looks quite readable to me than the second one.
Now if your question is which one should I choose ?
Than answer is any of them and you should not bother for this.StringBuilder only make difference with loop, other than that currently both of the cases would be almost same.
Simplest way : If you have access to apache common, you could use StringUtils.join().
Next, if all the Strings could be marked as final, then use + directly as using + on final Strings (which are compile time constants) is actually a compile-time operation.
If, you don't want to do the 2 things above, then use StringBuilder to concatenate strings (Even though + on Strings is done using StringBuilder internally.
This question already has answers here:
replace String with another in java
(7 answers)
Closed 8 years ago.
I have string "2x+3" and I want change 'x' to string "8".
String operation = "2x+3";
String x = "8";
And I want result such as "2*8+3"
or
String operation = "x+3";
String x = "8";
And I want result such as "8+3"
Does anyone know how to solve ?
operation = operation.replace("x","8")
Well you can't change the same string, because strings are immutable objects in java. What you can do is use the replace function in string class and get a new string out of it.
Have a look at this question : replace String with another in java
This question already has answers here:
Difference between null and empty ("") Java String
(22 answers)
What is null in Java?
(14 answers)
Closed 8 years ago.
What is the differences between String str1="" and String str2 =null?
When we print str1 there is no output and when we print str2 output is null.
"" is the empty string, null is the null reference.
The first is an empty String whereas the second is a null reference to a String.
An empty String is a String with no characters.
A null reference is a reference to a String that is not existent.
There Huge Difference "" means this empty String and Second one null means there is noting to assign and its noting exists.
"" means strings is created in string pool while for second one is nothing exist.
str1 there is no output
because string is empty so its printing nothing to output while second is null so its string value is null.
The first you are creating a new String object and assigning it "" or empty String. Your variable is pointing to this string object. The second you are not creating a new String object. You are creating a pointer that is not pointing to anything it is null. When you print using "" + yourstring it will print null because of the base Class objects toString() method.
This question already has answers here:
Difference between null and empty ("") Java String
(22 answers)
Closed 9 years ago.
What is the difference between a null string (String s = null) and an empty string (String s = "")?
This is what I have:
String s1 = ""; //print statement does not print any thing for s1 but s1.length()=0
String s2 = null;//print statement prints "null" for s2 but s2.length() gives exception
What does it mean?
String s1 = ""; means that the empty String is assigned to s1.
In this case, s1.length() is the same as "".length(), which will yield 0 as expected.
String s2 = null; means that (null) or "no value at all" is assigned to s2. So this one, s2.length() is the same as null.length(), which will yield a NullPointerException as you can't call methods on null variables (pointers, sort of) in Java.
Also, a point, the statement
String s1;
Actually has the same effect as:
String s1 = null;
Whereas
String s1 = "";
Is, as said, a different thing.
Null means nothing. Its just a literal. Null is the value of reference variable. But empty string is blank.It gives the length=0. Empty string is a blank value,means the string does not have any thing.
No method can be invoked on a object which is assigned a NULL value. It will give a nullPointerException. Hence, s2.length() is giving an exception.
When Object variables are initially used in a language like Java, they have absolutely no value at all - not zero, but literally no value - that is null
For instance: String s;
If you were to use s, it would actually have a value of null, because it holds absolute nothing.
An empty string, however, is a value - it is a string of no characters.
String s; //Inits to null
String a =""; //A blank string
Null is essentially 'nothing' - it's the default 'value' (to use the term loosely) that Java assigns to any Object variable that was not initialized.
Null isn't really a value - and as such, doesn't have properties. So, calling anything that is meant to return a value - such as .length(), will invariably return an error, because 'nothing' cannot have properties.
To go into more depth, by creating s1 = ""; you are initializing an object, which can have properties, and takes up relevant space in memory. By using s2; you are designating that variable name to be a String, but are not actually assigning any value at that point.