Untangling "efficient" code in java

Discussion in 'Java' started by New, Dec 31, 2003.

  1. New

    New Guest

    Hello.

    I am trying to reproduce the same results so I can prove to myself I
    understand what is happening:

    This prints a time in hh:mm:ss PM/AM
    Date now = new Date();
    out.println("DEBUG: " + DateFormat.getTimeInstance().format(now));

    I am trying to expand the above to make sure I understand it:

    I keep getting errors (I'm using JSP so the errors are hard to read)
    so I don't think it's correct:

    Date now = new Date();
    DateFormat df1 = new DateFormat();
    df1.getTimeInstance();
    DateFormat df2 = new DateFormat();
    String str = new String("");
    str = df2.format(now);
    out.println("DEBUG: " + str);

    Where did I go wrong?

    Thank you, New
     
    New, Dec 31, 2003
    #1
    1. Advertising

  2. (New) writes:

    > DateFormat df1 = new DateFormat();


    You don't need this, because

    > df1.getTimeInstance();


    is a static method that gets the current locale's default format, OR
    if you have the US-oriented JRE: the American format you seem to get.

    Also, you get it then immediately throw it away.

    > DateFormat df2 = new DateFormat();


    This creates an uninitiated DateFormat.

    > String str = new String("");


    That was unneccesary since you reassign it

    > str = df2.format(now);


    here.

    > Where did I go wrong?


    You never actually used the DateFormat you wanted.
     
    Tor Iver Wilhelmsen, Dec 31, 2003
    #2
    1. Advertising

  3. New

    Tony Morris Guest

    > Date now = new Date();
    > DateFormat df1 = new DateFormat(); // object created and assigned to df1

    reference // which is never again used (except for
    the waste of time on the next line)
    > df1.getTimeInstance(); // should be a static call

    (DateFormat.getTimeInstance()) and the
    // return value is not kept anyway, useless line
    > DateFormat df2 = new DateFormat(); // object created and assigned to df2

    reference.
    > String str = new String(""); // HORRIBLE abuse of the String(String)

    constructor.
    // Look up how Strings and object
    references are handled in Java, //
    particularly, the "interning" of literals.
    // Assigned to an object that is never
    used as str is
    // reassigned on the next line.
    > str = df2.format(now);
    > out.println("DEBUG: " + str);


    // This entire thing could have written as
    // out.println("DEBUG: " + new DateFormat().format(new Date()));
    // Your call.
    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software

    "New" <> wrote in message
    news:...
    > Hello.
    >
    > I am trying to reproduce the same results so I can prove to myself I
    > understand what is happening:
    >
    > This prints a time in hh:mm:ss PM/AM
    > Date now = new Date();
    > out.println("DEBUG: " + DateFormat.getTimeInstance().format(now));
    >
    > I am trying to expand the above to make sure I understand it:
    >
    > I keep getting errors (I'm using JSP so the errors are hard to read)
    > so I don't think it's correct:
    >
    > Date now = new Date();
    > DateFormat df1 = new DateFormat();
    > df1.getTimeInstance();
    > DateFormat df2 = new DateFormat();
    > String str = new String("");
    > str = df2.format(now);
    > out.println("DEBUG: " + str);
    >
    > Where did I go wrong?
    >
    > Thank you, New
     
    Tony Morris, Dec 31, 2003
    #3
  4. New

    New Guest

    I know it is less efficient...I was trying to explain that I need to
    break it down into "less efficient" code UNTIL I understand the
    "efficient" code.

    "Tony Morris" <> wrote in message news:<3ff35fd2$0$18751$>...
    > > Date now = new Date();
    > > DateFormat df1 = new DateFormat(); // object created and assigned to df1

    > reference // which is never again used (except for
    > the waste of time on the next line)
    > > df1.getTimeInstance(); // should be a static call

    > (DateFormat.getTimeInstance()) and the
    > // return value is not kept anyway, useless line
    > > DateFormat df2 = new DateFormat(); // object created and assigned to df2

    > reference.
    > > String str = new String(""); // HORRIBLE abuse of the String(String)

    > constructor.
    > // Look up how Strings and object
    > references are handled in Java, //
    > particularly, the "interning" of literals.
    > // Assigned to an object that is never
    > used as str is
    > // reassigned on the next line.
    > > str = df2.format(now);
    > > out.println("DEBUG: " + str);

    >
    > // This entire thing could have written as
    > // out.println("DEBUG: " + new DateFormat().format(new Date()));
    > // Your call.
    > --
    > Tony Morris
    > (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    > Software Engineer
    > IBM Australia - Tivoli Security Software
    >
    > "New" <> wrote in message
    > news:...
    > > Hello.
    > >
    > > I am trying to reproduce the same results so I can prove to myself I
    > > understand what is happening:
    > >
    > > This prints a time in hh:mm:ss PM/AM
    > > Date now = new Date();
    > > out.println("DEBUG: " + DateFormat.getTimeInstance().format(now));
    > >
    > > I am trying to expand the above to make sure I understand it:
    > >
    > > I keep getting errors (I'm using JSP so the errors are hard to read)
    > > so I don't think it's correct:
    > >
    > > Date now = new Date();
    > > DateFormat df1 = new DateFormat();
    > > df1.getTimeInstance();
    > > DateFormat df2 = new DateFormat();
    > > String str = new String("");
    > > str = df2.format(now);
    > > out.println("DEBUG: " + str);
    > >
    > > Where did I go wrong?
    > >
    > > Thank you, New
     
    New, Jan 2, 2004
    #4
  5. "New" <> wrote in message
    news:...>
    "Tony Morris" <> wrote in message
    news:<3ff35fd2$0$18751$>...
    > > > Date now = new Date();
    > > > DateFormat df1 = new DateFormat(); // object created and assigned to

    df1
    > > reference // which is never again used (except

    for
    > > the waste of time on the next line)
    > > > df1.getTimeInstance(); // should be a static call

    > > (DateFormat.getTimeInstance()) and the
    > > // return value is not kept anyway, useless line

    .....
    > I know it is less efficient...I was trying to explain that I need to
    > break it down into "less efficient" code UNTIL I understand the
    > "efficient" code.


    Please note that what Tony was saying above
    does not constitute code that is less efficient, it
    is code that does _nothing_. Big difference.

    Try and understand what Tony was saying..
    (There are some important points there)

    And could I ask that you put your comments
    _after_ what you are commenting on, rather
    than before? It (top-posting) makes converstaions
    hard to understand.

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Jan 3, 2004
    #5
  6. New

    Oscar Kind Guest

    New <> wrote:
    > I am trying to reproduce the same results so I can prove to myself I
    > understand what is happening:
    >
    > This prints a time in hh:mm:ss PM/AM
    > Date now = new Date();
    > out.println("DEBUG: " + DateFormat.getTimeInstance().format(now));
    >
    > I am trying to expand the above to make sure I understand it:
    >

    <complicated code>
    >
    > Where did I go wrong?


    You chose a complicated way to break it down, using too many objects.
    This resulted, as others have pointed out, in errors. The result is even
    more difficult to understand.

    Personally, I'd break the code down as follows, using comments and empty
    lines. Note I also added a few extra options for the (time) format
    (commented out).

    // Get the current time.
    Date now = new Date();

    // Get the default time format for the default locale.
    DateFormat timeFormat = DateFormat.getTimeInstance();

    // (commented out) Get the full time format for the default locale.
    //DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.FULL);

    // (commented out) Get the short time format for The Netherlands.
    //Locale theNetherlands = new Locale("nl", "NL");
    //DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT,
    // theNetherlands);

    // Format the current time using the time format above.
    String formattedTime = timeFormat.format(now);

    // Debug log.
    out.println("DEBUG: " + formattedTime);


    Oscar

    --
    No trees were harmed in creating this message.
    However, a large number of electrons were terribly inconvenienced.
     
    Oscar Kind, Jan 3, 2004
    #6
    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. barry
    Replies:
    5
    Views:
    3,875
    Roedy Green
    Oct 16, 2003
  2. gonzlobo

    Writing more efficient code

    gonzlobo, Jan 1, 2007, in forum: Python
    Replies:
    13
    Views:
    466
    Hendrik van Rooyen
    Jan 3, 2007
  3. Sanny
    Replies:
    22
    Views:
    1,044
    Andreas Leitgeb
    Apr 24, 2008
  4. W. eWatson
    Replies:
    21
    Views:
    748
    Rhodri James
    Feb 15, 2009
  5. Hal Fulton
    Replies:
    16
    Views:
    219
    Randy Kramer
    May 2, 2005
Loading...

Share This Page