Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #19134

Re: locals es loco?

Newsgroups comp.lang.java.programmer
Date 2012-10-05 10:55 -0700
References <4b5511c5-799d-4469-9502-cdc39491234b@googlegroups.com> <0n1t68lu75vll0319kj7v8p1i3hc9v8re3@4ax.com>
Message-ID <af02a6e4-d92f-48de-b52e-8953c948a77b@googlegroups.com> (permalink)
Subject Re: locals es loco?
From Lew <lewbloch@gmail.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

locals es loco? bob smith <bob@coolfone.comze.com> - 2012-10-04 07:50 -0700
  Re: locals es loco? markspace <-@.> - 2012-10-04 08:09 -0700
  Re: locals es loco? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-10-04 11:45 -0400
    Re: locals es loco? Lew <lewbloch@gmail.com> - 2012-10-04 12:01 -0700
  Re: locals es loco? Roedy Green <see_website@mindprod.com.invalid> - 2012-10-05 00:06 -0700
    Re: locals es loco? Lew <lewbloch@gmail.com> - 2012-10-05 10:55 -0700
      Re: locals es loco? Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-10-05 19:01 +0000
        Re: locals es loco? Lew <lewbloch@gmail.com> - 2012-10-05 12:41 -0700
      Re: locals es loco? Roedy Green <see_website@mindprod.com.invalid> - 2012-10-05 15:38 -0700

csiph-web