How to change variables elegantly?

How do I change two variables (of different types) on the same line in Java?

  • For instance, I want to combine the following two statements so they are on the same line in an if statement: ref = i b = true ref is an integer and b is a boolean. How do I change them at the same time?

  • Answer:

    Quite a strange question. Is that what you mean? ... ref = i; b = true; ... and is equivalent to: ... ref = i; b = true; ... and has no impact on performance. The 2nd way is more readable.

SKKKKKAF... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

You can separate the statements by a semi-colon and enclose that in curly braces, on the same line, i.e.: { ref = i; b = true; }

alex

That's generally not something that people want to do. In addition to the fact that the computer most likely isn't going to do them at the same time anyways, you would be mucking up your code, and making it less readable for other developers to maintain. If you could give us some insight into WHY you want to do this, someone might be of more help in addressing your issue.

thedude81321

Booleans may not be cast into any other type of variable nor may any other variable be cast into a boolean.

just_me ♦

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.