Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to understand the basics of for loops and how they work with booleans. There is a simple few lines of code that I wrote and I don't quite understand why the output is what it is.
public class notes {
public static void main(String[] args) {
boolean[] test = {false, false, true};
if (test[0] = false) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
I would assume that because the boolean value at index 0 in the test array is false, then this if statement should also print yes. However, it prints no. Why is this?
Try it like this. Since conditionals result in a true or false condition you can just use the boolean by itself. Since you want it to print yes when false, you need to make it true so the conditional will succeed. So invert the condition by prefixing a bang ! (aka the NOT operator).
public class notes {
public static void main(String[] args) {
boolean[] test = {false, false, true};
// if false do it.
if(!test[0]) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
You are assigning false to the first element of the array, which evaluates to false and causes the else branch to be executed instead. You need to use == for comparison and in the case of booleans, you can simply use the logical not operator (!) to check if it is false. It is always redundant to compare boolean values, as the result of comparison is a boolean.
if(!test[0]) {
System.out.println("yes");
} else {
System.out.println("no");
}
You are using an assignment operator =, instead of using a comparison operator ==, hence you don't compare, but rather assign a false value to the index 0 of your array and the same false is then evaluated as a boolean value; therefore, else block executes.
Try this instead:
public class notes {
public static void main(String[] args) {
boolean[] test = {false, false, true};
if(test[0] == false) { //you had a mistake here.
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
Note, that you can alternatively negate boolean expression with ! unary operator, like !booleanValue. In your case it would look like !test[0].
Have a look at operators in Java.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to check whether two expressions are equivalent or not in Java. Let's say I have two expressions:
exp 1:
A && B && ( C || D)
exp 2:
B && ( C || D ) && A
I want to check whether these two expressions are equivalent or not.
What i mean by equivalent was if say we have two expressions then these two expressions must have same tokens(A , B, C, D) and the same relational operators between them. And should return the same values for all inputs. I do not want to consider the order how java execute these things. And what i need is a library or some java code snippet for doing so. Information about an algorithm is also fine.
They are not due to the short evaluation. In Java, A && B returns false immediately when A is evaluated to false. It makes a difference e.g. when it comes to Exceptions or boolean methods that are not pure functions.
You can try the following experiment:
public class Test {
public n = 0;
public boolean A() {
System.out.println("A");
return false;
}
public boolean B() {
System.out.println("B");
return true;
}
public boolean C() {
n++;
return true;
}
public boolean D() {
n = n*2;
return false;
}
public static void main(String[] args) {
Test test = new Test();
if (test.A() && test.B()) { System.out.println("true"); }
if (test.B() && test.A()) { System.out.println("true"); }
if (test.C() && test.D()){}
else {System.out.println(t.n);}
t.n = 0;
if (test.D() && test.C()){}
else {System.out.println(t.n);}
t.n = 0;
boolean c = test.C();
boolean d = test.D();
if (d && c){}
else {System.out.println(t.n);}
}
}
It shows the consequences of a short evaluation connected with side effect.
This is my first time asking a question here, so I'll ask you to bear with me: I am trying to create a public boolean method, isEven(), that will check if a number is evenly divisible by two and return a value of true or false based on that. However, as this is a public method, I am unsure of how exactly to write it; this is my process thus far:
public boolean isEven()
{
if(WHAT_GOES_HERE? % 2 == 0)
return true;
else
return false;
}
I would appreciate some advice on how exactly to go about writing this method; thanks in advance!
The simplest way would be
public boolean isEven(int value){
return value % 2 == 0;
}
Using an if/else statement to return or set variables to boolean values is almost always redundant. Since you can return the condition you put in the if/else itself, the if/else is not needed.
This question already has answers here:
Difference between Return and Break statements
(14 answers)
Closed 5 years ago.
I am using a boolean that returns true. When something like this happens:
public boolean t(String label, String[] arguments) {
boolean j = false;
if (!j) {
return true;
}
return true;
}
However, inside of this boolean I have a switch statement. As you may know, you break out of switch statements usually. However I am doing something like this testing for booleans to be true or false. Would I return inside of a switch statement, or break if a boolean is false?
An example would be something like this:
switch (arguments.length) {
case 0:
if (j) return true;
break;
default:
break;
}
Which would be more useful, exchanging return true for break, or keep it the same?
A break statement would terminate execution of the switch statement and continue in the method. A return statement would leave the method entirely.
It's a matter of preference, really, as to which one is better. It comes down to method design.
I would say in most cases like you have above, it would be better to directly use a return statement and leave the method entirely.
That said, this:
public boolean t(String label, String[] arguments) {
boolean j = false;
if (!j) {
return true;
}
return true;
}
Can be simplified to this:
public boolean t(String label, String[] arguments) {
return true;
}
And if you're only checking one condition from one variable, then you don't need a switch statement; it's not as optimal.
I am doing something like this testing for booleans to be true or false
You shouldn't really "test" for boolean variables to be true or false, just return them.
Return call always returns the handler to the called function where as break in the switch statement is to avoid checking other cases present in the switch case. If you don want your program return any values then break is sufficient else return required.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have the following boolean method in Java, but I can't understand its return statement because it uses a ternary operation. Can anyone rewrite it to an if/else statement so that I can better understand what the ternary operation is doing?
public boolean collidesWith(Rectangle object){
return (isDestroyed)? false:hitbox.intersects(object);
}
The ternary operator is a short-hand for writing an if-else statement. Its general for is
<boolean condition to evaluate> ?
<return value if condition is true - i.e., the "if" branch > :
<return value is condition is false - i.e., the "else" branch>
So, if you unwrap the method you showed, you'd get:
public boolean collidesWith(Rectangle object){
if (isDestroyed) {
return false;
} else {
return hitbox.intersects(object);
}
}
Firstly, here is how I would write the method you posted (adding whitespace):
public boolean collidesWith(Rectangle object) {
return isDestroyed ? false : hitbox.intersects(object);
}
Here is the if-else you're looking for:
public boolean collidesWith(Rectangle object) {
if (isDestroyed) {
return false;
}
else {
return hitbox.intersects(object);
}
}
..or a bit simplified:
public boolean collidesWith(Rectangle object) {
if (isDestroyed)
return false;
return hitbox.intersects(object);
}
You may also make the ternary operator look a bit like an if-else:
public boolean collidesWith(Rectangle object) {
return isDestroyed ?
false :
hitbox.intersects(object);
}
public boolean collidesWith(Rectangle object){
if(isDestroyed)
return false;
else
return hitbox.intersects(object);
}
I tried to make a method to tell me if my array contained any duplicate coordinates, and if so set a boolean to true, else, set it to false. Any idea why it continuously returns false?
public void check(){
if(point[particle].equals(point) == true){
check = true;
} else {
check = false;
}
}
point = Point array
particle = Current particle
check = My boolean used to check
Look at this part of the code (from the condition of your if-statement):
point[particle].equals(point)
I think there is some variable shadowing (or something of that sort) going on here. point seems to be an array but you are testing for equality between a member of this array and the array itself -- this is why you keep getting false. Check your variable names and see if you accidentally used the same name for two different variables.
As a note,
if (condition == true) {
check = true;
} else {
check = false;
}
can be simplified to
check = condition;