0

What is the default value(or garbage value) of String?

22nd Mar 2018, 5:24 PM
Napster
Napster - avatar
13 Answers
+ 10
java and modern languages are dummies safe, you cannot have undefined behaviors. The fact that the compiler complains is good! You know you made a mistake and also where you did it! Isn't that awesome?
22nd Mar 2018, 7:35 PM
AZTECCO
AZTECCO - avatar
+ 10
Right @Martin ! thanks for the good clarification!
25th Mar 2018, 4:51 PM
AZTECCO
AZTECCO - avatar
+ 9
In java variables are objects, you cannot have garbage, they are always initialized (maybe to null). You cannot have strange values, because the object style is a powerful protection against set, initialize and get operations. 2nd is always a good thing to make the compiler complain if something wrong.. this way you'll find bugs easily. For example.. what is the difference below ? if ( a == 0 ) if ( 0 == a ) I hope I was more clear and I given something to you.
24th Mar 2018, 10:06 PM
AZTECCO
AZTECCO - avatar
+ 8
@Napster what's not clear?
23rd Mar 2018, 9:03 PM
AZTECCO
AZTECCO - avatar
+ 4
@Napster Here is an example I wrote up to explain what I meant. When it's coming from the class level, you get default values even if you don't initialize the variables after declaring them. If you do it on a local level, as per the example above that you said didn't work, then you'll get the error that you received about needing to initialize the variable. That's because local variables don't get default values. https://code.sololearn.com/cqbhzTHmVQap/#java class myClass { // Class level member declared but not initalized. Defaults to null String myStr; } public class Program { public static void main(String[] args) { // TEST THE CLASS LEVEL VARIABLE BY CHECKING FOR NULL VALUE myClass obj = new myClass(); if(obj.myStr == null) { System.out.println("See? It's null, which is why this returned true."); } } } ::::: OUTPUT ::::: See? It's null, which is why this returned true.
22nd Mar 2018, 7:21 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
default value of String is null String s1; System.out.println(s1); // null
22nd Mar 2018, 5:57 PM
Manorama
Manorama - avatar
+ 2
@Napster When you declare a variable on the local level, it isn't assigned a default value, so in the case of his example there isn't a default value; it is undefined. However, when you're doing this from a class level, it IS assigned default values. In that case, the default value will be null. Hope that helps.
22nd Mar 2018, 7:15 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
@Napster Great! Glad to hear that it's making a bit more sense to you now. Happy to help.
23rd Mar 2018, 3:28 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
@ Jakob Marley...Thanks a lot.Finally I understood it. Ur explanation was very easy to understand.Hope u will continue to help the Sololearners.😀😁☺😊
23rd Mar 2018, 1:42 PM
Napster
Napster - avatar
+ 1
@Martin Taylor..It worked in my computer.Thank u!!!😊😀
23rd Mar 2018, 1:43 PM
Napster
Napster - avatar
+ 1
@AZTECCO What r u trying to say?
23rd Mar 2018, 3:48 PM
Napster
Napster - avatar
+ 1
@AZTECCO I couldn't understand ur answer.It would be very nice of u to explain what u r trying to say
24th Mar 2018, 5:23 PM
Napster
Napster - avatar
0
@Manorama I tried this in java.It didn't work.It says that s1 isn't initialized.But thanks anyway😊😀😁
22nd Mar 2018, 6:25 PM
Napster
Napster - avatar