Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > I did not know that was legal

Reply
Thread Tools

I did not know that was legal

 
 
Roedy Green
Guest
Posts: n/a
 
      02-24-2006
I have using Intellij. One thing it often does in make suggestions on
how to improve your code. One it made, I thought was illegal till I
tried it out.


If you have code like this:

case 1:
int x = expression;
break;
case 2:
x = expression;
break;

that is LEGAL even though program flow does not flow through the
definition. You DON'T have to promote x out of the switch. You can
think of it that it works AS IF you always fell through each case. The
key to this mystery is understanding that local variables are not
actually allocated where you define them. They all get allocated at
once when you enter the method by reserving N stack slots, where they
effectively became extra dummy parameters to your method.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
paul@atom.sbrk.co.uk
Guest
Posts: n/a
 
      02-24-2006
In article <(E-Mail Removed)>, Roedy Green wrote:
> I have using Intellij. One thing it often does in make suggestions on
> how to improve your code. One it made, I thought was illegal till I
> tried it out.
>
>
> If you have code like this:
>
> case 1:
> int x = expression;
> break;
> case 2:
> x = expression;
> break;
>
> that is LEGAL


It might be legal, but does it really *improve* your code??

Paul
 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      02-24-2006
Roedy Green wrote:
>
> If you have code like this:
>
> case 1:
> int x = expression;
> break;
> case 2:
> x = expression;
> break;
>
> that is LEGAL even though program flow does not flow through the
> definition. You DON'T have to promote x out of the switch.

A way to write it a little less irritating is:

switch (...) {
int x;
case 1:
x = expression;
break;
case 2:
x = expression;
break;
}

--
"Thomas:Fritsch$ops:de".replace(':','.').replace(' $','@')
 
Reply With Quote
 
Timo Stamm
Guest
Posts: n/a
 
      02-24-2006
Roedy Green schrieb:
> The
> key to this mystery is understanding that local variables are not
> actually allocated where you define them. They all get allocated at
> once when you enter the method


It's not per method, but per block:

{
int x = 0;
}
x = 3; // unknown variable



Timo
 
Reply With Quote
 
Daniel Dyer
Guest
Posts: n/a
 
      02-24-2006
On Fri, 24 Feb 2006 11:51:03 -0000, Roedy Green
<(E-Mail Removed) > wrote:

> I have using Intellij. One thing it often does in make suggestions on
> how to improve your code. One it made, I thought was illegal till I
> tried it out.
>
>
> If you have code like this:
>
> case 1:
> int x = expression;
> break;
> case 2:
> x = expression;
> break;
>
> that is LEGAL even though program flow does not flow through the
> definition. You DON'T have to promote x out of the switch. You can
> think of it that it works AS IF you always fell through each case. The
> key to this mystery is understanding that local variables are not
> actually allocated where you define them. They all get allocated at
> once when you enter the method by reserving N stack slots, where they
> effectively became extra dummy parameters to your method.


It doesn't work if you put curly brackets around your case code like this,
which is how I prefer to write my switches:

case 1:
{
int x = expression;
break;
}
case 2:
{
x = expression;
break;
}


Dan.


--
Daniel Dyer
http://www.dandyer.co.uk
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-24-2006
On 24 Feb 2006 12:06:58 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote, quoted or
indirectly quoted someone who said :

>It might be legal, but does it really *improve* your code??


That is debatable, but in does have the effect of narrowing the scope
of a variable, which is general is a good thing. I mention this not
to advocate strange looking code, but so that you will believe you
eyes when you see it.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      02-24-2006
Daniel Dyer wrote:
> On Fri, 24 Feb 2006 11:51:03 -0000, Roedy Green
> <(E-Mail Removed) > wrote:
>
>> I have using Intellij. One thing it often does in make suggestions on
>> how to improve your code. One it made, I thought was illegal till I
>> tried it out.
>>
>>
>> If you have code like this:
>>
>> case 1:
>> int x = expression;
>> break;
>> case 2:
>> x = expression;
>> break;
>>
>> that is LEGAL even though program flow does not flow through the
>> definition. You DON'T have to promote x out of the switch. You can
>> think of it that it works AS IF you always fell through each case. The
>> key to this mystery is understanding that local variables are not
>> actually allocated where you define them. They all get allocated at
>> once when you enter the method by reserving N stack slots, where they
>> effectively became extra dummy parameters to your method.

>
>
> It doesn't work if you put curly brackets around your case code like
> this, which is how I prefer to write my switches:
>
> case 1:
> {
> int x = expression;
> break;
> }
> case 2:
> {
> x = expression;
> break;
> }


Those don't seem to be the same x.


> javac Main.java

Main.java:13: cannot find symbol
symbol : variable x
location: class Main
x = expression;
^
Main.java:18: cannot find symbol
symbol : variable x
location: class Main
System.out.println(x);
^
2 errors

---------------------------------------------

class Main {
public static void main(String[] args) {
int expression = 42;

switch(2) {
case 1:
{
int x = expression;
break;
}
case 2:
{
x = expression;
break;
}
}

System.out.println(x);

}
}
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-24-2006
On Fri, 24 Feb 2006 14:09:40 +0100, Timo Stamm <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>> key to this mystery is understanding that local variables are not
>> actually allocated where you define them. They all get allocated at
>> once when you enter the method

>
>It's not per method, but per block:
>
> {
> int x = 0;
> }
> x = 3; // unknown variable
>
>
>
>Timo


From my reading the JVM virtual machine spec, that is not correct.
they are all allocated slots on method entry. The compiler could share
slots when it is safe, but the actual allocation is done as part of
the call to set up the stack frame which sets the base for allocating
parameters and local variables all on the stack. When you call any
method you push parameters for it, then its return address and jump
the method. It then allocates its locals by bumping the stack pointer
up enough slots to leave room.

The only time this causes serious trouble is in recursion, because you
get a multiplier effect for every bit of padding in the local
variables.

At the JVM level, the type of each variable in a local slot is fixed.
You could not use the slot for a reference then recycle it as an int.
Of course AOT or Hotspot can do that sort of optimisation so long as
the next effect is the same.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-24-2006
On Fri, 24 Feb 2006 14:38:43 GMT, Jeffrey Schwab
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>> case 1:
>> {
>> int x = expression;
>> break;
>> }
>> case 2:
>> {
>> x = expression;
>> break;
>> }

>
>Those don't seem to be the same x.


in that "case" you have logically two different local variables named
x.


case 1:
{
int x = expression;
break;
}
case 2:
{
int x = expression;
break;
}

which is most of the time what you intend. You doing similar little
calculations for each case. x is local to each case.

By the way, for you Pascal programmers, those {} have no speed
penalty. They are purely for explaining your scope. No new block gets
allocated.

the JVM is very simple minded. local variables are numbered slot
numbers. If you allocated them on the fly, the compiler would need to
track stack depth. Not that hard, but then the slot number would
change depending no how many temporaries were on the stack. That would
complicate low level debugging. So Sun allocated all locals up front
so they could have fixed slot numbers.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Timo Stamm
Guest
Posts: n/a
 
      02-24-2006
Jeffrey Schwab schrieb:
> Daniel Dyer wrote:
>> It doesn't work if you put curly brackets around your case code like
>> this, which is how I prefer to write my switches:
>>
>> case 1:
>> {
>> int x = expression;
>> break;
>> }
>> case 2:
>> {
>> x = expression;
>> break;
>> }

>
> Those don't seem to be the same x.


I think that was Daniels point. The curly brackets define a new block.
So the local variables are only visible within each block.


Timo
 
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
Did this stop working? Or did it ever work? (metamagic) Daniel Waite Ruby 2 05-01-2008 11:08 PM
I did the run, cmd thing, and my video actually did mess up. The Maitch Computer Support 0 04-25-2005 11:37 PM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM
cisco t1 isdn call to DID or DID no ring tone optimus Cisco 1 12-31-2003 04:35 PM
Did you know weird MCSE 1 12-02-2003 08:46 PM



Advertisments