Easy enough to do, but I'd love to know your reasoning. How's an uninitialize primitive any less dangerous than an uninitialized reference. Just NPE's?
That's only true for member variables. A variable declared inside a method body is in an unitialised state until it is explicitly assigned a value. This is true for primitive types and object references. If your code attempts to read a variable while it's in this state you'll get a compilation error. Try compiling the following, if you want an example:
class Example { public void example() { int a; System.out.println("a = " + a); } }
Hope that helps, Vil.
Norris Shelton wrote:
int startNum; Defaults to a valid value, 0.
Object startObject; Defaults to a null, which can cause a NPE.
That's only true for member variables. A variable declared inside a method body is in an unitialised state until it is explicitly assigned a value. This is true for primitive types and object references. If your code attempts to read a variable while it's in this state you'll get a compilation error. Try compiling the following, if you want an example:
>
class Example { public void example() { int a; System.out.println("a = " + a); } }
>
Hope that helps, Vil.
>
Norris Shelton wrote:
>
int startNum; Defaults to a valid value, 0.
> >
Object startObject; Defaults to a null, which can cause a NPE.
Reasonable, although I personally prefer to have member variables explicitly initialized. If I see "private int m_foo = 0;", it means the developer actually intended it to be initialized to 0. If I see "private int m_foo;" he might have just forgot to initialize it, accidentally removed his initialization. Nonetheless, it seems a place that people can differ, so I'll add the switch.
Reasonable, although I personally prefer to have member variables
explicitly initialized. If I see "private int m_foo = 0;", it means the developer actually intended it to be initialized to 0. If I see "private int m_foo;" he might have just forgot to initialize it, accidentally removed his initialization. Nonetheless, it seems a place that people can differ, so I'll add the switch. >
Easy enough to do, but I'd love to know your reasoning. How's an uninitialize primitive any less dangerous than an uninitialized reference. Just NPE's?
int startNum;
Defaults to a valid value, 0.
Object startObject;
Defaults to a null, which can cause a NPE.
--
Norris Shelton
Web Developer
Sun Certified Java Programmer
"Dave Griffith" <dave.griffith@cnn.com> wrote in message
news:2678419.1074884309077.JavaMail.itn@is.intellij.net...
>
uninitialize primitive any less dangerous than an uninitialized reference.
Just NPE's?
That's only true for member variables. A variable declared inside a method
body is in an unitialised state until it is explicitly assigned a value.
This is true for primitive types and object references. If your code
attempts to read a variable while it's in this state you'll get a
compilation error. Try compiling the following, if you want an example:
class Example {
public void example() {
int a;
System.out.println("a = " + a);
}
}
Hope that helps,
Vil.
Norris Shelton wrote:
--
Vilya Harvey
vilya.harvey@digitalsteps.com / digital steps /
(W) +44 (0)1483 469 480
(M) +44 (0)7816 678 457 http://www.digitalsteps.com/
Correct, this is for member variables. I should have clarified.
--
Norris Shelton
Web Developer
Sun Certified Java Programmer
"Vilya Harvey" <vilya.harvey@digitalsteps.com> wrote in message
news:bv38jj$48v$1@is.intellij.net...
>
>
>
>
>
Reasonable, although I personally prefer to have member variables explicitly initialized. If I see "private int m_foo = 0;", it means the developer actually intended it to be initialized to 0. If I see "private int m_foo;" he might have just forgot to initialize it, accidentally removed his initialization. Nonetheless, it seems a place that people can differ, so I'll add the switch.
--Dave
Thanks.
--
Norris Shelton
Web Developer
Sun Certified Java Programmer
"Dave Griffith" <dave.griffith@cnn.com> wrote in message
news:21571962.1075138220895.JavaMail.itn@is.intellij.net...
explicitly initialized. If I see "private int m_foo = 0;", it means the
developer actually intended it to be initialized to 0. If I see "private
int m_foo;" he might have just forgot to initialize it, accidentally removed
his initialization. Nonetheless, it seems a place that people can differ,
so I'll add the switch.
>