# Thread: Confusion in implicit conversion

1. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## 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. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
12

## 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

3. ## 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

4. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## Re: Confusion in implicit conversion

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

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. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## Re: Confusion in implicit conversion

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

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. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
12

## 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

#### Posting Permissions

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