# Confusion in implicit conversion

• 08-05-2013, 08:25 AM
dojob
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?
Code:

`long currentSecond = (int)(totalSeconds % 60);`
I see no difference between even in the output if use
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:
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:

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"); }```
• 08-05-2013, 08:52 AM
gimbal2
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
• 08-05-2013, 08:54 AM
JosAH
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
• 08-05-2013, 12:32 PM
dojob
Re: Confusion in implicit conversion
Quote:

Originally Posted by gimbal2
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

Code:

`long currentSecond = (int)(totalSeconds % 60);`
and

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.
• 08-05-2013, 12:44 PM
dojob
Re: Confusion in implicit conversion
Quote:

Originally Posted by JosAH
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

Code:

`long currentSecond = totalSeconds % 60;`
Why is this better than

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.
• 08-05-2013, 12:52 PM
gimbal2
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