Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

java - My if statement ALWAYS returns true, no matter what

This just is't making sense to me at all.

This is my code:

boolean that = false;
        if (that == true);
        {
            System.out.println("That is " + that);
        }

And yet it will print the line even though my output is

That is false

I'm doing this in java and I'm using Eclipse galileo, so each time I compile/run my program it saves it so the compiler should be getting updated version of my program. What's going on?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A common mistake. Remove the ; at the end of the if statement.

BTW I always write the following if I use brackets and I use the code formatter of the IDE.

    if (that == true) {
        System.out.println("That is " + that);
    }

This means if you have a mis-placed ; or { it can be more obvious.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...