Results 1 to 6 of 6
  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Confusion in implicit conversion

    Following is the code of show current time from the book and code from one of the blogger in Internet.

    Question 1
    What is the use of converting to int in here?
    Java Code:
    long currentSecond = (int)(totalSeconds % 60);
    I see no difference between even in the output if use
    Java Code:
    int currentSecond = totalSeconds % 60;
    Question 2
    Why the book example does not make the same long xxx = (int) xxx for current minute and current hour as in code by the blogger below.

    Below is the code from the book:
    Java Code:
      public static void main(String[] args) {
        // Obtain the total milliseconds since midnight, Jan 1, 1970
        long totalMilliseconds = System.currentTimeMillis();
    
        // Obtain the total seconds since midnight, Jan 1, 1970
        long totalSeconds = totalMilliseconds / 1000;
    
        // Compute the current second in the minute in the hour
        long currentSecond = (int)(totalSeconds % 60);
    
        // Obtain the total minutes
        long totalMinutes = totalSeconds / 60;
    
        // Compute the current minute in the hour
        long currentMinute = totalMinutes % 60;
    
        // Obtain the total hours
        long totalHours = totalMinutes / 60;
    
        // Compute the current hour
        long currentHour = totalHours % 24;
    
        // Display results
        System.out.println("Current time is " + currentHour + ":"
          + currentMinute + ":" + currentSecond + " GMT");
    }
    Below is the code from one of blogger from the internet:

    Java Code:
    public static void main(String[] args)
    {
          long totalMilliseconds = System.currentTimeMillis();
    
          long totalSeconds = totalMilliseconds / 1000L;
    
          int currentSecond = (int)(totalSeconds % 60L);
    
          long totalMinutes = totalSeconds / 60L;
    
          int currentMinute = (int)(totalMinutes % 60L);
    
          long totalHours = totalMinutes / 60L;
    
          int currentHour = (int)(totalHours % 24L);
    
        System.out.println("Current time is " + currentHour + ":"
          + currentMinute + ":" + currentSecond + " GMT");
    }
    Last edited by dojob; 08-05-2013 at 12:33 PM.

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,760
    Rep Power
    5

    Default Re: Confusion in implicit conversion

    1) its more common to use an int type; long types are usually only used when something doesn't actually fit into an int. Many standard API methods expect an int for example, such as the Calendar class. The question would have been more clear if you had copy/pasted the code properly; the type of the variable is not long but int according to your last code dump

    2) because its not strictly necessary to do so? You'd have to ask the author of the book
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default Re: Confusion in implicit conversion

    The cast to int is completey unnecessary; another much more 'dangerous' flaw in the code above is the fact that longs are signed: after 0x7fffffffffffffff millis after 1970 the above calculations all end up wrong; if I'm not mistaken that'll be in the year 292490278AD

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Confusion in implicit conversion

    Quote Originally Posted by gimbal2 View Post
    1) its more common to use an int type; long types are usually only used when something doesn't actually fit into an int. Many standard API methods expect an int for example, such as the Calendar class. The question would have been more clear if you had copy/pasted the code properly; the type of the variable is not long but int according to your last code dump

    2) because its not strictly necessary to do so? You'd have to ask the author of the book
    Oops sorry for my mistake. It supposed to be

    Java Code:
    long currentSecond = (int)(totalSeconds % 60);
    and

    Java Code:
    int currentSecond = totalSeconds % 60;
    Have corrected the above code.

    I guess you are right. Using int than long is more suitable in this case as it will not exceed the maximum value of int.
    Last edited by dojob; 08-05-2013 at 12:34 PM.

  5. #5
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Confusion in implicit conversion

    Quote Originally Posted by JosAH View Post
    The cast to int is completey unnecessary; another much more 'dangerous' flaw in the code above is the fact that longs are signed: after 0x7fffffffffffffff millis after 1970 the above calculations all end up wrong; if I'm not mistaken that'll be in the year 292490278AD

    kind regards,

    Jos
    Are you suggesting that a better way to do is by

    Java Code:
    long currentSecond = totalSeconds % 60;
    Why is this better than

    Java Code:
    int currentSecond = (int)(totalSeconds % 60);

    Anyway, the flaw that mentioned above is due to overflow. Since that you mentioned it, is there a better way to deal with it as long is the primitive data type that can hold the maximum value of integer.

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,760
    Rep Power
    5

    Default Re: Confusion in implicit conversion

    I'm getting confused here who is talking about what as at one point the discussion is about milliseconds and the other it is about seconds :s
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Type Conversion Confusion
    By lala in forum New To Java
    Replies: 8
    Last Post: 01-07-2011, 08:59 AM
  2. Implicit object in jsp declared where?
    By mahendra.athneria in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 08-11-2009, 02:23 PM
  3. Implicit objects
    By JavaJunkie in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-20-2009, 06:21 AM
  4. JSP implicit objects
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •