Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > locals es loco?

Reply
Thread Tools

locals es loco?

 
 
bob smith
Guest
Posts: n/a
 
      10-04-2012
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?

 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      10-04-2012
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.

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-04-2012
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-04-2012
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-05-2012
On Thu, 4 Oct 2012 07:50:32 -0700 (PDT), bob smith
<(E-Mail Removed)> 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.


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-05-2012
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
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      10-05-2012
Lew <(E-Mail Removed)> 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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-05-2012
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-05-2012
On Fri, 5 Oct 2012 10:55:20 -0700 (PDT), Lew <(E-Mail Removed)>
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Locals window show variable in RED? dgk ASP .Net 2 05-22-2006 06:24 PM
Does NetFlix have drop boxes for locals? The Professor DVD Video 10 12-23-2004 07:03 AM
How do I use the Locals Window to find a value in a Dataset???? Aaron Ackerman ASP .Net 5 10-13-2003 08:39 PM
How safe is modifying locals()? Paul Paterson Python 15 07-28-2003 09:57 PM
Question about "exec in globals, locals" Giles Brown Python 2 07-04-2003 10:51 AM



Advertisments