Timeline for Java: difference between String a = null and String a = new String()
Current License: CC BY-SA 3.0
22 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Dec 17, 2016 at 5:19 | history | edited | Tulains Córdova | CC BY-SA 3.0 |
edited title
|
Dec 16, 2016 at 21:35 | history | protected | gnat | ||
Dec 16, 2016 at 18:49 | answer | added | gnasher729 | timeline score: 0 | |
Dec 15, 2016 at 18:52 | history | edited | Tulains Córdova | CC BY-SA 3.0 |
added 4 characters in body
|
Aug 6, 2015 at 2:49 | comment | added | Andres F. |
If at all possible, try to write something like final String x = "the string"; . This way: a- x cannot be null, and b- x is guaranteed not to change its value during its scope, avoiding nasty surprises.
|
|
Jul 28, 2015 at 17:13 | vote | accept | Rajat Saxena | ||
Jul 27, 2015 at 16:26 | answer | added | Tulains Córdova | timeline score: 11 | |
Jul 27, 2015 at 16:25 | comment | added | user40980 |
The object allocated by new String() would be garbage collected as there are likely no other references to it. Why one does this as a two statements, I don't know. Why not String a = "Harry"; in the first place? Or leave it unassigned until you actually use it?
|
|
Jul 27, 2015 at 16:23 | comment | added | Rajat Saxena |
@MichaelT So if I do something like String a= new String() and then a = "Harry" ,in this case the object created by new String() will be lost?
|
|
Jul 27, 2015 at 16:22 | comment | added | user40980 | @Panzercrisis Correct - I don't completely agree with it. However, if one lacks the discipline and instead writes sloppy code NPE pop up everywhere (and thus the billion dollar mistake). If one is going to be undisciplined with assignments and tests, then the NullObject pattern is probably safer to use. | |
Jul 27, 2015 at 16:18 | comment | added | Panzercrisis |
@MichaelT What's the TL;DR? The guy in the video just kind of keeps rambling, and it seems like not having the distinction between null and "" (or new Object() ) would run its own risk of mistakes.
|
|
Jul 27, 2015 at 16:14 | comment | added | user40980 | @RajatSaxena Are null references really a bad thing? | |
Jul 27, 2015 at 16:13 | comment | added | Rajat Saxena |
@KilianFoth: Can you please guide me to some blog post or documentation link where I can read more about this null thing?
|
|
Jul 27, 2015 at 16:11 | comment | added | Kilian Foth |
Just to add to the confusion: null is widely considered a billion-dollar mistake. It was created because it's so easy to implement, but it introduces an unnecessary special case into program logic that causes endless troubles. If at all possible, try to program as if null didn't exist and avoid all this trouble. This means paying the price for an occasional "" here and there, but nowadays this is a good trade-off even in the small environment of mobile devices.
|
|
Jul 27, 2015 at 16:09 | comment | added | Rajat Saxena |
So it means that String a= null is a way to go?
|
|
Jul 27, 2015 at 16:09 | answer | added | DavidTheWin | timeline score: 8 | |
Jul 27, 2015 at 16:09 | comment | added | user40980 |
@Panzercrisis If the empty string is created with String foo = ""; they are all interned and take no additional space. If you are for some reason doing new String() , any reasonable static analysis or code review will flag it as "don't do this" for very good reason. I've only very rarely seen good reason to use new String() .
|
|
Jul 27, 2015 at 16:09 | review | First posts | |||
Jul 27, 2015 at 16:15 | |||||
Jul 27, 2015 at 16:08 | comment | added | Panzercrisis | Empty strings take more space and time anyway and might confuse the reader of your code about what you're doing with it further down. (They might think you're probably concatenating onto it later or something.) | |
Jul 27, 2015 at 16:07 | comment | added | dramzy |
The difference is that you'll get an empty string in the other case rather than null .
|
|
Jul 27, 2015 at 16:07 | comment | added | user40980 | That completely depends on the context of the code. Sometimes you'd rather have a NPE rather than doing a mistaken operation on an empty string and thinking everything is ok. Ideally though, you don't have either, but that will require you to write the code appropriately. | |
Jul 27, 2015 at 16:04 | history | asked | Rajat Saxena | CC BY-SA 3.0 |