This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Weird java behavior with casts to primitive types
Why following prints 1?
int i = (char) - (int) + (long) - 1;
System.out.println(i);
Why above lines of code prints 1? How come the value of i become 1?
Look at it this way, with each expression getting its own variable (well, aside from the -1).
long a = (long) -1; // a = -1
int b = (int) a; // b = -1
int c = -b; // c = 1
int d = (char) c; // d = 1
int i = (int) d; // i = 1 (implicit conversion)
If I am not mistaken,
int i = (char) - (int) + (long) - 1;
is actually equivalent to
int i = (char)(-(int)(+((long)(- 1))));
since -1 and 1 fit nicely into all data types used in the expression, we can drop the casts to get
int i = -(+(- 1));
Related
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I want to solve this math question in processing:
S=1+1/2-1/3+1/4...+1/99-1/100.
Here is my code (don't know why it doesn't work. I suppose it will print one number in the console, but it comes out a series of natural numbers):
float N = 0;
float T = 0;
int i = 1;
void draw() {
for (i = 1; i < 100; i += 2) {
N = N + 1 / i;
T = T + 1 / (i + 1);
}
println(N - T);
}
First off, draw() runs every frame. Use setup() instead, it runs once.
Second, dividing ints results in an int. Go ahead, do println(1/i) and see the magic.
Third, always google around for some time before going to SO. You'll learn more by finding it out yourself. And this was very solvable with google and a little effort ;)
working code:
float N = 0;
float T = 0;
int i = 1;
void setup() {
for (i = 1; i < 100; i += 2) {
N = N + 1.0 / i;
T = T + 1.0 / (i + 1);
}
println(N - T);
}
1 / i is a division expression between two integers, the result of which will be an int obtained by truncating the fraction part of the result. Since you are calculating float you can resolve it by using a 1f float literal
for (int i = 1; i < 100; i += 2) {
N = N + 1f / i;
T = T + 1f / (i + 1);
}
However your code currently counts the following:
Consider i = 1 for which you get T = 0.5 which you later subtract from N while you should be adding it as per your problem statement. One way to sole it is to write the code as:
float sum = 1;
for (int i = 2; i <= 100; i++) {
float term = 1f / i;
if (i % 2 != 0) {
term *= -1;
}
sum += term;
}
System.out.println(sum);
I know that in Python you can do floor division like this:
5 // 2 #2
The // is used for something totally different in Java. Is there any way to do floor division in Java?
You can do
double val = 5 / 2;
int answer = Math.floor(val);
OR
int answer = Math.floorDiv(5, 2);
If you were to call System.out.println(answer); the output would be
2
You can easily use Math.floorDiv() method.
For example:
int a = 15, b = 2;
System.out.println(Math.floorDiv(a, b));
// Expected output: 7
If you're using integers in the division and you cast the solution to another integer (by storing the result in another integer variable), the division is already a floor division:
int a = 5;
int b = 2;
int c = 5/2;
System.out.println(c);
>> 2
As khelwood stated, it also works with negative values but the rounding is done towards 0. For example, -1.7 would be rounded to -1.
use floorDiv()
int x = 10;
int y = 3;
System.out.println(Math.floorDiv(x,y));
I was asked to explain why the following snippet prints 1. I have stared at it for a while but not yet able to say why it prints out 1 or even why it does compile. Can someone kindly tell me why?
int i = (byte) + (char) - (int) + (long) - 1;
System.out.println(i);
This is just a sequence of casts and number/char conversions:
int i = (byte) +(char) -(int) +(long) -1;
can be made verbose as:
int a = -1;
long b = (long) a;
int c = (int) -b; //makes it positive
char d = (char) c;
byte e = (byte) d;
int f = e;
System.out.println(f);
You have here 4 casting operators and 4 +/- operators.
Since +/- can't be applied to casting operators, the only way to evaluate this expression is by treat the - and + as unary operators :
int i = (byte) (+ ((char) (- ((int) (+ ((long) (- 1)))))));
-1 int
-1 long
-1 long
-1 int
--1 == +1 int
1 char
1 char
1 byte
1 int
This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 7 years ago.
Could explain me why there is a difference when I want to sum some numbers?
int a = 4;
int b = 6;
int e = 10;
int wynik1 = a += b += e; //so it is 20.
System.out.println(wynik1);
int wynik2 = a + b + e;
System.out.println(wynik2); // so it is 46....
Should I just use always += instead of +?
I'm confused because when I was learning, for example, loops, I was using for (int p = 20; p<40; p=p+ 5) and it was working fine.
Why is it 46?
In most cases (exception) a += b as equivalent of a = a + b
So
int wynik1 = a += b += e; //so it is 20.
is same as
int wynik1 = (a = a + (b = b + e)); // so it is 20.
which means that
first b = b + e will be executed making b 6 + 10 = 16
then since b is 16 a will be assigned with result of 4 + 16 = 20
which finally will be assigned to wynik1.
So after that line (a = a + (b = b + e)) (or in your case a += b += e;) our variables will hold these values:
a = 20
b = 16
e = 10 (e didn't change since there was no e=.. in our code)
This should explain why
int wynik2 = a + b + e; //20 + 16 + 10
is 46.
+= is way different from +.
a+b means add a and b and do something with the result.
a += b means add a and b and assign the result back to a.
In your example I don't think you want to have that kind of side effect in the first expression as you probably want to use the original values in the next expression.
The += operator is distinct from the + operator in that it also assigns the result back to the variable.
a = a + 5;
a += 5;
Are equivalent.
In your example,
int wynik1 = a += b += e;
Not only is wynik1 equal to 20, but a is now also equal to 20 and b is now 16. This is why your second line returns 46:
a + b + e
= 20 + 16 + 10
= 46
The operator += is a shortcut.
Doing: a += 1; is equivalent to a = a + 1;
So, when you do: int wynik1 = a += b += e;
is reality you are doing:
int wynik1 = (a = a + (b = b + e));
+ just adds numeric values
+= adds the value on the right to the variable on the left part
Examples:
int a=3;
int b=4;
int c = a+b; // result c==7
c += 1; // result c==8
You can also use any other operator, like - * /
int d = 4;
d -= 3; // result d=1
int e=13;
int f *=3; // result f=39
As for the line:
int wynik1 = a += b += e;
b will be added with 10,
a with b, which is now 16, so the result is 20
The difference with a simple + is that the value of b is changed as well.
You could rewrite this to:
b = b + e; //results in b equals16
a = a + b; //results in a equals 20
First, note that
a += b;
is equivalent to
a = a + b;
The order of operations for assignment in Java is right to left. So,
a += b += e;
is
b = b + e; //16
a = a + b; //20
wynik1 = a; //20
Hence
wynik2 = a + b + e; //46
This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 6 years ago.
A colleague of mine asked this question to me and I am kind of confused.
int i = 123456;
short x = 12;
The statement
x += i;
Compiles fine however
x = x + i;
doesn't
What is Java doing here?
int i = 123456;
short x = 12;
x += i;
is actually
int i = 123456;
short x = 12;
x = (short)(x + i);
Whereas x = x + i is simply x = x + i. It does not automatically cast as a short and hence causes the error (x + i is of type int).
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
- JLS ยง15.26.2
The + operator of integral types (int, short, char and byte) always returns an int as result.
You can see that with this code:
//char x = 0;
//short x = 0;
//byte x = 0;
int x = 0;
x = x + x;
It won't compile unless x is an int.
Numbers are treated as int unless you specifically cast them otherwise. So in the second statement when you use a literal number instead of a variable, it doesn't automatically cast it to the appropriate type.
x = x + (short)1;
...should work.