Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #19097 > unrolled thread
| Started by | bob smith <bob@coolfone.comze.com> |
|---|---|
| First post | 2012-10-04 07:50 -0700 |
| Last post | 2012-10-05 15:38 -0700 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | bob smith <bob@coolfone.comze.com> |
|---|---|
| Date | 2012-10-04 07:50 -0700 |
| Subject | locals es loco? |
| Message-ID | <4b5511c5-799d-4469-9502-cdc39491234b@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-10-04 08:09 -0700 |
| Message-ID | <k4k8qk$sib$1@dont-email.me> |
| In reply to | #19097 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2012-10-04 11:45 -0400 |
| Message-ID | <k4kau4$b01$1@dont-email.me> |
| In reply to | #19097 |
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
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-10-04 12:01 -0700 |
| Message-ID | <d18b325d-165e-4ce1-96bb-fe43d961a017@googlegroups.com> |
| In reply to | #19099 |
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/jls/se7/html/jls-16.html http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.2 http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.3.1 -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-10-05 00:06 -0700 |
| Message-ID | <0n1t68lu75vll0319kj7v8p1i3hc9v8re3@4ax.com> |
| In reply to | #19097 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-10-05 10:55 -0700 |
| Message-ID | <af02a6e4-d92f-48de-b52e-8953c948a77b@googlegroups.com> |
| In reply to | #19121 |
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
[toc] | [prev] | [next] | [standalone]
| From | Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> |
|---|---|
| Date | 2012-10-05 19:01 +0000 |
| Message-ID | <slrnk6ubkl.u9l.avl@gamma.logic.tuwien.ac.at> |
| In reply to | #19134 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-10-05 12:41 -0700 |
| Message-ID | <38c00c6b-424b-4f3a-8795-5ee01b1417c2@googlegroups.com> |
| In reply to | #19140 |
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
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-10-05 15:38 -0700 |
| Message-ID | <d4ou68httuofe5k9vm2vk77mv4n7nq4j8t@4ax.com> |
| In reply to | #19134 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web