What is the difference between these two java statement? [duplicate] - java

This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed last year.
int s=0;
s+=Math.pow(2,3);
this line does not show possible lossy conversion whereas
int s=0
s=s+Math.pow(2,3)
the above line shows error.
Any reason for this?

The problem is due to the method signature for Math.pow;. It returns the result as a double. You then attempte to return the double as an int, and that is a lossy conversion.

Related

(int) object syntax Java [duplicate]

This question already has answers here:
What does a 'int' in parenthesis mean when giving a value to an int?
(7 answers)
Closed 2 years ago.
What does the second line in the following code represent?
long longNumber = Integer.MAX_VALUE;
int intNumber = (int) longNumber;
I've created a longNumber and assigned it MAX_VALUE. What does the second line mean?
Thanks in advance.
Writing "(Generic Type) VariableName" mean that you are changing the type of "VariableName"
this syntax is called "CASTING"
In this case you are converting a Long variable to an Int variable.
Casting is not always a secure method becouse if the LONG number is higher than Integer.maxvalue the number will NOT be converted in the correct way (becouse LONG type has more bits than normal INT)

why is the output of System.out.println((5/2.0)*2); is 5.0? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Java integer-double division confusion [duplicate]
(5 answers)
Closed 4 years ago.
I think 5 is an int. But there are no variables. I don't know why the output is a double. I tried in Netbeans already.
Multiplication and division are done left to right (and regardless, you put parentheses around the first operation), and if you divide any primitive umber by a double, you will get a double out of it.
5/2.0 will clearly be evaluated first.
So, even though ‘5’ is an int, if you do 5/2.0, you’ll get a double (namely, 2.5). Now, at this point, your code is already working with a double.
When you multiply 2.5 by 2, you then output 5.0, since 2.5 is a double.

Java Why is char = char^char different from char^= char? [duplicate]

This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 6 years ago.
I was solving a problem in leet code and noticed the following code was not allowed in Java,
char c = 's';
c = c^c;
While the following was
char c = 's';
c^=c;
Is there a particular reason? Thanks you.
This is also true for plus or minus. c^c is evaluated as an int so the right hand side is int and can not be assigned into a char.
In the ^= case, the right hand side is char, and can be applied onto a char.
This is not the most obvious behavior.

java double number division giving strange output [duplicate]

This question already has answers here:
Simple division in Java - is this a bug or a feature?
(5 answers)
Closed 7 years ago.
Found below output strange. Is it really?
double rate = 11/12; // outputs 0.0
double rate = 11.00/12; // outputs 0.916666667;
Why so much difference?
In the 1st case first division is done so an int divided by an int gives a integer i.e, 11/12=0 Then this integer is converted to double .ie, 0.0. In the 2nd case a double(11.00) is divided by a integer(12).The integer 12 is then automatically type casted to double as division should occur with similar types. This division gives a double value which is exact in reality(0.916666667)
See this link https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

calculating the power in Java [duplicate]

This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
I am trying to calculate the power as below but it is giving me 'bad operands type for binary operator '^'. I am guessing that it is a precedence issue but it still doesn't fix with inserting additional brackets
double pw = ((N - (df + 1))^2);
You should use java.lang.Math.pow(x,y)
Example: java.lang.Math.pow(2,3) returns 8
See this
http://www.tutorialspoint.com/java/lang/math_pow.htm
java.lang.Math.pow(double a, double b)
You can use static import for this.

Categories

Resources