what is '^=' operator in java [duplicate] - java

This question already has answers here:
Short hand assignment operator, +=, True Meaning?
(2 answers)
Difference between variable += value and variable = variable+value;
(4 answers)
Closed 3 years ago.
My code is as following:
char ch = t.charAt(t.length() - 1);
// result of XOR of two char is Integer.
for(int i = 0; i < s.length(); i++){
ch = ch^s.charAt(i);
ch = ch^t.charAt(i);
}
return ch;
it throws error
Line 6: error: incompatible types: possible lossy conversion from int to char
ch = ch^s.charAt(i);
Line 7: error: incompatible types: possible lossy conversion from int to char
ch = ch^t.charAt(i);
2 errors
However, When I change
ch = ch^s.charAt(i);
ch = ch^t.charAt(i);
to
ch ^= s.charAt(i);
ch ^= t.charAt(i);
Then, my code can work.
Are '^=' and '* = ^' different?? Why I search this question about '^=', it says they are same??
What is the '^=' operator?

From the functionality, they are the same: both perform an exclusive OR.
But the data types are different:
if I do x = x ^ y, the data type of x ^ y is always int or greater. When the result is assigned to something smaller, you have to cast.
if I do x ^= y, the data type doesn't grow as the assignment "knows its type".
See the language specification at https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2 for more details.

You need to declare ch to be an int, or store the result of the bitwise xor ^= in an int field. Right now it's trying to store it in a char which is where the error is coming from.

Related

(char) (i + 'a') JAVA: what's happening here? [duplicate]

This question already has answers here:
Adding and subtracting chars, why does this work? [duplicate]
(4 answers)
Parentheses around data type?
(5 answers)
What does a 'int' in parenthesis mean when giving a value to an int?
(7 answers)
What is the purpose and meaning of (char)i or (int)i in java?
(3 answers)
Closed 2 years ago.
I encountered a similar piece of code:
public class print {
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
System.out.print((char) (i + 'a'));
}
}
}
If I run it, I get "abcdef".
My question regards this expression: (char) (i + 'a').
I kind of intuitively get what's going on, but I want a rigorous step-by-step explanation of how the computer translates it. As indicated in some answers, the char is simply a number displayed as a character. Fine, but what does this syntax with parentheses actually do? Is it a conversion? Can I use it for other types as well?
The ASCII value of a is 97.
When i = 0, i + 'a' = 0 + 97 => When cast into char, it will be a
When i = 1, i + 'a' = 1 + 97 => When cast into char, it will be b
When i = 2, i + 'a' = 2 + 97 => When cast into char, it will be c
...and so on
Java char is a 16-bit integral type. 'a' is the same as 97, which you can see with System.out.println((int) 'a'); - it follows that 98 is 'b' and so on across the entire ASCII table.
You can use char as an integer value and vise versa
Below code may help:
char aChar = 'a'; // a char
int aCharAscii = aChar; // 97
char bChar = 'a' + 1; // b char
int bCharAscii = aChar + 1; // 98

Character in Java don't update with post increment [duplicate]

This question already has answers here:
Is there a difference between x++ and ++x in java?
(18 answers)
What is x after "x = x++"?
(18 answers)
Closed 4 years ago.
I want to get next character and I am writing the code like this
char c = 'A';
c = c++;
System.out.println(c);
The printed character is A. But if I use preincrement operator with 'c' then I get next character (B). Here is the code with preincrement operator.
char c = 'A';
c = ++c;
System.out.println(c);
Can someone explain the difference?
The increment operator doesn't make sense if you assign that result back to the variable. Doing
c = c++;
takes the return value of c++, which is 'A', and assigns that to c. Instead, simply do
c++; // or ++c
In your case, you probably want to do
System.out.println(++c); // prints 'B', and |c| is now 'B'
c = c++; means, first the current value will be used and then it will be incremented. Therefore it's first printing the current value
c = c++;
System.out.println(c);
translates to:
c = c; // since the actual value is returned first
System.out.println(c);
and hence, the value A get printed.
Whereas
c = ++c;
System.out.println(c);
translates to
c = c+1;
System.out.println(c);

In java if "char c = 'a' " why does "c = c + 1" not compile?

I tried to compile the following code:
public static void main(String[] args){
for (char c = 'a'; c <='z'; c = c + 1) {
System.out.println(c);
}
}
When I try to compile, it throws:
Error:(5, 41) java: incompatible types: possible lossy conversion from
int to char
The thing is, it does work if I write c = (char)(c + 1), c += 1 or c++.
I checked and the compiler throws a similar error when I try char c = Character.MAX_VALUE + 1; but I see no way that the value of 'c' can pass 'char' type maximum in the original function.
c + 1 is an int, as the operands undergo binary numeric promotion:
c is a char
1 is an int
so c has to be widened to int to make it compatible for addition; and the result of the expression is of type int.
As for the things that "work":
c = (char)(c + 1) is explicitly casting the expression to char, so its value is compatible with the variable's type;
c += 1 is equivalent to c = (char) ((c) + (1)), so it's basically the same as the previous one.
c++ is of type char, so no cast is required.
First you are declaring c as char than you are using it as an int

charvariable=(char)(charvariable+3) what does this syntax mean?

I have been looking around on the internet at caesar ciphers and while I understand the loop I don't understand why this line of code is able to shift a char to another char? I don't understand this line here:
letter = (char)(letter - 26);
When I take (char) out it doesn't work and I have never seen it with the type being in parentheses followed by an operation.
Hopefully this is an easy question and thanks for the help.
for (int i = 0; i < buffer.Length; i++)
{
// Letter.
char letter = buffer[i];
// Add shift to all.
letter = (char)(letter + shift);
// Subtract 26 on overflow.
// Add 26 on underflow.
if (letter > 'z')
{
//The following line is the line I don't understand. Why char in parentheses then another parentheses?
letter = (char)(letter - 26);
}
else if (letter < 'a')
{
letter = (char)(letter + 26);
}
// Store.
buffer[i] = letter;
}
(char) is a cast. That means that it takes a value which is of one type, and converts it to a value of another type. Thus, if x is an int, (double)x yields a double whose value is the same value as the integer value.
The reason (char) is necessary in this expression is that Java does all its integer arithmetic on values of type int or long. So even though letter is a char, in the expression letter + 26, letter will be automatically converted to an int, and then 26 is added to the integer. (char) converts it back to a char type (which is an integer value from 0 to 65535). Java will not automatically convert a larger integer type (int, whose values are from -2147483648 to 2147483647) to a shorter integer type (char), therefore it's necessary to use a cast.
However, Java does allow this:
letter += 26;
which has the same effect, and does not require a cast.
There are 26 letters in the english alphabet, and char is an integral type
char ch = 'Z' - 25;
System.out.println(ch); // <-- A
JLS-4.2.1 - Integral Types and Values says (in part),
For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

Adding int to short [duplicate]

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.

Categories

Resources