why is Java the way it is?

Discussion in 'Java' started by Roedy Green, Aug 27, 2008.

  1. Roedy Green

    Roedy Green Guest

    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?
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 27, 2008
    #1
    1. Advertising

  2. Roedy Green

    EJP Guest

    Roedy Green wrote:
    > If for can do it, why not while?


    Good question. The content of an 'if' statement is essentially either
    (<statement>; <expression>; <statement>)
    or
    (<declaration>; <expression>; <statement>).

    The content of a while-condition is just <expression>.

    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.
    EJP, Aug 27, 2008
    #2
    1. Advertising

  3. Roedy Green

    Roedy Green Guest

    On Wed, 27 Aug 2008 06:28:53 GMT, EJP
    <> 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.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 27, 2008
    #3
  4. Roedy Green <> wrote:

    >Why is this illegal?


    I don't know the reason but...

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


    ....this code does not compile. you have to write

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

    >If for can do it, why not while?


    why then you don't use for?

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

    and if you prefer using while and want to redefine variable place
    later again you can put the code block into braces:

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

    int place = 42;

    Dirk
    Dirk Michaelsen, Aug 27, 2008
    #4
  5. Roedy Green

    Ian Semmel Guest

    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?


    Well, none of these statements compile on my java but

    Probably because they are different statements

    while ( booleanExpression) statement;

    for (ForInit;Expression;ForUpdate) statement
    where
    ForInit is StatementExpressionList OR LocalVariableDefinition

    You can't define a variable in a boolean expression

    This is ok
    int place;
    boolean b = ( ( place = s.indexOf ( 'a' ) ) >= 0 );
    while ( b ) {}

    This is not
    boolean b = ( ( int place = s.indexOf ( 'a' ) ) >= 0 );
    Ian Semmel, Aug 27, 2008
    #5
  6. Roedy Green

    Motosauro Guest

    Ian Semmel ha scritto:
    >
    >
    > 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?

    >
    > Well, none of these statements compile on my java but
    >
    > Probably because they are different statements
    >
    > while ( booleanExpression) statement;
    >
    > for (ForInit;Expression;ForUpdate) statement
    > where
    > ForInit is StatementExpressionList OR LocalVariableDefinition
    >
    > You can't define a variable in a boolean expression
    >
    > This is ok
    > int place;
    > boolean b = ( ( place = s.indexOf ( 'a' ) ) >= 0 );
    > while ( b ) {}
    >
    > This is not
    > boolean b = ( ( int place = s.indexOf ( 'a' ) ) >= 0 );


    Ok, but in the first case b gets evaluated only the first time:
    if it evaluates to true it would be like writing
    while(true){}

    You should put the evaluation inside the while loop, or change it into a
    do-while loop, in order to assign the 'place' variable before evaluating it

    What I would do though would be :
    int place = 0;
    while(s.indexOf(x)>=0){
    place = s.indexOf(x);
    /* do something with place*/
    x++; /* or whatever counter would be appropriate */
    }
    Motosauro, Aug 27, 2008
    #6
  7. Roedy Green

    mv1945 Guest

    Roedy Green escribió:
    > Why is this illegal?
    >
    > while ( int place = s.indexOf( x ) >= 0 )
    > {
    > ..
    > }
    > you have to write it:
    >


    First al all: place must be boolean



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

    There are no sense to redefine place in each iteraction

    > If for can do it, why not while?
    >

    In a for sentence eg:
    for ( int i=0; i<10;i++)
    int i=0 initilize the variable but it the same al time.

    Regards
    mv1945, Aug 27, 2008
    #7
  8. Roedy Green

    Tom Anderson Guest

    On Wed, 27 Aug 2008, Ian Semmel wrote:

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

    >
    > while ( booleanExpression) statement;
    >
    > for (ForInit;Expression;ForUpdate) statement
    > where
    > ForInit is StatementExpressionList OR LocalVariableDefinition
    >
    > You can't define a variable in a boolean expression


    Or indeed, any expression. This:

    int x = (int y = 1) + 1;

    Isn't legal. This:

    int y ;
    int x = (y = 1) + 1;

    Is. That's the crux of it.

    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.

    tom

    --
    It not infrequently happens that something about the earth, about the sky,
    about other elements of this world, about the motion and rotation or even
    the magnitude and distances of the stars, about definite eclipses of the
    sun and moon, about the passage of years and seasons, about the nature
    of animals, of fruits, of stones, and of other such things, may be known
    with the greatest certainty by reasoning or by experience. -- St Augustine
    Tom Anderson, Aug 27, 2008
    #8
  9. 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, I don't find that annoying at all. I prefer the
    'for' anyway.

    --
    DF.
    Daniele Futtorovic, Aug 27, 2008
    #9
  10. > 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.
    Andreas Leitgeb, Aug 27, 2008
    #10
  11. 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
    Patricia Shanahan, Aug 27, 2008
    #11
  12. Roedy Green

    Daniel Pitts Guest

    Roedy Green wrote:
    > On Wed, 27 Aug 2008 06:28:53 GMT, EJP
    > <> 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/>
    Daniel Pitts, Aug 27, 2008
    #12
  13. Roedy Green

    Tom Anderson Guest

    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
    Tom Anderson, Aug 27, 2008
    #13
  14. 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
    Roland de Ruiter, Aug 27, 2008
    #14
  15. 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
    Claudio Nieder, Aug 27, 2008
    #15
  16. Claudio Nieder <> 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.
    Andreas Leitgeb, Aug 27, 2008
    #16
  17. 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.
    Daniele Futtorovic, Aug 27, 2008
    #17
  18. Tom Anderson wrote:
    > This:
    >
    > int place ;
    > while ((place = s.indexOf(x)) >= 0 ) ...
    >
    > Can be written:
    >
    > for (int place; (place = s.indexOf(x)) >= 0;) ...


    Or

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

    Which isn't bad except for the redunancy in the first and third clauses.
    Mike Schilling, Aug 28, 2008
    #18
  19. Roedy Green

    Roedy Green Guest

    On Wed, 27 Aug 2008 07:48:26 -0700, Daniel Pitts
    <> wrote, quoted or indirectly
    quoted someone who said :

    >void myMethod() {
    > doSomething();
    > {
    > int foo = 10;
    > while (foo < 100) {
    > foo = nextFoo(foo);
    > }
    > // foo is not in scope here.
    > doSomethingElse();
    >}

    The problem you need an ugly extra pair of {}, which even in your case
    failed to balance. Figuring out nesting and fixing broken nesting is
    by far the syntax feature of Java what wastes the most of my time and
    the one that I find hardest just to eyeball.
    I suggest some ways out in my Scid project. See
    http://mindprod.com/project/scid.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 28, 2008
    #19
  20. Roedy Green

    Roedy Green Guest

    On Wed, 27 Aug 2008 12:39:23 +0100, Tom Anderson
    <> wrote, quoted or indirectly quoted someone who
    said :

    >int x = (int y = 1) + 1;
    >
    >Isn't legal. This:


    Why not? I am not asking about the language as defined now, but what
    the language designers might have been thinking when they concocted
    the syntax.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 28, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Why, why, why???

    =?Utf-8?B?VGltOjouLg==?=, Jan 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    549
    Juan T. Llibre
    Jan 27, 2005
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    443
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    853
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,737
    Smokey Grindel
    Dec 2, 2006
  5. manish sahu
    Replies:
    3
    Views:
    988
Loading...

Share This Page