Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   locals es loco? (http://www.velocityreviews.com/forums/t953038-locals-es-loco.html)

bob smith 10-04-2012 02:50 PM

locals es loco?
 
I'm trying to come up with a mnemonic that helps me remember which variables get auto-initialized in Java. Is it just locals that you have to explicitly initialize?


markspace 10-04-2012 03:09 PM

Re: locals es loco?
 
On 10/4/2012 7:50 AM, bob smith wrote:
> I'm trying to come up with a mnemonic that helps me remember which
> variables get auto-initialized in Java. Is it just locals that you
> have to explicitly initialize?
>



I think so. There's class variables, instance variables, local
variables, and method arguments off the top of my head. The first two
are always initialized to null/0/false if you don't init them.
Technically method arguments need to be initialized, but the compiler
won't let you call a method without supplying all of the arguments.


Eric Sosman 10-04-2012 03:45 PM

Re: locals es loco?
 
On 10/4/2012 10:50 AM, bob smith wrote:
> I'm trying to come up with a mnemonic that helps me remember which variables get auto-initialized in Java. Is it just locals that you have to explicitly initialize?


Fields (both instance fields and static fields) are always
initialized to zero/false/null as appropriate.

Parameters of methods and constructors are always initialized
by the caller's argument expressions.

The variable in a `catch' clause is initialized to refer to
whatever was thrown.

Nothing else is initialized until and unless you initialize it.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Lew 10-04-2012 07:01 PM

Re: locals es loco?
 
Eric Sosman wrote:
> bob smith wrote:
>> I'm trying to come up with a mnemonic that helps me remember which
>> variables get auto-initialized in Java. Is it just locals that you have to explicitly initialize?>


> Fields (both instance fields and static fields) are always
> initialized to zero/false/null as appropriate.
>
> Parameters of methods and constructors are always initialized
> by the caller's argument expressions.
>
> The variable in a `catch' clause is initialized to refer to
> whatever was thrown.
>
> Nothing else is initialized until and unless you initialize it.


http://docs.oracle.com/javase/specs/...ml/jls-16.html
http://docs.oracle.com/javase/specs/...html#jls-8.3.2
http://docs.oracle.com/javase/specs/...html#jls-9.3.1

--
Lew

Roedy Green 10-05-2012 07:06 AM

Re: locals es loco?
 
On Thu, 4 Oct 2012 07:50:32 -0700 (PDT), bob smith
<bob@coolfone.comze.com> wrote, quoted or indirectly quoted someone
who said :

>I'm trying to come up with a mnemonic that helps me
> remember which variables get auto-initialized in Java.
>Is it just locals that you have to explicitly initialize?


you have static, instance and locals.
Anything else?

Of those, only locals need explicit initialisation, but you can do
things like this:

int n;
if ( a > 1 ){ n = 1; }
else { n = 2; }

you could of course abbreviate that to
int n = (a > 1) ? 1 : 2 ;
--
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex.



Lew 10-05-2012 05:55 PM

Re: locals es loco?
 
Roedy Green wrote:
> bob smith wrote, quoted or indirectly quoted someone who said :
>> I'm trying to come up with a mnemonic that helps me
>> remember which variables get auto-initialized in Java.
>> Is it just locals that you have to explicitly initialize?

>
> you have static, instance and locals.
> Anything else?


Nope.

> Of those, only locals need explicit initialisation, but you can do
> things like this:
>
> int n;
> if ( a > 1 ){ n = 1; }
> else { n = 2; }
>
> you could of course abbreviate that to
> int n = (a > 1) ? 1 : 2 ;


The semantics of initialization, the second form Roedy shows, are slightly different
from the semantics of program execution in the first form.

For example, the second form allows the variable to be 'final'.

Also notice Roedy's use of parentheses in the ternary expression. The language doesn't
require them, but he uses them to improve readability of the expression.

I don't know of any language where the ternary operator's precedence is higher than
the comparison operator's, nor where assignment trumps the ternary. Still, the
plethora of obscure operators tends to obscure meaning, so many programmers use
parentheses this way. It's considered good practice.

Not that that resolves all controversy. Some might prefer

int n = (a > 1 ? 1 : 2);

I for one would not denigrate

int n = a > 1 ? 1 : 2;

as a Java code reviewer, but I'd accept the comment as a reviewee.

--
Lew

Andreas Leitgeb 10-05-2012 07:01 PM

Re: locals es loco?
 
Lew <lewbloch@gmail.com> wrote:
> Roedy Green wrote:
>> int n;
>> if ( a > 1 ){ n = 1; }
>> else { n = 2; }
>>
>> you could of course abbreviate that to
>> int n = (a > 1) ? 1 : 2 ;

>
> The semantics of initialization, the second form Roedy shows, are slightly different
> from the semantics of program execution in the first form.
>
> For example, the second form allows the variable to be 'final'.


So does the first.


Lew 10-05-2012 07:41 PM

Re: locals es loco?
 
Andreas Leitgeb wrote:
> Lew wrote:
>
>> Roedy Green wrote:
>>> int n;
>>> if ( a > 1 ){ n = 1; }
>>> else { n = 2; }

>
>>> you could of course abbreviate that to
>>> int n = (a > 1) ? 1 : 2 ;

>
>> The semantics of initialization, the second form Roedy shows, are slightly different
>> from the semantics of program execution in the first form.
>> For example, the second form allows the variable to be 'final'.

>
> So does the first.


Oops.

Correction noted.

--
Lew

Roedy Green 10-05-2012 10:38 PM

Re: locals es loco?
 
On Fri, 5 Oct 2012 10:55:20 -0700 (PDT), Lew <lewbloch@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>
>For example, the second form allows the variable to be 'final'.


So can the first. I do it all the time. I like to insert final every
possible place it can go.

The compiler is quite clever about detecting missing initialisation
code. For example if you declare prior to a switch for example, the
compiler will complain if you forget to initialise it in one of the
cases.
--
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex.




All times are GMT. The time now is 10:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.