CodeMonkey
CodeMonkey

Reputation: 12424

casting between short,int,long,double,float in Java

As i understand, when you cast between 2 of these types with an arithmetic operation in Java, for example double + int, the result will be as the bigger type (meaning in this example, the result will be double). What happens when you make an arithmetic operation on 2 types with the same size? what will int + float and long + double give? since int and float are 4 bytes each, and long and double are 8 bytes.

Upvotes: 0

Views: 4454

Answers (6)

Peter Lawrey
Peter Lawrey

Reputation: 533492

A gotcha of this "promotion" is that a long + float will "widen" to using a float.

e.g.

System.out.println(1111111111111111111L + 0.0f);
System.out.println(1111111111111111111L + 0.0);

prints

1.11111113E18
1.11111111111111117E18

When dealing with long and float, you may not get a wider type and can lose more precision than you might expect.

Upvotes: 0

rlinden
rlinden

Reputation: 2041

There are two an interesting FAQ on type conversion that can be found in: http://www.programmersheaven.com/2/FAQ-JAVA-Type-Conversion-Casting

http://myhowto.org/java/60-understanding-the-primitive-numeric-type-conversions-in-java/

Answering your questions on two types of the same size, the return value is of the type with the biggest precision.

Try the following code:

public static void main(String[] args) {
    int i=1;
    float f=2.5f;
    long l=10;
    double d=3.74;
    System.out.println(i+f);
    System.out.println(f+i);
    System.out.println(l+d);
    System.out.println(d+l);
}

You will see that the results are 3.5 and 13.74, which are a float and a double, respectively (tested in Netbeans 6.9 and java 1.6).

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272487

This is all specified by the binary numeric promotion rules in the JLS. From http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2:

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:

  1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).

  2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

    • If either operand is of type double, the other is converted to double.

    • Otherwise, if either operand is of type float, the other is converted to float.

    • Otherwise, if either operand is of type long, the other is converted to long.

    • Otherwise, both operands are converted to type int.

After the type conversion, if any, value set conversion (§5.1.13) is applied to each operand.

Binary numeric promotion is performed on the operands of certain operators:

  • The multiplicative operators *, / and % (§15.17)

  • The addition and subtraction operators for numeric types + and - (§15.18.2)

  • The numerical comparison operators <, <=, >, and >= (§15.20.1)

  • The numerical equality operators == and != (§15.21.1)

  • The integer bitwise operators &, ^, and | (§15.22.1)

  • In certain cases, the conditional operator ? : (§15.25)

("value set conversion" is about mapping between floating-point representations.)

Upvotes: 10

assylias
assylias

Reputation: 328598

The behavior of the additive operators + and - is defined in by 15.18.2. Additive Operators (+ and -) for Numeric Types of the JLS. It states that it first performs a binary numeric promotion:

Binary numeric promotion is performed on the operand.

This in turns is defined by 5.6.2. Binary Numeric Promotion. In substance, for primitives:

  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.

Upvotes: 0

Jorge
Jorge

Reputation: 18237

Still going to return the "bigger" type that can hold the entirely value. In your specific question

if you make a + between a int and a float the return will be a float and long + double returns a double,

Upvotes: 0

Andrew Logvinov
Andrew Logvinov

Reputation: 21831

int + float will give you float (note that you'll have to cast result to float because double is used by default). long + double will give you double.

Upvotes: 2

Related Questions