Bitwise operation not concatenating with string in print() in Java
up vote
6
down vote
favorite
This code
int a = 6;
System.out.print("The result is " + a*a);
works just fine, but this one
int a = 6;
System.out.print("The result is " + a^a);
produces an exception:
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous tree type: at
pkg1.pkg4.taking.input.TakingInput.main(TakingInput.java:11)
Why so?
The question arose when I was trying to print the results of several bitwise operations in one swoop, like so:
System.out.print(a&b + "n" + a|b + "n" + a^b);
I looked up the description of the print()
method and several topics on bitwise operators and printing to the console on SO including the recommended topics when composing the question, but couldn't find an answer.
java printing bitwise-operators operator-precedence
add a comment |
up vote
6
down vote
favorite
This code
int a = 6;
System.out.print("The result is " + a*a);
works just fine, but this one
int a = 6;
System.out.print("The result is " + a^a);
produces an exception:
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous tree type: at
pkg1.pkg4.taking.input.TakingInput.main(TakingInput.java:11)
Why so?
The question arose when I was trying to print the results of several bitwise operations in one swoop, like so:
System.out.print(a&b + "n" + a|b + "n" + a^b);
I looked up the description of the print()
method and several topics on bitwise operators and printing to the console on SO including the recommended topics when composing the question, but couldn't find an answer.
java printing bitwise-operators operator-precedence
I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forgetRuntimeException
!
– nullpointer
43 mins ago
That's what the error says:uncompilable source code
. Another problem was I was using single quotes instead of double quotes withn
.
– John Allison
39 mins ago
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
This code
int a = 6;
System.out.print("The result is " + a*a);
works just fine, but this one
int a = 6;
System.out.print("The result is " + a^a);
produces an exception:
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous tree type: at
pkg1.pkg4.taking.input.TakingInput.main(TakingInput.java:11)
Why so?
The question arose when I was trying to print the results of several bitwise operations in one swoop, like so:
System.out.print(a&b + "n" + a|b + "n" + a^b);
I looked up the description of the print()
method and several topics on bitwise operators and printing to the console on SO including the recommended topics when composing the question, but couldn't find an answer.
java printing bitwise-operators operator-precedence
This code
int a = 6;
System.out.print("The result is " + a*a);
works just fine, but this one
int a = 6;
System.out.print("The result is " + a^a);
produces an exception:
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous tree type: at
pkg1.pkg4.taking.input.TakingInput.main(TakingInput.java:11)
Why so?
The question arose when I was trying to print the results of several bitwise operations in one swoop, like so:
System.out.print(a&b + "n" + a|b + "n" + a^b);
I looked up the description of the print()
method and several topics on bitwise operators and printing to the console on SO including the recommended topics when composing the question, but couldn't find an answer.
java printing bitwise-operators operator-precedence
java printing bitwise-operators operator-precedence
edited 34 mins ago
asked 55 mins ago
John Allison
160112
160112
I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forgetRuntimeException
!
– nullpointer
43 mins ago
That's what the error says:uncompilable source code
. Another problem was I was using single quotes instead of double quotes withn
.
– John Allison
39 mins ago
add a comment |
I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forgetRuntimeException
!
– nullpointer
43 mins ago
That's what the error says:uncompilable source code
. Another problem was I was using single quotes instead of double quotes withn
.
– John Allison
39 mins ago
I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget
RuntimeException
!– nullpointer
43 mins ago
I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget
RuntimeException
!– nullpointer
43 mins ago
That's what the error says:
uncompilable source code
. Another problem was I was using single quotes instead of double quotes with n
.– John Allison
39 mins ago
That's what the error says:
uncompilable source code
. Another problem was I was using single quotes instead of double quotes with n
.– John Allison
39 mins ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
10
down vote
accepted
This is because the +
has higher precedence than the ^
so it compiles to:
("The result is " + a) ^ a
Which obviously will not work. Put parenthesis around it:
System.out.print("The result is " + (a^a));
1
Ouch... that was so basic... Thanks. :)
– John Allison
51 mins ago
@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
46 mins ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
This is because the +
has higher precedence than the ^
so it compiles to:
("The result is " + a) ^ a
Which obviously will not work. Put parenthesis around it:
System.out.print("The result is " + (a^a));
1
Ouch... that was so basic... Thanks. :)
– John Allison
51 mins ago
@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
46 mins ago
add a comment |
up vote
10
down vote
accepted
This is because the +
has higher precedence than the ^
so it compiles to:
("The result is " + a) ^ a
Which obviously will not work. Put parenthesis around it:
System.out.print("The result is " + (a^a));
1
Ouch... that was so basic... Thanks. :)
– John Allison
51 mins ago
@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
46 mins ago
add a comment |
up vote
10
down vote
accepted
up vote
10
down vote
accepted
This is because the +
has higher precedence than the ^
so it compiles to:
("The result is " + a) ^ a
Which obviously will not work. Put parenthesis around it:
System.out.print("The result is " + (a^a));
This is because the +
has higher precedence than the ^
so it compiles to:
("The result is " + a) ^ a
Which obviously will not work. Put parenthesis around it:
System.out.print("The result is " + (a^a));
edited 51 mins ago
answered 52 mins ago
GBlodgett
7,81041431
7,81041431
1
Ouch... that was so basic... Thanks. :)
– John Allison
51 mins ago
@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
46 mins ago
add a comment |
1
Ouch... that was so basic... Thanks. :)
– John Allison
51 mins ago
@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
46 mins ago
1
1
Ouch... that was so basic... Thanks. :)
– John Allison
51 mins ago
Ouch... that was so basic... Thanks. :)
– John Allison
51 mins ago
@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
46 mins ago
@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
46 mins ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53606291%2fbitwise-operation-not-concatenating-with-string-in-print-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget
RuntimeException
!– nullpointer
43 mins ago
That's what the error says:
uncompilable source code
. Another problem was I was using single quotes instead of double quotes withn
.– John Allison
39 mins ago