Test to see of a variable has been used/initialised

C

Chris Smith

Lasse said:
No. That code, if executed as the body of a method, would never access
an uninitialized variable. If the compiler could know that, it could
omit the warning.

A large point of this thread, emphasized elsewhere but perhaps also
needed here, is that the compiler could *not* omit the error (not
warning) in that case. Not if it wanted to be a Java compiler, anyway.
The compiler could conceivable be made smart enough to determine with
certainty that the value would be initialized at that point... perhaps
that information might even be useful in generating a more helpful error
message... but a compliant Java compiler *will* reject that code with an
error message and *will* fail the compile.

Otherwise, it's broken. Not badly broken, but it is broken
nevertheless.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Michael Borgwardt

Roedy said:
Yes, null is a value. Uninitialised variables is C have a random
value in them left around from the last guy who used the RAM.

Actually, I think this is platform-specific.
 
C

Chris Uppal

Eric said:
However, the rules are what a mathematician might call
"Sufficient but not necessary" in that they complain about
non-initialization in situations where it cannot happen.

I'm pretty sure (have been all along) that we agree on the facts. I'm just
quibbling over the terminology that you are using which I think can give the
wrong impression.

I hadn't been intending to add to this thread, but reading some of the
responses on other branches (sorry, mixed metaphor), I think it's worth one
more post.


For instance, I would not say:
the compiler is smart enough to
know that `true' is true, but not smart enough to know that
array lengths are non-negative.

I would say that the compiler is MUST NOT be smart enough to know...

(I'm using capitals here in the sense of RFC 2119, not shouting at you ;-)

It is illegal, in Java, for it to handle that case as common-sense would seem
to indicate.

I'm sure Neil could easily add a special case to make the compiler "know" that
arrays have positive length (probably rather easy since someArray.length is a
special case in the compiler anyway), but -- in so far as his job is to
maintain the /Java/ compiler -- he isn't allowed to.

-- chris
 
S

steve

Absolutely. An unitialized variable would contatin whatever garbage was
previously hanging around in that memory location. Null, in this case, is
definitely the result of initialization.

- Virgil

please do not be silly, intialized , is bringing a variable to a known state,
"Null" is not a known state.

also
int[] dontBeSilly =null;

is not bringing dontBeSilly to a structure of ints, it will compile, but is
it intialized?
 
S

steve

Actually, I think this is platform-specific.


no null is not a value, it is not an initialized state.
null could be anything or nothing., it may be a value or an object or neither
of these.

if i have an area of memory 0-20, filled with random trash that is "null"

can i do this

memory_location1 =1
memory_location2=6

if content(memory_location1)= content(memory_location2)
print "the nulls equal each other";
end if

or will this statement only be true some of the time for some of the nulls?

actually for c the values are byte values with a KNOWN state some place
between 0 and 255.
I would like to say that "nulls cannot exist at this low level binary state
and are related only to objects", but i suspect that someone would contradict
me on this.
 
T

Tony Morris

please do not be silly, intialized , is bringing a variable to a known
state,
"Null" is not a known state.

No, but "null" certainly is (Java Language Specification, Second Edition
3.10.7)
also
int[] dontBeSilly =null;

is not bringing dontBeSilly to a structure of ints, it will compile, but is
it intialized?

Yes, it declares an array reference of int and assigns that reference to
null (JLS 3.10.7) - this is said to be "initialised" (JLS 4.5.3). If the
explicit assignment to null were not present, the array reference would be
said to be "uninitialised".
All this assumes that your statement is in a local context.

If it were not in a local context (i.e. a class member declaration and
assignment), then (perhaps contrary to popular belief) initialisation occurs
both implicitly and explicitly (two distinct occurances) to the same value
(null) - this occurs at object initialisation and construction time.

I strongly suggest you familiarise yourself with the Java Language
Specification.
Specifically,
"The Rule of Definite Assignment" (JLS 16)
"Local Variable Declaration Assignments" (JLS 14.4)
"Kinds of Variables" (JLS 4.5.3)

Also, using defamation, such as calling somebody's ideas "silly", helps
nobody (loosely speaking - I can understand how sometimes one's silliness
needs to be explicitly pointed out in order to be constructive). This
effect is exaggerated when the person making the defamatory call is actually
incorrect (as in your case).

In a nutshell, "pull your head in :), and go educate yourself".

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
T

Tony Morris

steve said:
no null is not a value, it is not an initialized state.
null could be anything or nothing., it may be a value or an object or neither
of these.

if i have an area of memory 0-20, filled with random trash that is "null"

can i do this

memory_location1 =1
memory_location2=6

if content(memory_location1)= content(memory_location2)
print "the nulls equal each other";
end if

or will this statement only be true some of the time for some of the nulls?

actually for c the values are byte values with a KNOWN state some place
between 0 and 255.
I would like to say that "nulls cannot exist at this low level binary state
and are related only to objects", but i suspect that someone would contradict
me on this.

Incorrect.
Your misunderstanding really should be corrected before attempting to
(unknowingly) mislead those who are genuinely requesting advice.

I refer you to another post on this thread that gives references, which
should assist you in gaining a further knowledge such that your
misunderstandings are corrected.

Good luck.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
R

Roedy Green

please do not be silly, intialized , is bringing a variable to a known state,
"Null" is not a known state.

Yes it is. You are too young too recall the bad old days of Fortran
and C when uninitialised variables had a RANDOM value, usually 0, so
the bug would not always show up.

With Java, references are initialised to null, a very definite value
(internally a 0 pointer). If you try to dereference them, you won't
get random junk, you will get a NullPointerException.

"Known" means you can predict value, and the value is null. It does
not mean "not initialised" null is a zero pointer.

I don't know if you are just trying to pull my chain, or if you are
budding philosopher. From a practical assembler point of view the
values are indeed determinate.
 
R

Roedy Green

Actually, I think this is platform-specific.

In the old days it was considered "efficient" not to zero ram before
you. In today's world the cost of an undetected uninitialised
variable is severe. Further it can be a security leak. You must not
be allowed to snoop of what the guy using ram before you was doing.

Way back when in the days of the 7044 and WATFOR all RAM was
initialised to bad parity. Then when you used an uninitialised
variable you got a "Trap" an interrupt.


Today the trend is to at least zero out all ram before use.
 
R

Roedy Green

no null is not a value, it is not an initialized state.
null could be anything or nothing., it may be a value or an object or neither
of these.

Perhaps is your world, but in Java null is a very concrete thing,
implemented as a 0 pointer, and explicitly checked for on every
dereference for safety.

Computerists grab words from everyday language and redefine them with
very specific meaning. null is one of those cases.


Objects in Java are completely insubstantial -- a pattern of
electrons. No one can show you a Java object on a plate.

This does not mean that people calling about garbage collecting
objects are "silly". These are just metaphors.
 
T

Tony Morris

Perhaps is your world, but in Java null is a very concrete thing,
implemented as a 0 pointer, and explicitly checked for on every
dereference for safety.

I fail to see where you get the idea that "null" is implemented by a VM as a
0 pointer.
Neither the Java VM Specification or the Java Language Specification mention
this (unless I'm blind).
If you are referring to a specific VM implementation detail, please make
clear which VM (otherwise, the statement on its own is blatantly incorrect),
along with evidence (since even if you are referring to a particular VM, I'm
still unconvinced).

I'm inclined to suggest this is incorrect, until proven otherwise.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
R

Roedy Green

I fail to see where you get the idea that "null" is implemented by a VM as a
0 pointer.

It is not in the spec, but I'd bet 99% of JVMs handle it that way
because then you can do a block 0 move to init an object. All fields
int, char, ref get the same bit pattern.


If you used a non-zero null pattern for references, your object
initialisation would have to be done field by field -- very slow.

The only reason to use some other pattern would be to interface with
some special hardware nullpointer check.
 
V

Virgil Green

steve said:
no null is not a value, it is not an initialized state.
null could be anything or nothing., it may be a value or an object or neither
of these.

null, in the Java language, is a very specific value within any given VM. A
variable containing a null reference *has* to be in a known state, else it
could not be checked to see if it was null.
if i have an area of memory 0-20, filled with random trash that is "null"

can i do this

memory_location1 =1
memory_location2=6

if content(memory_location1)= content(memory_location2)
print "the nulls equal each other";
end if

or will this statement only be true some of the time for some of the
nulls?

This code:

public class NullCheck
{
public static void main(String[] args)
{
String s1 = null;
String s2 = null;
System.out.println(s1 == s2);
}
}

when compiled with a compliant Java compiler and executed in a compliant
Java Virtual Machine will *always* produce the output:

true

Those are two reference variables, each containing a null reference, and
equal to each other -- *always* -- in Java.

- Virgil
 
V

Virgil Green

steve said:
Absolutely. An unitialized variable would contatin whatever garbage was
previously hanging around in that memory location. Null, in this case, is
definitely the result of initialization.

- Virgil

please do not be silly, intialized , is bringing a variable to a known state,
"Null" is not a known state.

also
int[] dontBeSilly =null;

is not bringing dontBeSilly to a structure of ints, it will compile, but is
it intialized?

You may call it silliness if it suits you. It is nevertheless correct. null
in Java is a known state. As for your int[] variable, dontBeSilly, that is
initialized to null. I quote the JVM
(http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html
#48440)

==========
A variable (§4.5) is a storage location. A variable of a primitive type
always holds a value of that exact type. A variable of a class type T can
hold a null reference or a reference to an instance of class T or of any
class that is a subclass of T. A variable of an interface type can hold a
null reference or a reference to any instance of any class that implements
the interface. If T is a primitive type, then a variable of type "array of
T" can hold a null reference or a reference to any array of type "array of
T"; if T is a reference type, then a variable of type "array of T" can hold
a null reference or a reference to any array of type "array of S" such that
type S is assignable (§5.2) to type T. A variable of type Object can hold a
null reference or a reference to any object, whether class interface or
array.
==========

your int[] type variable must hold either a null reference or a reference to
an array of int. Yes, it is most definitely initialized. The variable
dontBeSilly holds a null reference.

- Virgil
 
V

Virgil Green

Roedy Green said:
It is not in the spec, but I'd bet 99% of JVMs handle it that way
because then you can do a block 0 move to init an object. All fields
int, char, ref get the same bit pattern.


If you used a non-zero null pattern for references, your object
initialisation would have to be done field by field -- very slow.

The only reason to use some other pattern would be to interface with
some special hardware nullpointer check.

Most like reasonable assumptions... but they are only assumptions.

The JVM spec at
http://java.sun.com/docs/books/vmspec/2nd-edition/html/Overview.doc.html#150
79

=====
The Java virtual machine specification does not mandate a concrete value
encoding null.
=====

states that the actual value of null is undefined by the JVM spec. I
interpret that to mean it can be anything the JVM author wishes it to be.

- Virgil
 
R

Roedy Green

=====
The Java virtual machine specification does not mandate a concrete value
encoding null.
=====

states that the actual value of null is undefined by the JVM spec. I
interpret that to mean it can be anything the JVM author wishes it to be.

That's what I said. However, I also explained why you would not in
practice find a JVM with NULL defined internally as anything but 0.

The Serialisation format likely does sanctify null=0 since you have to
decide on something for interchange.
 
V

Virgil Green

Roedy Green said:
That's what I said. However, I also explained why you would not in
practice find a JVM with NULL defined internally as anything but 0.

Understood. I was merely agreeing with you and providing the reference.
The Serialisation format likely does sanctify null=0 since you have to
decide on something for interchange.

That very thought occurred to me. Some common definition needs to exist. I
didn't bother to research it, though.

- Virgil
 
J

Joona I Palaste

Roedy Green said:
Yes, null is a value. Uninitialised variables is C have a random
value in them left around from the last guy who used the RAM.
That never happens in Java. Either Java nulls/zeroes for you, or
forces you to set locals to something, possibly null/0.

I'm amazed how much people argue about this. Of course null is a value,
it's a value of type "reference to an object", and that's the type that
non-primitive variables in Java have. Null is a reference, not an
object, and it's a reference that doesn't refer to any object. Seeing
as Java has reference values, but not object values, null is a value.
It's as simple as that.
 
R

Roedy Green

I'm amazed how much people argue about this. Of course null is a value,
it's a value of type "reference to an object", and that's the type that
non-primitive variables in Java have. Null is a reference, not an
object, and it's a reference that doesn't refer to any object. Seeing
as Java has reference values, but not object values, null is a value.
It's as simple as that.

The problem is people don't do assembler any more. If they did the
answer would be obvious. References are addresses. Null is a zero
address. Objects are hunks of ram allocated on heap, no confusion at
all.
 
J

Joona I Palaste

The problem is people don't do assembler any more. If they did the
answer would be obvious. References are addresses. Null is a zero
address. Objects are hunks of ram allocated on heap, no confusion at
all.

What the heck have assembler or addresses got to do with it? Null would
be a reference value even if Java was compiled and interpreted by monks
writing on parchment instead of an electronic machine. Can't you guys
recognise language semantics when you see them?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top