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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
17
down vote

favorite
1












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
    Dec 4 at 5:46











  • 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
    Dec 4 at 5:50















up vote
17
down vote

favorite
1












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
    Dec 4 at 5:46











  • 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
    Dec 4 at 5:50













up vote
17
down vote

favorite
1









up vote
17
down vote

favorite
1






1





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 Dec 4 at 5:54

























asked Dec 4 at 5:34









John Allison

350114




350114











  • I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget RuntimeException!
    – nullpointer
    Dec 4 at 5:46











  • 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
    Dec 4 at 5:50

















  • I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget RuntimeException!
    – nullpointer
    Dec 4 at 5:46











  • 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
    Dec 4 at 5:50
















I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget RuntimeException!
– nullpointer
Dec 4 at 5:46





I wonder what Java version are you compiling this with, it doesn't compile with java-11 for sure, forget RuntimeException!
– nullpointer
Dec 4 at 5:46













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
Dec 4 at 5:50





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
Dec 4 at 5:50













1 Answer
1






active

oldest

votes

















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


Or as Holger mentioned, you can eliminate this problem by using printf:



System.out.printf("The result is %d", a^a);





share|improve this answer






















  • @JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
    – Jai
    Dec 4 at 5:43






  • 4




    @Jai The OP surely used an IDE as without, he wouldn’t even try to run a program which didn’t compile. Besides that, here, the original Java designers are to blame. Using + for string concatenation was a big mistake. It not only has an unfortunate precedence, the usual expectation of commutativity for an addition does not hold when at least one of the operands of + is a string. But even Java itself has an alternative, System.out.printf("The result is %d", a^a);
    – Holger
    Dec 4 at 7:44











  • @Holger Hmm, my Eclipse is showing a compile time error when I do this. I guess it's the same whichever way - you can always miss the error messages and run the application...
    – Jai
    Dec 4 at 7:55










  • @Jai the standard javac command line tool does not generate .class files when there are compile errors, hence, it is impossible to run the application afterwards. It is a special (dubious imho) option of IDEs to still generate classes on compiler errors, which will throw an exception at runtime then. You can turn this feature off and never miss an error, but the default is on for most IDEs.
    – Holger
    Dec 4 at 8:06










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


Or as Holger mentioned, you can eliminate this problem by using printf:



System.out.printf("The result is %d", a^a);





share|improve this answer






















  • @JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
    – Jai
    Dec 4 at 5:43






  • 4




    @Jai The OP surely used an IDE as without, he wouldn’t even try to run a program which didn’t compile. Besides that, here, the original Java designers are to blame. Using + for string concatenation was a big mistake. It not only has an unfortunate precedence, the usual expectation of commutativity for an addition does not hold when at least one of the operands of + is a string. But even Java itself has an alternative, System.out.printf("The result is %d", a^a);
    – Holger
    Dec 4 at 7:44











  • @Holger Hmm, my Eclipse is showing a compile time error when I do this. I guess it's the same whichever way - you can always miss the error messages and run the application...
    – Jai
    Dec 4 at 7:55










  • @Jai the standard javac command line tool does not generate .class files when there are compile errors, hence, it is impossible to run the application afterwards. It is a special (dubious imho) option of IDEs to still generate classes on compiler errors, which will throw an exception at runtime then. You can turn this feature off and never miss an error, but the default is on for most IDEs.
    – Holger
    Dec 4 at 8:06














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


Or as Holger mentioned, you can eliminate this problem by using printf:



System.out.printf("The result is %d", a^a);





share|improve this answer






















  • @JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
    – Jai
    Dec 4 at 5:43






  • 4




    @Jai The OP surely used an IDE as without, he wouldn’t even try to run a program which didn’t compile. Besides that, here, the original Java designers are to blame. Using + for string concatenation was a big mistake. It not only has an unfortunate precedence, the usual expectation of commutativity for an addition does not hold when at least one of the operands of + is a string. But even Java itself has an alternative, System.out.printf("The result is %d", a^a);
    – Holger
    Dec 4 at 7:44











  • @Holger Hmm, my Eclipse is showing a compile time error when I do this. I guess it's the same whichever way - you can always miss the error messages and run the application...
    – Jai
    Dec 4 at 7:55










  • @Jai the standard javac command line tool does not generate .class files when there are compile errors, hence, it is impossible to run the application afterwards. It is a special (dubious imho) option of IDEs to still generate classes on compiler errors, which will throw an exception at runtime then. You can turn this feature off and never miss an error, but the default is on for most IDEs.
    – Holger
    Dec 4 at 8:06












up vote
22
down vote



accepted







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


Or as Holger mentioned, you can eliminate this problem by using printf:



System.out.printf("The result is %d", 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));


Or as Holger mentioned, you can eliminate this problem by using printf:



System.out.printf("The result is %d", a^a);






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 5 at 0:05

























answered Dec 4 at 5:37









GBlodgett

8,51441531




8,51441531











  • @JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
    – Jai
    Dec 4 at 5:43






  • 4




    @Jai The OP surely used an IDE as without, he wouldn’t even try to run a program which didn’t compile. Besides that, here, the original Java designers are to blame. Using + for string concatenation was a big mistake. It not only has an unfortunate precedence, the usual expectation of commutativity for an addition does not hold when at least one of the operands of + is a string. But even Java itself has an alternative, System.out.printf("The result is %d", a^a);
    – Holger
    Dec 4 at 7:44











  • @Holger Hmm, my Eclipse is showing a compile time error when I do this. I guess it's the same whichever way - you can always miss the error messages and run the application...
    – Jai
    Dec 4 at 7:55










  • @Jai the standard javac command line tool does not generate .class files when there are compile errors, hence, it is impossible to run the application afterwards. It is a special (dubious imho) option of IDEs to still generate classes on compiler errors, which will throw an exception at runtime then. You can turn this feature off and never miss an error, but the default is on for most IDEs.
    – Holger
    Dec 4 at 8:06
















  • @JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
    – Jai
    Dec 4 at 5:43






  • 4




    @Jai The OP surely used an IDE as without, he wouldn’t even try to run a program which didn’t compile. Besides that, here, the original Java designers are to blame. Using + for string concatenation was a big mistake. It not only has an unfortunate precedence, the usual expectation of commutativity for an addition does not hold when at least one of the operands of + is a string. But even Java itself has an alternative, System.out.printf("The result is %d", a^a);
    – Holger
    Dec 4 at 7:44











  • @Holger Hmm, my Eclipse is showing a compile time error when I do this. I guess it's the same whichever way - you can always miss the error messages and run the application...
    – Jai
    Dec 4 at 7:55










  • @Jai the standard javac command line tool does not generate .class files when there are compile errors, hence, it is impossible to run the application afterwards. It is a special (dubious imho) option of IDEs to still generate classes on compiler errors, which will throw an exception at runtime then. You can turn this feature off and never miss an error, but the default is on for most IDEs.
    – Holger
    Dec 4 at 8:06















@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
Dec 4 at 5:43




@JohnAllison If you used an IDE, you would probably know about this before you actually compile and run it.
– Jai
Dec 4 at 5:43




4




4




@Jai The OP surely used an IDE as without, he wouldn’t even try to run a program which didn’t compile. Besides that, here, the original Java designers are to blame. Using + for string concatenation was a big mistake. It not only has an unfortunate precedence, the usual expectation of commutativity for an addition does not hold when at least one of the operands of + is a string. But even Java itself has an alternative, System.out.printf("The result is %d", a^a);
– Holger
Dec 4 at 7:44





@Jai The OP surely used an IDE as without, he wouldn’t even try to run a program which didn’t compile. Besides that, here, the original Java designers are to blame. Using + for string concatenation was a big mistake. It not only has an unfortunate precedence, the usual expectation of commutativity for an addition does not hold when at least one of the operands of + is a string. But even Java itself has an alternative, System.out.printf("The result is %d", a^a);
– Holger
Dec 4 at 7:44













@Holger Hmm, my Eclipse is showing a compile time error when I do this. I guess it's the same whichever way - you can always miss the error messages and run the application...
– Jai
Dec 4 at 7:55




@Holger Hmm, my Eclipse is showing a compile time error when I do this. I guess it's the same whichever way - you can always miss the error messages and run the application...
– Jai
Dec 4 at 7:55












@Jai the standard javac command line tool does not generate .class files when there are compile errors, hence, it is impossible to run the application afterwards. It is a special (dubious imho) option of IDEs to still generate classes on compiler errors, which will throw an exception at runtime then. You can turn this feature off and never miss an error, but the default is on for most IDEs.
– Holger
Dec 4 at 8:06




@Jai the standard javac command line tool does not generate .class files when there are compile errors, hence, it is impossible to run the application afterwards. It is a special (dubious imho) option of IDEs to still generate classes on compiler errors, which will throw an exception at runtime then. You can turn this feature off and never miss an error, but the default is on for most IDEs.
– Holger
Dec 4 at 8:06

















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

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?