This question already has answers here:
Weird Integer boxing in Java
(12 answers)
Closed 8 years ago.
I am confuse about autoboxing unboxing in java. Please see my following two progarm.
Integer x = 400;
Integer y = x;
x++; x--;
System.out.println((x==y));
The output is false.
I known why the output is false. Because of autoboxing x.
Integer x = 100;
Integer y = x;
x++; x--;
System.out.println((x==y));
The output is true.
But the program is same as the upper. Why the output is true?
Please explain me detail.
Thank you very much.
This is because Integers -128 to 127 are cached, so in second example x and y refer to the same Integer instance.
Integer x = 100; // x refers to cached 100
x++;
is equivalent to
int var = x.intValue();
var++;
x = Integer.valueOf(var); // returns cached 100
See Integer.valueOf(int) API.
Related
This question already has answers here:
Meaning of *= in Java
(4 answers)
Closed 4 years ago.
Hello everyone I have a very simple question that I just don't understand. I've tried googling it but haven't found a clear answer.
What is x after the following statements?
int x = 2;
int y = 1;
x *= y + 1;
I know that the answer is 4 but I don't understand why it is 4. Just need some clarity on what x* means exactly. Thanks!
I think this line is the one why you ask
x *= y + 1;
This is a shorthand for
x = x * (y + 1);
This works also with other operators like - and +, when the first variable is the same as the variable on the left side (which will be assigned).
Of course x is 4, if you don't understand the last statement, you can read it like this
x = x * y + 2
The x*= symbol means x=x* the result of whatever you put after the equals symbol.
x*= y+1 will turn to x = x * (y+1). The expresion you put after equals is evaluated first and then multiplied with x. The result will be cast to the type of the assignment variable (x).
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 6 years ago.
The question require me to write a Java program to show the results of the following cast operator expressions:
(double) (23 / 14) + 7.65
My Code:
public class op {
public static void main(String [] args) {
int num = 23/14;
double r1 = (double) num;
double result = r1 + 7.65;
System.out.println("Results: "+ result);
}
}
I don't think I have done correctly, what are the problems of my code?
By the way, can someone tell me what are the differences between long, double, int, float? How do we know when to use these primitive data types? I read an explanation here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
but is there any 'human-version' of the explanation?
Thank you for your help.
The problem is due to the used types.
Since you divide two integers (23 and 14), the result is considered and int as well. Therefor, 23/14 = 1.642857142857143, which is truncated to fit in an int result, more specifically, 1.
result is the sum of 1 (int) and 7.65 (double). Since one of them is a double, to other is converted to the "upper" type as well (double) and the operation becomes 1.0+7.65 = 8.65.
The result is correct, because you asked the result of (double) (23 / 14) + 7.65 which means the result of casting the result of the operations in brackets to double summed with 7.65. Which is 8.65 as previously explained.
If you want to use a division using doubles, consider:
double r1 = 1.0 * 23/14;
Lets see step-by-step:
int num = 23/14; // int division of 23/14 results in 1
So, here num = 1
When you cast num to double value of r1 is setted to 1.0.
double result = r1 + 7.65; //1.0 + 7.65 = 8.65
ok, int is short term for INTEGER which are natural numbers that we use normally but with no decimal places and if your number has some value in between roughly -2 billion to +2 billion. if your range exceeds that and you still want an integer then go for long data type.
floats are for decimal values like 3.147 with a range of +10*38 to -10*38 or so, but if your range exceeds this(practically this happens rarely) go for double.
coming to the code you put here , if you divide a int by another int (like 23/14) you get only get the integer part of the answer(only '1' in 23/14=1.642...) , next when you cast it to double you get 1.0 and next you are going to add that to 7.65 which will make the ultimate answer as 8.65 hope this answers your Q....
You could change this int num = 23/14
to double num = ((double) 23)/14
or double num = (23 * 1.0)/14
This question already has answers here:
Promotion in Java?
(5 answers)
short plus short is an int [duplicate]
(2 answers)
Closed 9 years ago.
I have code like this
short a = 1;
short b = 2 ;
short c = a + b; // dosen't compile
What is the reason for compilation failure? x + x always produces Integer or bigger Number, but why?
None of the binary operators will produce an Integer. However, it will use an int instead of shorter types, byte, short and char If the compiler can inline the value it can cast the value for your. e.g.
final short a = 1;
final short b = 2;
short c = a + b; // does compile, because of constant inlining.
The only operator which produces an Integer is a cast.
Integer i = (Integer) 1;
BTW: On oddity is that Java defines the 32-bit float as being "wider" than the 64-bit long value. This has the downside that float has much less precision. Consider this.
long l = 7777777777777777777L;
l += 0.0f;
System.out.println(l);
prints
7777777579364188160
Even though 0.0F was added to l it was implicitly cast to float (as float is wider) and then cast back (as an operator assignment was used) resulting in a error of ~20 billion.
This question already has answers here:
why same code in two technology behaving different [duplicate]
(3 answers)
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
my code is :
code(){
int x=7;
x=x++;
output x; //prints 8 in C, prints 7 in Java
}
Guys the above code: prints 8 in C, and 7 in Java !!
Why is this so? please explain.
That will print 7 in Java. x=x++; is equivalent to :
int temp = x;
x = x + 1;
x = temp;
The result would have been different if you would have used prefix operator , ++x .
See for yourself over here: java code; C code.
Read Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…) to comprehend the output in C.
In Java, x=x++, is evaluated as:
int temp = x;
x = x + 1;
x = temp;
So, basically there is no change is x after that expression.
In C however, that expression is an Undefined Behaviour. Also see Sequence Points Wiki
This code cause undefined behaviour in C so the result may be any, 7, 8, 15 or Page fault. Why this code give 7, is compiler matter.
In the Java background, something like the following occurs (for the i = i++ statement):
int temp = i; // store current value of i
i = i + 1; // increase i because of i++
i = temp; // assign to i
x=x++;
This gives arbitrary results in C, mainly depending on compiler. Read about sequential points in C. You may refer to C Programming by Dennis ritchie.
it is because of operator precedence. = has more precedence in C than Java.
This question already has answers here:
Varying behavior for possible loss of precision
(1 answer)
Why does Java perform implicit type conversion from double to integer when using the "plus equals" operator? [duplicate]
(2 answers)
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 9 years ago.
I have run into the following surprising line:
int x = 7;
x += 0.5;
is apparently legal syntax! After the addition, x is still 7, so the double is being cast to an int and rounded down to 0, but this is done without any explicit cast in the code. Is anyone else surprised by this? What's the rationale here?
edit to clarify my question: Can anyone give a good reason for this decision? It strikes me as a terrible decision to require explicit casting everywhere else, but have this one spot in the language where you silently throw away data. Am I missing something?
x += 0.5;
is equivalent to:
x = (int) (x + 0.5)
In general:
x += y is equivalent to x = (type of x) (x + y)
See 15.26.2. Compound Assignment Operators
x += 0.5; is the same as x = (int) (x + 0.5);.
This is because compound assignment operators puts an implicit cast (automatic cast):
So
x+=0.5 => x =(int)(x + 0.5) => x = (int)(7.5) => x = 7