java division: Inconvertible types found: int - java

What's wrong with this code
int numOfPrimes=pf.FindNumPrimes(10000);
Double frequency=((Double)numOfPrimes)/10000d;
Says
inconvertible types found : int
required: java.lang.Double Double
frequency=((Double)numOfPrimes)/10000d;

You're trying to autobox an int to a Double object, which is invalid.
Try:
int numOfPrimes=pf.FindNumPrimes(10000);
Double frequency=((double)numOfPrimes)/10000d;

Don't cast from primitives to wrapper types. Use lower-case double. And you don't need any casting in this case - the compiler does that automatically. The above can be simplified to:
int numOfPrimes = ...;
double frequency = numOfPrimes / 10000d;
You should almost never mix primitives with wrappers. And always prefer primitives (if possible). Use Double.valueOf(..) for conversion if you need to.

Double is not a primitive type (like int, long, byte, etc). It's a class type. You can convert between double and Double using autoboxing but not between int and Double.
You should either declare numOfPrimes as double or do the cast to a double instead of a Double
double numOfPrimes=pf.FindNumPrimes(10000);
Double frequency=((Double)numOfPrimes)/10000d;
or
int numOfPrimes=pf.FindNumPrimes(10000);
Double frequency=((double)numOfPrimes)/10000d;
or without unnecessary casts:
double numOfPrimes = pf.FindNumPrimes(10000);
Double frequency= numOfPrimes /10000d;
or
int numOfPrimes = 10;
Double frequency = numOfPrimes /10000d;

Related

Why can I assign an int literal to double, but not to Double? [duplicate]

Why do I get an error when attempting to initialize a Double to an int, even though it does not throw an exception when using the primitive type, double?
Double a = 1; // error - incompatible types
Double b = 1.0; // OK
double c = 1; // OK
Why is the behavior different between the class Double and the primitive type, double?
When you initialize your Double as:
Double a = 1;
There needs to be done 2 things:
Boxing int to Integer
Widening from Integer to Double
Athough, boxing is fine, but widening from Integer to Double isn't valid. So, it fails to compile.
Note that, Java doesn't support Widening followed by Boxing conversion, as specified in JLS §5.2:
Assignment contexts allow the use of one of the following:
an identity conversion (§5.1.1)
a widening primitive conversion (§5.1.2)
a widening reference conversion (§5.1.5)
a boxing conversion (§5.1.7) optionally followed by a widening
reference conversion
an unboxing conversion (§5.1.8) optionally followed by a widening
primitive conversion.
Your 2nd assignment goes through boxing conversion.
While 3rd assignment goes through widening conversion.
It is because of Java's autoboxing which will convert 1 into an Integer instead of a Double.
Double a = Double.valueOf(1);
should work
The because of auto boxing
Double a = 1; equals to Double a = Integer.valueOf(1);
Double b = 1.0; equals to Double b = Double.valueOf(1);
double c = 1; equals to double c = (double) 1;
What more you can do is to mark your number with D.
Double d = 1D; equals to Double d = Double.valueOf(1);

integer division java returns a double? What is going on?

See screenshot:
What is going on there? i see that we're casting 23F to an int so in choice "II", we wind up doing 23/7 which should give us 3... at least so I thought. But why do we have 3.0? Why do we get a double?
Here is the context:
float x = myFunc(23F);
int myFunc(float x) { ... }
The right hand side of the expression is an int and you have correctly deduced that its value is 3.
When you assign an int value to a float variable, the integer will be converted to a float.
This is an example of a primitive widening conversion. It can happen in various contexts when you are going from a "smaller" primitive numeric type to a "larger" primitive numeric type. The int type is "smaller" than the float type. In general:
byte < short and char < int < long < float < double.
Why do we get a double?
See above. But it is a float not a double.
When you output a number in Java using println, a float and a double will look the same. The println method won't output a float with an F suffix. The F and D suffixes are only used in Java source code.
Because the function that return the result is assigned to z, and the type of z is float.

Why no promotion when adding ints to List<Double>

in Java I created an ArrayList of Double and I invoked the method list.add(1), however, I get an error. If I can assign an int to a double variable like this: double num = 1; due to automatic promotion, then why can't I add a 1 to ArrayList of Double via automatic promotion?
You're not trying to convert int to double; you're trying to convert int to Double, which is a combination of boxing and the implicit conversion from int to double. That doesn't work, even in a simple assignment:
// Error: incompatible types: int cannot be converted to Double
Double num = 1;
It doesn't even work for Long - you need to specify a long literal:
Long num1 = 1; // Invalid
Long num2 = 1L; // Valid
In your case, you just need to use a double literal, e.g.
list.add(1.0);
list.add(1D);

Casting Double object to int

How can I convert from a Double object to an int? The following code works but seems slightly unconventional (with casting twice):
Double d = new Double(4.0);
int i = (int)(double)d;
When I try int i = (int)d, I get an error from Eclipse (Cannot cast from Double to int), which makes sense. Nevertheless, is there a simpler way of converting a Double object to an int?
Double.intValue()
is the provided method that does that conversion.
If you cast double to int you will loss decimal value.. so 4.99 double become 4 int. If still want to convert than try-
int i = d.intValue();
In your case you use
Double.intValue()
as a method to convert the Double Object to Integer.

Why can't I set a Double object equal to an int? - Java

Why do I get an error when attempting to initialize a Double to an int, even though it does not throw an exception when using the primitive type, double?
Double a = 1; // error - incompatible types
Double b = 1.0; // OK
double c = 1; // OK
Why is the behavior different between the class Double and the primitive type, double?
When you initialize your Double as:
Double a = 1;
There needs to be done 2 things:
Boxing int to Integer
Widening from Integer to Double
Athough, boxing is fine, but widening from Integer to Double isn't valid. So, it fails to compile.
Note that, Java doesn't support Widening followed by Boxing conversion, as specified in JLS §5.2:
Assignment contexts allow the use of one of the following:
an identity conversion (§5.1.1)
a widening primitive conversion (§5.1.2)
a widening reference conversion (§5.1.5)
a boxing conversion (§5.1.7) optionally followed by a widening
reference conversion
an unboxing conversion (§5.1.8) optionally followed by a widening
primitive conversion.
Your 2nd assignment goes through boxing conversion.
While 3rd assignment goes through widening conversion.
It is because of Java's autoboxing which will convert 1 into an Integer instead of a Double.
Double a = Double.valueOf(1);
should work
The because of auto boxing
Double a = 1; equals to Double a = Integer.valueOf(1);
Double b = 1.0; equals to Double b = Double.valueOf(1);
double c = 1; equals to double c = (double) 1;
What more you can do is to mark your number with D.
Double d = 1D; equals to Double d = Double.valueOf(1);

Categories

Resources