Re: Using 'final' with local variables

Discussion in 'Java' started by Arne Vajhøj, Sep 16, 2009.

  1. Arne Vajhøj

    Arne Vajhøj Guest

    rossum wrote:
    > A question for the Java gurus. When coding in C or C++ I use
    > 'register' to hint to the compiler that something might usefully be
    > kept in a place with easy/quick access:


    Good C code style in the 1980's.

    :)

    Don't do it today.

    It is a deal - the compiler does not give you hints about how
    to write code - you do not give the compiler hints about how
    to compile.


    > In Java there is no direct equivalent of 'register', the closest
    > probably is 'final' to indicate that the local variable will not
    > change. The approximate Java equivalent is:
    >
    > for (int i = 0; i < 20; ++i) {
    > final int iSquared = i * i;
    > for (int j = 0; j < 250; ++j) {
    > // Calculations involving i, iSquared and j
    > }
    > }
    >
    > However, in Java the compiler is creating byte code and the machine
    > code is created by the JIT from the bytecode so there is a less direct
    > connection between my source and the machine code.
    >
    > At last, my question: does the hint given by the 'final' keyword on a
    > local variable get through to the JIT or is it just wasted typing?
    >
    > Some days I add a 'final' to my unchanging local variables, other days
    > I can't be bothered. Which is the better practice? Is there likely
    > to be any impact on execution speed?


    The final keyword on local variable will not improve
    performance (in general).

    But some people use it anyway to ensure that the variable
    is not changed.

    I think it can be good for fields, but that it is misplaced
    for arguments and local variables because:
    - it clutters the code
    - if it is a problem to look at the entire method and see
    whether the variable is changed or not, then the method
    is too long and should be broken up

    http://www.ibm.com/developerworks/java/library/j-jtp1029.html

    talks a little bit about some of it (including the register
    keyword !).

    Arne
     
    Arne Vajhøj, Sep 16, 2009
    #1
    1. Advertising

  2. Arne Vajhøj

    Dave Searles Guest

    Arne Vajhøj wrote:
    > It is a deal - the compiler does not give you hints about how
    > to write code


    Sure it does; they're called "warnings".

    > - you do not give the compiler hints about how
    > to compile.
     
    Dave Searles, Sep 16, 2009
    #2
    1. Advertising

  3. In article <h8q2li$6tm$-september.org>, Dave Searles
    <> wrote:

    > Arne Vajhøj wrote:
    > > It is a deal - the compiler does not give you hints about how
    > > to write code

    >
    > Sure it does; they're called "warnings".

    [ SNIP ]

    And flat-out in your face errors. :)

    AHS
     
    Arved Sandstrom, Sep 16, 2009
    #3
  4. Arne Vajhøj

    Dave Searles Guest

    Arved Sandstrom wrote:
    > In article <h8q2li$6tm$-september.org>, Dave Searles
    > <> wrote:
    >
    >> Arne Vajhøj wrote:
    >>> It is a deal - the compiler does not give you hints about how
    >>> to write code

    >> Sure it does; they're called "warnings".

    > [ SNIP ]
    >
    > And flat-out in your face errors. :)


    I wouldn't call outright errors "hints". :)
     
    Dave Searles, Sep 16, 2009
    #4
  5. Arne Vajhøj

    Klaus Dicks Guest

    rossum wrote:

    > On Wed, 16 Sep 2009 07:02:05 -0400, Dave Searles
    > <> wrote:
    >
    >>Arved Sandstrom wrote:
    >>> In article <h8q2li$6tm$-september.org>, Dave Searles
    >>> <> wrote:
    >>>
    >>>> Arne Vajhøj wrote:
    >>>>> It is a deal - the compiler does not give you hints about how
    >>>>> to write code
    >>>> Sure it does; they're called "warnings".
    >>> [ SNIP ]
    >>>
    >>> And flat-out in your face errors. :)

    >>
    >>I wouldn't call outright errors "hints". :)

    > Only in the sense that being beaten about the head with a blunt object
    > is a 'hint'.


    no, that's a 'hit'... ;)
    >
    > rossum


    --
    freundliche Grüsse

    Klaus Dicks
    Organisationsprogrammierer
    SHD Einzelhandelssoftware GmbH & Co. KG,
     
    Klaus Dicks, Sep 24, 2009
    #5
    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. Jon Skeet
    Replies:
    5
    Views:
    590
    xarax
    Jul 4, 2003
  2. JFCM
    Replies:
    4
    Views:
    5,756
  3. Philipp
    Replies:
    3
    Views:
    1,147
    Roedy Green
    Nov 26, 2008
  4. markspace
    Replies:
    12
    Views:
    614
    Dave Searles
    Sep 21, 2009
  5. Jim Janney
    Replies:
    2
    Views:
    295
    Jim Janney
    Sep 22, 2009
Loading...

Share This Page