-6

Hi is something like this possible in Java?

boolean flag = true;
if(flag) return flag = false; // return true and assign false to flag afterwards

To clarify. The above works, but is assigns false first. Want I want to achieve is to return the flag as soon as its true and reset it to false afterwards.

The structure looks something like this:

boolean flag = false;
// some operations which can set the flag true
if(flag){ flag = false ; return true};
// some operations which can set the flag true
if(flag){ flag = false ; return true};
// some operations which can set the flag true
if(flag){ flag = false ; return true};

I was thinking about to do it in one go by return flag = false;

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • Why don't you just use `if(flag){ flag = false; return true; }`? – Kevin Cruijssen May 30 '16 at 14:02
  • @KevinCruijssen Yes, that would work. Was just curious if there is something for that case. – Murat Karagöz May 30 '16 at 14:03
  • 1
    Use an atomic boolean: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicBoolean.html#getAndSet(boolean) – StephaneM May 30 '16 at 14:03
  • 1
    If you return a value, then the function is over, and no more assignments may be performed. Your question is either too broad, or [XY](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) resolution on the wrong problem. Please, edit your question, add details, the wanted behavior, and the seen behavior. – Bonatti May 30 '16 at 14:04
  • It does work, but it assigns `false` *before* it returns it. – RaminS May 30 '16 at 14:04
  • 1
    `"but is assigns false first"` - Of course it does. Much in the same way that `x = 1 + 2` performs the addition *first*, *before* assigning the result to `x`. How else would it work? – David May 30 '16 at 14:08
  • It is interesting to see how a simple question about Java can generate so much interest in stackoverflow – pedrofb May 30 '16 at 14:16
  • 1
    http://ideone.com/QWXOrL – zapl May 30 '16 at 15:26

3 Answers3

5

No, there's nothing built-in that does what you describe. You'd do it with a temporary variable:

boolean flag = true;
boolean returnValue = flag;
flag = false;
return returnValue;

Or based on your further edit to the question ("The structure looks something like this"), you can use !:

boolean flag = false;
// some operations which can set the flag true
if(flag) return !(flag = false);
// some operations which can set the flag true
if(flag) return !(flag = false);
// some operations which can set the flag true
if(flag) return !(flag = false);

I really, really would not do that. It's unnecessarily obtuse.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 4
    @MuratK.: Fatigue. There are so many questions posted with very little explanation and even less thought, that when a vague question is posted (and the original question was very vague), people are just so tired of it they react more harshly than they arguably should, or would in real life. It's well worth spending time reviewing your question before posting, asking yourself if you're assuming information you haven't included, trying to ensure you're concise but not unclear, etc., so that the initial version of the question looks more like your final version. – T.J. Crowder May 30 '16 at 14:16
2

Have a look at java.util.concurrent.AtomicBoolean. I haven't tried this, but it might give the behavior you're asking about:

AtomicBoolean flag = new AtomicBoolean(true);
System.out.println("First, I'm " + flag.get());
Boolean was = flag.getAndSet(false);
System.out.println("I was " + was + " but now I'm " +
    Flag.get());
Kevin Anderson
  • 4,568
  • 3
  • 13
  • 21
2

No, there isn't a way to do that.

Why not?

Well you would need to ask the Java language designers for the real answer, but I imagine that they would have dismissed such a proposal out of hand. Java is designed to be a language that is easy to learn, read and understand. Adding operators that are designed to do "clever" things in a concise way is liable to make the language harder to learn, and harder to read ... for the average programmer. And, if the operator is only really useful in a tiny number of use-cases, that makes the readability versus utility argument even harder to win.

Also, adding new features to Java is often more technically difficult than you would imagine because of interactions with other (existing) language features.

And actually, there is precedent to back this up. One of the Java 7 / 8 revision proposals in Project Coin was to add an elvis operator to Java. The proposal was considered ... and ultimately dismissed.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216