Adding char to an int without getting an incompatibility error [duplicate] - java

This question already has answers here:
final characters in Java [duplicate]
(2 answers)
Closed 7 years ago.
I'm just learning that you can add an int to a char. I've tried out the following expecting it to compile, but I'm getting an incompatible types error:
char a = 'e' + (number / 10)
I can't figure out why, if
char c = '1'
I've seen that similar questions suggest the use of 'final' to resolve, but I've modified it to apply that and it still gets the same error.....
works then the above doesn't....
Any ideas?

This occurs because you need to cast the chars into ints before you can actually perform calculations with them: int answer = (int) 'e' + (number / 10)
And then cast the answer back to a char:
answer = (char) answer

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)

Can anyone explain how do we get this output ? (bitwise not) [duplicate]

This question already has answers here:
Java: What does ~ mean
(5 answers)
Closed 5 years ago.
int x=10;
System.out.println(~x);
//this will print -11
//how to do the calculation manually using complement arithmetic
This is a Negation operator it will consider ~x= -(10+1), so you will get -11 as the output. Refer some C books you can get more explanation on this
Perhaps this helps you: You can print out the bits of the integer as following. There you can see that a int is represented as a 32-bit value. Explanations about bitwise not operator can be found on the web i guess;
int x = 10;
System.out.println(Integer.toBinaryString(x)); //00000000000000000000000000001010
System.out.println(Integer.toBinaryString(~x)); //11111111111111111111111111110101
System.out.println(~x); //-11

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.

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.

Java compile question [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why int i = 2147483647 + 1; is ok, but byte b = 127 + 1; is not compilable?
Im a beginner with java so please appreciate this beginner question. But why is my compiler not fine with byte b = 127 + 1; but compiles fine with int i = 2147483647 + 1;
Your compiler complains because it sees two ints being added together (int 127) and (int 1) and then it worries that some precision will be lost as it attempts to store the result (int 128) into a byte.
The numbers you selected tend to hint that you think it is related to overflow. It is not, as even if it is important to keep overflow in mind when programming, the compiler never complains about overflow issues.
because when the compiler sees 127 it treats it as an int, not a byte. You need a cast to fit the result back into a byte.

Categories

Resources