Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between casting to int and intValue() method in Java

Tags:

java

I have a long value that needed to be converted to an integer. When I use casting, time to time integer value gives a minus value, which is not expected. But when I use the intValue() method in Long, the expected results comes.

I want to know the difference of casting and using intValue() method

  1. Casting Example

    int days = (int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));

  2. intValue Example

    int days = ((Long) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24))).intValue();

Edit: More Elaborate Example to show minus values without overflow as suggested in comments. Before casting the result is 27. When casting, the value becomes -22. But if intValue method is used, the result is 27.

Code

            System.out.println("nextDeliveryDate = " + nextDeliveryDate);
            System.out.println("nextDeliveryDate.getTime() = " + nextDeliveryDate.getTime());
            System.out.println("expectedDeliveryDate = " + expectedDeliveryDate);
            System.out.println("expectedDeliveryDate.getTime() = " + expectedDeliveryDate.getTime());
            System.out.println("nextDeliveryDate.getTime() - expectedDeliveryDate.getTime() = " + (nextDeliveryDate.getTime() - expectedDeliveryDate.getTime()));
            System.out.println("(nextDeliveryDate.getTime() - expectedDeliveryDate.getTime()) / (1000 * 60 * 60 * 24) = " + (nextDeliveryDate.getTime() - expectedDeliveryDate.getTime()) / (1000 * 60 * 60 * 24));
            System.out.println("((int) (nextDeliveryDate.getTime() - expectedDeliveryDate.getTime()) / (1000 * 60 * 60 * 24)) = " + ((int) (nextDeliveryDate.getTime() - expectedDeliveryDate.getTime()) / (1000 * 60 * 60 * 24)));
            System.out.println("((Long) ((nextDeliveryDate.getTime() - expectedDeliveryDate.getTime()) / (1000 * 60 * 60 * 24))).intValue() = " + ((Long) ((nextDeliveryDate.getTime() - expectedDeliveryDate.getTime()) / (1000 * 60 * 60 * 24))).intValue());

Results

Info: nextDeliveryDate = Thu May 14 00:00:00 IST 2015
Info: nextDeliveryDate.getTime() = 1431541800000
Info: expectedDeliveryDate = Fri Apr 17 00:00:00 IST 2015
Info: expectedDeliveryDate.getTime() = 1429209000000
Info: nextDeliveryDate.getTime() - expectedDeliveryDate.getTime() = 2332800000
Info: (nextDeliveryDate.getTime() - expectedDeliveryDate.getTime()) / (1000 * 60 * 60 * 24) = 27
Info: ((int) (nextDeliveryDate.getTime() - expectedDeliveryDate.getTime()) / (1000 * 60 * 60 * 24)) = -22
Info: ((Long) ((nextDeliveryDate.getTime() - expectedDeliveryDate.getTime()) / (1000 * 60 * 60 * 24))).intValue() = 27
like image 949
Buddhika Ariyaratne Avatar asked Apr 14 '15 15:04

Buddhika Ariyaratne


People also ask

What does intValue do in Java?

intValue. Returns the value of the specified number as an int , which may involve rounding or truncation. Returns: the numeric value represented by this object after conversion to type int .

What is the difference between int [] A and int a [] in Java?

Difference between “int[] a” and “int a[]” for Multidimensional Arrays in Java. For Multidimensional Arrays, in Java, there is no difference and any of the mentioned syntaxes can be used for declaration.

What is the difference between integer and int in Java?

In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.

What is difference between int and int?

The major difference between an Integer and an int is that Integer is a wrapper class whereas int is a primitive data type. An int is a data type that stores 32-bit signed two's complement integer whereas an Integer is a class that wraps a primitive type int in an object.


1 Answers

I don't think your results should be different, because here is what the Long.intValue() method does:

public int intValue() {
    return (int)value;
}

Something isn't right with your control flow between those two lines.

You can try this test below:

Calendar now = Calendar.getInstance();
Date fromDate = now.getTime();
now.add(Calendar.YEAR, 10);
Date toDate = now.getTime();

System.out.println("line 0: " + (toDate.getTime() - fromDate.getTime()));
System.out.println("line 1: " + (int)(toDate.getTime() - fromDate.getTime()));
System.out.println("line 2: " + new Long((toDate.getTime() - fromDate.getTime())).intValue());

int days = (int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));

System.out.println("line 3: " + days);

days = ((Long) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24))).intValue();

System.out.println("line 4: " + days);
like image 129
Mecon Avatar answered Sep 25 '22 17:09

Mecon