Diffrence between String str = "' & String str =null [duplicate] - 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.

Related

Difference in initializating of String m= "" and String m = null ; [duplicate]

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.

What is the difference between this two ways of initializing String [duplicate]

This question already has answers here:
What is Java String interning?
(8 answers)
What is the difference between "text" and new String("text")?
(13 answers)
Closed 6 years ago.
This two codes have defferent outputs and i don't know why.
String a="abc";
String b="abc";
System.out.println(a==b + " " + a.equals(b));
The output is "true true"
String a="abc";
String b=new String("abc");
System.out.println(a==b + " " + a.equals(b));
The output is "false true"
when you use this
String a="abc";
String b="abc";
the java creates only one object in memory which is abc and here a and b are pointing to same object and == don't check the string content instead it check the reference value. but as soon as you do this
String b=new String("abc");
java creates a new object b in memory which is different from a ,now b and a are pointing to two different objects hence if you compare contents with equals function result will be true but if you compare reference now, result will be false
Read about it's usage
This has to be a duplicate of a large number of questions, but I will comment by saying that when you do the following:
String a = "abc";
String b = "abc";
The JVM creates a single String object in the constant pool which contains the String abc. Hence, the a and b Strings simply point to the same string in the pool.
However, when you do the following:
String a = "abc";
String b = new String("abc");
a new object is created even though abc already exists in the pool. Hence the comparison a == b returns false, although the contents of both these strings remains equivalent.

How to check a string that not empty in java? [duplicate]

This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 7 years ago.
How to check a string in java that is empty or not?
For example :
string str = " " and string str2 = " " and str3 = ""
str includes 3 spaces, str2 includes 1 space, and str3 has no characters but all of them are empty.
How do I check this in Android?
Just check with str.isEmpty() as isEmpty() method will return true for actual length if 0.
As NULL refers to memory assignment of your String str. To check empty String use isEmpty() method.
Update:
In your case, best scenario should be,
if(str != null && str.trim().length() > 0)
{
// String str is not NULL or not Empty
}else
{
// String str is NULL or Empty
}
Null != empty.
Null means the variable is not initialized, like:
String s;
Your str,str2 and str3 are initialized, zo they are not null.
To check for same characters:
str.equals("foo"), ("foo" is in your case spaces :) )
NOT str == "foo". The last means you're checking same references.
You can see if the characters match
if (str.equals ("")) {
// true
}
if (str3.equals (" ")) {
// true
}

java String and StringBuilder [duplicate]

This question already has answers here:
String can't change. But int, char can change
(7 answers)
Closed 8 years ago.
I am confuse about String and String Builder. Here is my simple code
StringBuilder sb1 = new StringBuilder("123");
String s1 = "123";
sb1.append("abc");
s1.concat("abc");
System.out.println(sb1 + " " + s1);
sb1 output for 123abc. It is ok! because it use append method.But String s1 should be abc123
but it output is abc. Why? And what is concat method purpose? Please explain me.
Thank you
.But String s1 should be abc123 but it output is abc.
Strings are immutable in Java. concat doesn't change the existing string - it returns a new string. So if you use:
String result = s1.concat("abc");
then that will be "123abc" - but s1 will still be "123". (Or rather, the value of s1 will still be a reference to a string with contents "123".)
The same is true for any other methods on String which you might expect to change the contents, e.g. replace and toLowerCase. When you call a method on string but don't use the result (as is the case here), that's pretty much always a bug.
The fact that strings are immutable is the whole reason for StringBuilder existing in the first place.
concat function not change the string but it returns the result which is not assigned in your case:
String concat(String textToAppend)
so change:
s1 = s1.concat("abc");
string objects are immutable. Immutable simply means unmodifiable or unchangeable
but if you give
String result = s1.concat("abc");
output is 123abc
and
StringBuilder are mutable
you can perform changes
s1.concat("abc") will create a new object in heap with the "abc" concatenated to s1. but s1 is still pointing to original s1 which is "123". so you need to make your s1 reference to point to new object using s1 = s1.concat("abc");

Difference between null and empty string [duplicate]

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.

Categories

Resources