There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
JShell is useful, very useful, but I can't remember whether plain simple variables are treated as fields would be or as local variables would be. I think it treats them like fields, in which case all uninitialised variables of reference type default to null.Steven Toxoto wrote:You can use jshell to do some quick tests . . . If you don't initialise a string, it will be null by default.
Pratik Kandalgaonkar wrote:What is the difference between
just declaring a variable
assigning variable nothing
according to my knowledge the latter one assign null
but i didn't get the former one (String j)
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
Les Morgan wrote:my understanding is that
gives you a reference variable ready to be pointed to a container; but references NULL to start.
Junilu Lacar wrote:
True only if the declaration is an instance variable. Local variables do not have a default initial value, not even null.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
I am 99% sure that is the explanation of why newly‑declared local variables must be definitely assigned to before they can be used, otherwise they fail to compile. It's all in the JLS link Junilu gave you:-Tim Holloway wrote:. . . That was a VERY big problem in C - local variables would have a value equal to whatever garbage was currently on the stack . . . the present-day Java compilers are all smart enough to detect variables that were never given a value before being read and scream bloody murder about it. . . . .
That has been the situation for as long as I can remember.That JLS section wrote:. . . For every access of a local variable or blank final field x, x must be definitely assigned before the access, or a compile-time error occurs. . . .
Campbell Ritchie wrote:That has been the situation for as long as I can remember.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
In Java®, that applies to local variables, not usually to fields or method parameters. Non‑final fields have default values, so a field would default to null.Fred Masen wrote:. . . you don't initialize the string and later on you want to use it , the compiler(with Eclipse) show an error . . .
You do realise those two initialisations are very different from each other?String j = null;
. . . String j= "";
Software Developer
Vinay Singh
The error is that the memory has not been filled with a “real” reference to an object. The exception is the result of that error.Fred Masen wrote:Yep
String j = "; has a length 0
String j = null; is NULL
I usually initialized myself that way I avoid all the crappy NullPointerException errors
No, the space for a field or an array element is allocated immediately.. . . it will throw a NullPointerException . . . it has not yet been allocated space in memory.
Not NULL, please, but null.. . .Then NULL is perfectly fine to use it.
Water! People swim in water! Even tiny ads swim in water:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
|