Bitwise operation not concatenating with string in print() in Java











up vote
7
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.










share|improve this question
























  • I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget RuntimeException!
    – nullpointer
    1 hour 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
    1 hour ago

















up vote
7
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.










share|improve this question
























  • I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget RuntimeException!
    – nullpointer
    1 hour 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
    1 hour ago















up vote
7
down vote

favorite









up vote
7
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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 2 hours ago









John Allison

165112




165112












  • I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget RuntimeException!
    – nullpointer
    1 hour 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
    1 hour ago




















  • I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget RuntimeException!
    – nullpointer
    1 hour 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
    1 hour ago


















I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget RuntimeException!
– nullpointer
1 hour ago






I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget RuntimeException!
– nullpointer
1 hour 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
1 hour 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
1 hour ago














1 Answer
1






active

oldest

votes

















up vote
13
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));





share|improve this answer



















  • 1




    Ouch... that was so basic... Thanks. :)
    – John Allison
    2 hours ago










  • @JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
    – Jai
    2 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
13
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));





share|improve this answer



















  • 1




    Ouch... that was so basic... Thanks. :)
    – John Allison
    2 hours ago










  • @JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
    – Jai
    2 hours ago















up vote
13
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));





share|improve this answer



















  • 1




    Ouch... that was so basic... Thanks. :)
    – John Allison
    2 hours ago










  • @JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
    – Jai
    2 hours ago













up vote
13
down vote



accepted







up vote
13
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));





share|improve this answer














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));






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 2 hours ago









GBlodgett

7,85541531




7,85541531








  • 1




    Ouch... that was so basic... Thanks. :)
    – John Allison
    2 hours ago










  • @JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
    – Jai
    2 hours ago














  • 1




    Ouch... that was so basic... Thanks. :)
    – John Allison
    2 hours ago










  • @JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
    – Jai
    2 hours ago








1




1




Ouch... that was so basic... Thanks. :)
– John Allison
2 hours ago




Ouch... that was so basic... Thanks. :)
– John Allison
2 hours ago












@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
2 hours ago




@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
2 hours ago


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Accessing regular linux commands in Huawei's Dopra Linux

Can't connect RFCOMM socket: Host is down

Kernel panic - not syncing: Fatal Exception in Interrupt