Java 1.6 becoming idiot-proof or just slack?

Discussion in 'Java' started by Joshua Cranmer, Mar 10, 2007.

  1. Richard Maher wrote:
    > Hi,
    >
    > I've had the following code running on Java 1.6 happily for sometime and
    > only after someone tried to compile it with 1.4 did I discover my recurring
    > :-( school-boy error of saying intVar = new Object. I've changed the code so
    > that it now it says intVar = Integer.parseInt(stringStuff) but I was just
    > wondering why it ever worked. JAVAC "just knew" I wanted the .value()
    > instead of the object or is this really useful functionality that comes in
    > Java 1B :)


    Ever hear of autoboxing? int's and Integers can freely convert between
    the other most of the time. (Came from 1.5)
    Joshua Cranmer, Mar 10, 2007
    #1
    1. Advertising

  2. Hi,

    I've had the following code running on Java 1.6 happily for sometime and
    only after someone tried to compile it with 1.4 did I discover my recurring
    :-( school-boy error of saying intVar = new Object. I've changed the code so
    that it now it says intVar = Integer.parseInt(stringStuff) but I was just
    wondering why it ever worked. JAVAC "just knew" I wanted the .value()
    instead of the object or is this really useful functionality that comes in
    Java 1B :)

    Regards Richard Maher

    public class DateVMS
    {
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
    int hsecs;

    DateVMS (String inDate)
    {
    this.year = new Integer(inDate.substring( 0, 4));
    this.month = new Integer(inDate.substring( 4, 6));
    this.month--;
    this.day = new Integer(inDate.substring( 6, 8));
    this.hour = new Integer(inDate.substring( 8, 10));
    this.minute = new Integer(inDate.substring(10, 12));
    this.second = new Integer(inDate.substring(12, 14));
    this.hsecs = new Integer(inDate.substring(14, 16));
    }
    public int getYear()
    {
    return year;
    }
    Richard Maher, Mar 10, 2007
    #2
    1. Advertising

  3. Joshua Cranmer

    Lew Guest

    > Richard Maher wrote:
    >> Hi,
    >>
    >> I've had the following code running on Java 1.6 happily for sometime and
    >> only after someone tried to compile it with 1.4 did I discover my
    >> recurring
    >> :-( school-boy error of saying intVar = new Object. I've changed the
    >> code so
    >> that it now it says intVar = Integer.parseInt(stringStuff) but I was just
    >> wondering why it ever worked. JAVAC "just knew" I wanted the .value()
    >> instead of the object or is this really useful functionality that
    >> comes in Java 1B :)


    Joshua Cranmer wrote:
    > Ever hear of autoboxing? int's and Integers can freely convert between
    > the other most of the time. (Came from 1.5)


    Until I read your response I was scratching my head trying to figure out where
    in the posted code the OP's difficulty was. I just could not see anywhere in
    the posted code a

    >> :-( school-boy error of saying intVar = new Object.


    It takes a gift of active listening to step outside the problem statement to
    discern the real issue. It was instructive to me to find my blind spot in this
    case.

    -- Lew
    Lew, Mar 10, 2007
    #3
  4. Joshua Cranmer

    Daniel Pitts Guest

    On Mar 9, 5:41 pm, "Richard Maher" <>
    wrote:
    > Hi,
    >
    > I've had the following code running on Java 1.6 happily for sometime and
    > only after someone tried to compile it with 1.4 did I discover my recurring
    > :-( school-boy error of saying intVar = new Object. I've changed the code so
    > that it now it says intVar = Integer.parseInt(stringStuff) but I was just
    > wondering why it ever worked. JAVAC "just knew" I wanted the .value()
    > instead of the object or is this really useful functionality that comes in
    > Java 1B :)
    >
    > Regards Richard Maher
    >
    > public class DateVMS
    > {
    > int year;
    > int month;
    > int day;
    > int hour;
    > int minute;
    > int second;
    > int hsecs;
    >
    > DateVMS (String inDate)
    > {
    > this.year = new Integer(inDate.substring( 0, 4));
    > this.month = new Integer(inDate.substring( 4, 6));
    > this.month--;
    > this.day = new Integer(inDate.substring( 6, 8));
    > this.hour = new Integer(inDate.substring( 8, 10));
    > this.minute = new Integer(inDate.substring(10, 12));
    > this.second = new Integer(inDate.substring(12, 14));
    > this.hsecs = new Integer(inDate.substring(14, 16));
    > }
    > public int getYear()
    > {
    > return year;
    > }


    Java 1.5 introduced something called auto boxing/unboxing, which
    allows primitives to be automatically boxed by there Object
    counterpoints, and visa versa.

    In any case, you'd be better off using
    Integer.parseInt(stringToParse). It returns an "int" rather than an
    "Integer". Your existing code has the effect of parsing an int,
    creating an Integer object around it, and then converting it BACK to
    an int.

    Oops :)
    Daniel Pitts, Mar 10, 2007
    #4
  5. Joshua Cranmer

    Lew Guest

    Daniel Pitts wrote:
    > Java 1.5 introduced something called auto boxing/unboxing, which
    > allows primitives to be automatically boxed by there Object
    > counterpoints, and visa versa.
    >
    > In any case, you'd be better off using
    > Integer.parseInt(stringToParse). It returns an "int" rather than an
    > "Integer". Your existing code has the effect of parsing an int,
    > creating an Integer object around it, and then converting it BACK to
    > an int.
    >
    > Oops :)


    Conceivably the JIT could optimize that away. In fact, static analysis of the
    program would lead to inlining and optimizing that whole mechanism away.

    The real question is what best expresses the programmer's intentions, which in
    this instance argues for the parseInt() call, but it looks like Java is
    intentionally blurring the distinction between primitives and their boxed
    versions.

    Weirdly, Java tries to make it easier on programmers with autoboxing, then we
    aren't comfortable with it so we go back to the more rigorous ways anyhow.

    -- Lew
    Lew, Mar 10, 2007
    #5
  6. Joshua Cranmer

    Ashoka! Guest

    On Mar 10, 9:07 am, Lew <> wrote:
    > Daniel Pitts wrote:
    > > Java 1.5 introduced something called auto boxing/unboxing, which
    > > allows primitives to be automatically boxed by there Object
    > > counterpoints, and visa versa.

    >
    > > In any case, you'd be better off using
    > > Integer.parseInt(stringToParse). It returns an "int" rather than an
    > > "Integer". Your existing code has the effect of parsing an int,
    > > creating an Integer object around it, and then converting it BACK to
    > > an int.

    >
    > > Oops :)

    >
    > Conceivably the JIT could optimize that away. In fact, static analysis of the
    > program would lead to inlining and optimizing that whole mechanism away.
    >
    > The real question is what best expresses the programmer's intentions, which in
    > this instance argues for the parseInt() call, but it looks like Java is
    > intentionally blurring the distinction between primitives and their boxed
    > versions.
    >
    > Weirdly, Java tries to make it easier on programmers with autoboxing, then we
    > aren't comfortable with it so we go back to the more rigorous ways anyhow.
    >
    > -- Lew


    We are from the old school Lew, Java is copying C# with this auto
    boxing stuff. If you have worked with C# you know the problems it can
    lead to errors in parts of the code you have never written and the
    only way to correct them is by changing project settings.

    regards
    Usman Ismail
    Ashoka!, Mar 10, 2007
    #6
  7. Joshua Cranmer

    Chris Uppal Guest

    Lew wrote:

    > Weirdly, Java tries to make it easier on programmers with autoboxing,
    > then we aren't comfortable with it so we go back to the more rigorous
    > ways anyhow.


    If you are the kind of person who likes that kind of ambiguity (between
    primitive values and their object wrappers) then presumably you wouldn't have
    been attracted to Java in the first place. (The "you" is generic here, of
    course, not personal).

    Some people like -- or at least, don't really notice -- semantic slush, but why
    would they want to use Java ?

    -- chris
    Chris Uppal, Mar 10, 2007
    #7
  8. Lew wrote:
    > Weirdly, Java tries to make it easier on programmers with autoboxing,
    > then we aren't comfortable with it so we go back to the more rigorous
    > ways anyhow.
    >
    > -- Lew


    If I remember correctly, the main impetus for auto-boxing/-unboxing was
    because of generics, thus it is probably only intended for generics.
    I've never used it outside of the Collections, although it is
    conceivable that a few classes in Swing are slightly helped by the
    introduction of auto-boxing/-unboxing.
    Joshua Cranmer, Mar 10, 2007
    #8
  9. Joshua Cranmer

    Daniel Pitts Guest

    On Mar 10, 10:43 am, Joshua Cranmer <>
    wrote:
    > Lew wrote:
    > > Weirdly, Java tries to make it easier on programmers with autoboxing,
    > > then we aren't comfortable with it so we go back to the more rigorous
    > > ways anyhow.

    >
    > > -- Lew

    >
    > If I remember correctly, the main impetus for auto-boxing/-unboxing was
    > because of generics, thus it is probably only intended for generics.
    > I've never used it outside of the Collections, although it is
    > conceivable that a few classes in Swing are slightly helped by the
    > introduction of auto-boxing/-unboxing.


    It has some "usefulness". It makes code "look" cleaner if you have
    need to convert between primitives and objects.

    For instance, if you have a Form object which can take an optional
    integer.

    public class Form {
    private Integer myInteger;
    public void setMyInteger(Integer i) {
    myInteger = i;
    }
    public Integer getMyInteger() {
    return myInteger;
    }
    }

    But, say you have a case where you need a default value of 43...

    In the past, you would have to have written
    myForm.setMyInteger(new Integer(43));
    but now, you can safely use myForm.setMyInteger(43);
    Daniel Pitts, Mar 10, 2007
    #9
  10. Hi Daniel,

    > Your existing code has the effect of parsing an int,
    > creating an Integer object around it, and then converting it BACK to
    > an int.
    >
    > Oops :)


    Bit of room to move on the performance front then :)

    I've gone for the Integer.parseInt(myString) strategy and all is well, but I
    couldn't let the opportunity go without whinging about the JavaScript
    version of parseInt() and why my 13 byte alert message was being truncated
    to 11 bytes. No one here probably cares but just in case. . .

    I receive 3 ASCII numeric digits over the network which tells my client how
    many more bytes to read to get the message text; in this case "013" or
    thirteen bytes to come. No with Java you get a lovely 13 returned from
    parseInt("013") but with Javascript you get 11 because JS has taken it upon
    itself to deem the leading "0" as a radix identifier and tell me it's octal
    :-( So now I have to tell it it's decimal with the second parameter.
    Standards eh?

    Cheers Richard Maher

    "Daniel Pitts" <> wrote in message
    news:...
    > On Mar 9, 5:41 pm, "Richard Maher" <>
    > wrote:
    > > Hi,
    > >
    > > I've had the following code running on Java 1.6 happily for sometime and
    > > only after someone tried to compile it with 1.4 did I discover my

    recurring
    > > :-( school-boy error of saying intVar = new Object. I've changed the

    code so
    > > that it now it says intVar = Integer.parseInt(stringStuff) but I was

    just
    > > wondering why it ever worked. JAVAC "just knew" I wanted the .value()
    > > instead of the object or is this really useful functionality that comes

    in
    > > Java 1B :)
    > >
    > > Regards Richard Maher
    > >
    > > public class DateVMS
    > > {
    > > int year;
    > > int month;
    > > int day;
    > > int hour;
    > > int minute;
    > > int second;
    > > int hsecs;
    > >
    > > DateVMS (String inDate)
    > > {
    > > this.year = new Integer(inDate.substring( 0, 4));
    > > this.month = new Integer(inDate.substring( 4, 6));
    > > this.month--;
    > > this.day = new Integer(inDate.substring( 6, 8));
    > > this.hour = new Integer(inDate.substring( 8, 10));
    > > this.minute = new Integer(inDate.substring(10, 12));
    > > this.second = new Integer(inDate.substring(12, 14));
    > > this.hsecs = new Integer(inDate.substring(14, 16));
    > > }
    > > public int getYear()
    > > {
    > > return year;
    > > }

    >
    > Java 1.5 introduced something called auto boxing/unboxing, which
    > allows primitives to be automatically boxed by there Object
    > counterpoints, and visa versa.
    >
    > In any case, you'd be better off using
    > Integer.parseInt(stringToParse). It returns an "int" rather than an
    > "Integer". Your existing code has the effect of parsing an int,
    > creating an Integer object around it, and then converting it BACK to
    > an int.
    >
    > Oops :)
    >
    Richard Maher, Mar 18, 2007
    #10
  11. Hi Joshua,

    > Ever hear of autoboxing?


    Nope.

    But I've looked it up now thanks to your reply.

    Cheers Richard Maher

    "Joshua Cranmer" <> wrote in message
    news:NEnIh.55$I56.42@trnddc06...
    > Richard Maher wrote:
    > > Hi,
    > >
    > > I've had the following code running on Java 1.6 happily for sometime and
    > > only after someone tried to compile it with 1.4 did I discover my

    recurring
    > > :-( school-boy error of saying intVar = new Object. I've changed the

    code so
    > > that it now it says intVar = Integer.parseInt(stringStuff) but I was

    just
    > > wondering why it ever worked. JAVAC "just knew" I wanted the .value()
    > > instead of the object or is this really useful functionality that comes

    in
    > > Java 1B :)

    >
    > Ever hear of autoboxing? int's and Integers can freely convert between
    > the other most of the time. (Came from 1.5)
    Richard Maher, Mar 18, 2007
    #11
  12. Richard Maher wrote:
    > I receive 3 ASCII numeric digits over the network which tells my client how
    > many more bytes to read to get the message text; in this case "013" or
    > thirteen bytes to come. No with Java you get a lovely 13 returned from
    > parseInt("013") but with Javascript you get 11 because JS has taken it upon
    > itself to deem the leading "0" as a radix identifier and tell me it's octal
    > :-( So now I have to tell it it's decimal with the second parameter.
    > Standards eh?


    Java and JavaScript share 4 letters in the name, but not that much else.

    Different language different rules.

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Mar 18, 2007
    #12
  13. Joshua Cranmer

    Guest

    On 3ÔÂ10ÈÕ, ÉÏÎç9ʱ41·Ö, "Richard Maher" <>
    wrote:
    > Hi,
    >
    > I've had the following code running on Java 1.6 happily for sometime and
    > only after someone tried to compile it with 1.4 did I discover my recurring
    > :-( school-boy error of saying intVar = new Object. I've changed the code so
    > that it now it says intVar = Integer.parseInt(stringStuff) but I was just
    > wondering why it ever worked. JAVAC "just knew" I wanted the .value()
    > instead of the object or is this really useful functionality that comes in
    > Java 1B :)
    >
    > Regards Richard Maher
    >
    > public class DateVMS
    > {
    > int year;
    > int month;
    > int day;
    > int hour;
    > int minute;
    > int second;
    > int hsecs;
    >
    > DateVMS (String inDate)
    > {
    > this.year = new Integer(inDate.substring( 0, 4));
    > this.month = new Integer(inDate.substring( 4, 6));
    > this.month--;
    > this.day = new Integer(inDate.substring( 6, 8));
    > this.hour = new Integer(inDate.substring( 8, 10));
    > this.minute = new Integer(inDate.substring(10, 12));
    > this.second = new Integer(inDate.substring(12, 14));
    > this.hsecs = new Integer(inDate.substring(14, 16));
    > }
    > public int getYear()
    > {
    > return year;
    > }


    More see here!
    http://www.flash50.com/index.php
    , Mar 18, 2007
    #13
  14. Joshua Cranmer

    Lew Guest

    wrote:
    > More see here!
    > http://www.spam...50.com/index.php


    What a piece of crap your website is, fy4. It's poorly laid out, poorly
    written, won't work if Javascript is disabled, visually ugly and has
    absolutely nothing to do with Java. Garbage.

    I hope this feedback is useful to you. Others have already spoken to your
    bloody offensive spam practice. Also garbage.

    -- Lew
    Lew, Mar 18, 2007
    #14
    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. Replies:
    3
    Views:
    309
  2. knight

    Worst Case Slack

    knight, Aug 23, 2008, in forum: VHDL
    Replies:
    0
    Views:
    1,158
    knight
    Aug 23, 2008
  3. c64
    Replies:
    0
    Views:
    1,791
  4. debayan_p

    Question on "slack"

    debayan_p, Nov 12, 2009, in forum: VHDL
    Replies:
    1
    Views:
    636
    debayan_p
    Nov 13, 2009
  5. sagar g

    negative slack

    sagar g, Sep 12, 2012, in forum: VHDL
    Replies:
    2
    Views:
    959
Loading...

Share This Page