why is Java the way it is?

This is a discussion on why is Java the way it is? within the Java forums in Programming Languages category; Eric Sosman wrote: > [...] > for (int place; (place = s.indexOf(x) >= 0; ) Oh, drat. Precedence and syntax bugs seem rampant throughout this whole thread. What I *meant* was for (int place; (place = s.indexOf(x)) >= 0; ) -- Eric Sosman esosman@ieee-dot-org.inva lid...

Go Back   Application Development Forum > Programming Languages > Java

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #11  
Old 08-27-2008, 08:24 AM
Eric Sosman
Guest
 
Default Re: why is Java the way it is?

Eric Sosman wrote:
> [...]
> for (int place; (place = s.indexOf(x) >= 0; )


Oh, drat. Precedence and syntax bugs seem rampant
throughout this whole thread. What I *meant* was

for (int place; (place = s.indexOf(x)) >= 0; )

--
Eric Sosman
esosman@ieee-dot-org.invalid
Reply With Quote
  #12  
Old 08-27-2008, 08:55 AM
Andreas Leitgeb
Guest
 
Default Re: why is Java the way it is?

> Roedy Green escribió:
>> Why is this illegal?
>> while ( int place = s.indexOf( x ) >= 0 )


It reminds me of my casual wish to just save away some
interims-result of a formula, and have the compiler
just "dup" it on the stack for immediately subsequent
use, rather than "store" it into a local variable and
(immediately subsequently) load it back from there.

But usually, I then remember the dogma about "the jit
does all the kinky magic for me - it knows *much* better
than me" and I then just save the value into a local
variable.

And then I'm wondering, why (or whether)
String all=""; for(/*some long loop*/) all += next_part;
is still my responsibility to rewrite for using explicit
StringBuilder.

Reply With Quote
  #13  
Old 08-27-2008, 09:27 AM
Patricia Shanahan
Guest
 
Default Re: why is Java the way it is?

Roedy Green wrote:
> Why is this illegal?
>
> while ( int place = s.indexOf( x ) >= 0 )
> {
> ..
> }
> you have to write it:
>
> int place;
> while ( place = s.indexOf( x ) >= 0 )
> {
> ..
> }
>
>
> If for can do it, why not while?


The for loop has an initialization clause whose result, if any, is not
used, so a declaration fits in nicely there.

Patricia
Reply With Quote
  #14  
Old 08-27-2008, 10:48 AM
Daniel Pitts
Guest
 
Default Re: why is Java the way it is?

Roedy Green wrote:
> On Wed, 27 Aug 2008 06:28:53 GMT, EJP
> <esmond.not.pitt@not.bigpond.com> wrote, quoted or indirectly quoted
> someone who said :
>
>> Allowing a <declaration> in a while-condition would be +less+
>> symmetrical with 'for' than <expression> is: it would imply
>> re-initialization of the item every time around the loop, which is
>> asymmetric with 'for', where it only happens once.

>
> That makes sense logically. Pragmatically it forces you to expose
> variables to wider scope than they should have.

No, but you can accidentally expose them to a wider scope if you don't
think about it.
void myMethod() {
doSomething();
{
int foo = 10;
while (foo < 100) {
foo = nextFoo(foo);
}
// foo is not in scope here.
doSomethingElse();
}

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Reply With Quote
  #15  
Old 08-27-2008, 11:26 AM
Tom Anderson
Guest
 
Default Re: why is Java the way it is?

On Wed, 27 Aug 2008, Daniele Futtorovic wrote:

> On 27/08/2008 13:39, Tom Anderson allegedly wrote:
>> The for loop is an aberration, in that it has a very complicated
>> parenthesised bit.
>>
>> I do agree that this is sort of annoying, though. Being able to declare a
>> variable in the loop condition of a while would be tidier than having to
>> declare it before the loop.

>
> Given that you can use a 'for' over a 'while' any time and, AFAIAA,
> without any penalties,


Without penalties? What are those two semicolons, then?

> I don't find that annoying at all. I prefer the 'for' anyway.


Fair enough. Personally, i find the for loop syntax really clumsy.

This:

int place ;
while ((place = s.indexOf(x)) >= 0 ) ...

Can be written:

for (int place; (place = s.indexOf(x)) >= 0 ...

But that looks hell of ugly to me - it's got a repetition of the variable
name and two completely pointless semicolons.

This:

while ((int place = s.indexOf(x)) >= 0) ...

Would be much nicer. Although then you'd have to allow declarations inside
expressions, and the world would rapidly go mad. This:

this.foo = someBoolean ? (int bar = 23) : ((String bar = "baz").hashCode()) ;

Would be fun. I guess the rule would be that any statement involving a
declaration inside an expression gets rewritten as a block comprising the
declaration followed by the expression with the declaration changed to an
assignment. So Roedy's example would become:

{
int place ;
while ((place = s.indexOf(x)) >= 0 ) ...
}

Exactly as desired, and the troublemaking example would become:

{
int bar ;
String bar ;
this.foo = someBoolean ? (bar = 23) : ((bar = "baz").hashCode()) ;
}

Which would then fail to compile, because of the conflicting decarations
of bar.

You'd have to be a bit clearer about scope, though. Clearly, this:

int foo = (int bar = 23) + 1 ;
baz(foo) ;

Couldn't be rewritten to this:

{
int bar ;
int foo = (bar = 23) + 1 ;
}
baz(foo) ;

Because it screws up the scope of foo. It'd have to be:

int foo ;
{
int bar ;
foo = (bar = 23) + 1 ;
}
baz(foo) ;

There are doubtless other traps.

tom

--
Why did one straw break the camel's back? Here's the secret: the million
other straws underneath it - it's all mathematics. -- Mos Def
Reply With Quote
  #16  
Old 08-27-2008, 11:46 AM
Roland de Ruiter
Guest
 
Default Re: why is Java the way it is?

On 27-8-2008 14:24, Eric Sosman wrote:
> Eric Sosman wrote:
>> [...]
>> for (int place; (place = s.indexOf(x) >= 0; )

>
> Oh, drat. Precedence and syntax bugs seem rampant
> throughout this whole thread. What I *meant* was
>
> for (int place; (place = s.indexOf(x)) >= 0; )
>

If you really hate parenthesis (and don't mind duplicating expressions),
why not
for (int place = s.indexOf(x); place >= 0; place = s.indexOf(x))
--
Regards,

Roland
Reply With Quote
  #17  
Old 08-27-2008, 12:12 PM
Claudio Nieder
Guest
 
Default Re: why is Java the way it is?

Hi,

> And then I'm wondering, why (or whether)
> String all=""; for(/*some long loop*/) all += next_part;
> is still my responsibility to rewrite for using explicit StringBuilder.


Unless that long loop is the most time-critical or memory consuming part
in my application, I wouldn't care whether to use StringBuilder or not.

And if I think it could be, I would first benchmark it and see whether
the StringBuilder solution really saves me some time or space or if the
JIT is able to optimize it well enough.

claudio (also guilty of sometimes optimizing, when not in need to)
--
Claudio Nieder, Talweg 6, CH-8610 Uster, Tel +4179 357 6743,
www.claudio.ch
Reply With Quote
  #18  
Old 08-27-2008, 01:03 PM
Eric Sosman
Guest
 
Default Re: why is Java the way it is?

Roland de Ruiter wrote:
> On 27-8-2008 14:24, Eric Sosman wrote:
>> Eric Sosman wrote:
>>> [...]
>>> for (int place; (place = s.indexOf(x) >= 0; )

>>
>> Oh, drat. Precedence and syntax bugs seem rampant
>> throughout this whole thread. What I *meant* was
>>
>> for (int place; (place = s.indexOf(x)) >= 0; )
>>

> If you really hate parenthesis (and don't mind duplicating expressions),
> why not
> for (int place = s.indexOf(x); place >= 0; place = s.indexOf(x))


Because I mind duplicating expressions. Too much opportunity
to produce expressions that are *almost* duplicates ...

--
Eric.Sosman@sun.com
Reply With Quote
  #19  
Old 08-27-2008, 04:21 PM
Andreas Leitgeb
Guest
 
Default Re: why is Java the way it is?

Claudio Nieder <private@claudio.ch> wrote:
>> And then I'm wondering, why (or whether)
>> String all=""; for(/*some long loop*/) all += next_part;
>> is still my responsibility to rewrite for using explicit StringBuilder.


> Unless that long loop is the most time-critical or memory consuming part ...

I take that as a given, or otherwise I wouldn't care, myself
(and just write how I feel like, which may casually still involve
StringBuilder)

> or if the JIT is able to optimize it well enough.

I doubt it.

> claudio (also guilty of sometimes optimizing, when not in need to)

Sometimes it's a question of habit, when I write contrived code,
of which I know from earlier tests that it is indeed more efficient.

e.g.:
cur=it.next(); diff=cur-old; old=cur; doSomething(diff);
which I rather do like this:
doSomething(old-(old=it.next())); //(doSometing() is symmetric)

This change (well, some variant of it) actually once gave
a prog of mine a measurable speedup in overall runtime -
it was iterated almost a billion times, and doSomething
was rather cheap.

Otoh., I'm really lazy, so instead of writing:
for (int i=0; i<5; i++) ...
in non-time-critical and repetitive context, I rather do:
static private final int[] L5={0,1,2,3,4}; // just once
for (int i:L5) ...
despite its worse performance.

Reply With Quote
  #20  
Old 08-27-2008, 04:32 PM
Daniele Futtorovic
Guest
 
Default Re: why is Java the way it is?

On 27/08/2008 17:26, Tom Anderson allegedly wrote:
> On Wed, 27 Aug 2008, Daniele Futtorovic wrote:
>
>> On 27/08/2008 13:39, Tom Anderson allegedly wrote:
>>> The for loop is an aberration, in that it has a very complicated
>>> parenthesised bit.
>>>
>>> I do agree that this is sort of annoying, though. Being able to
>>> declare a variable in the loop condition of a while would be tidier
>>> than having to declare it before the loop.

>>
>> Given that you can use a 'for' over a 'while' any time and, AFAIAA,
>> without any penalties,

>
> Without penalties? What are those two semicolons, then?
>
>> I don't find that annoying at all. I prefer the 'for' anyway.

>
> Fair enough. Personally, i find the for loop syntax really clumsy.
>
> <snip, snip, snip>
>
> tom
>


Well, you sure have thought a lot about it. But as my father used to
say: ain't no use crying over spilt sperm.

One thing I'd like to note, though, is that syntactical over-cleverness
in a computer language is -- at least potentially -- a Bad Idea (tm).
IMHO Java derives[1] much of its strength from the fact that it is
rather clumsy and rigid. But rigidity leads to clarity, at least when
it's done right, and encourages discipline, which is a very important
factor. As a result, staggeringly complex constructs could be built with
Java, precisely because the building blocks were simple.

[1] ...or rather derived. It's past the critical mass by now, so that it
can rely on weight (and incumbent PR) rather than on quality. Thence the
autoboxing.

--
DF.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:53 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.