Java plus equals or equals plus operator? [duplicate] - java

This question already has answers here:
What is the difference between a += b and a =+ b , also a++ and ++a?
(9 answers)
Closed 7 years ago.
Is += the same as =+?
I can't find any reason for why the plus sign is reversible.
For what reasons would I need to use one of the other? Where can i find docs on this i tried searching but didnt see the use of both.

It's not the same.
x+=5 is equivalent to x=x+5.
x=+5 (or x=(+5)) is equivalent to x=5.

Related

Purpose of iconst_x [duplicate]

This question already has answers here:
Different behaviour of java bytecode
(6 answers)
Closed 7 years ago.
Why do we have the iconst_* instructions?
Why would I ever want to use these instead of bipush?
I found this StackOverflow question when searching but it does not properly answer my question.
Because a bipush instruction takes two bytes in the bytecode, and an iconst_* instruction takes one byte.

Exponent Math in Java [duplicate]

This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
Could anyone explain to me why
System.out.println(100*(1-10^(-10/10)));
results in the number "800" being printed out? The correct answer is 90 when you use a calculator. How would I go about doing this calculation in Java?
Thanks!
The ^ operator does not do what you think it does. It is bitwise-xor
You need to look into the Math.pow() method.

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.

How to get the name of the containing function [duplicate]

This question already has answers here:
Getting the name of the currently executing method
(23 answers)
Closed 8 years ago.
In C++, you can use __FUNCTION_NAME__ to get the name of the function that contains __FUNCTION_NAME__.
Is there an equivalent in Java? It could, in Java, be possible to do something with this and reflection. Is there something simpler though?
Thread.currentThread().getStackTrace()
will usually contain the method you’re calling it from but there are pitfalls
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StackTraceElement.html
Thread.currentThread().getStackTrace()[ste.length - 1 - depth].getMethodName();
depth = 0 (zero) will give current method
also
System.out.println((new Throwable()).getStackTrace()[0].toString());
Sample output:
com.junk.Junk3.main(Junk3.java:12)

Can you implement a method to act as a +... but for objects instead of primitives? [duplicate]

This question already has answers here:
Operator overloading in Java
(10 answers)
Why doesn't Java need Operator Overloading? [closed]
(10 answers)
Closed 9 years ago.
I think that there is something like this in C/C++, but what about in java? Can I add a method that will add two of them together, but instead of doing say obj1.add(obj2) you can do obj1 + obj2? Can you do the same for -, *, /, and == & other comparatives? Can you create a primitive, or would that take modifying the JVM?
That's called operator overloading. And no Java does not support it. You can find more information here.
No you cannot. There is no operator overloading in Java. The only thing that comes close to an overloaded operator in Java is + which can concatenate strings and perform standard addition.

Categories

Resources