- Class Boolean
- Field Summary
- Constructor Summary
- Method Summary
- Methods declared in class java.lang.Object
- Field Details
- TRUE
- FALSE
- TYPE
- Constructor Details
- Boolean
- Boolean
- Method Details
- parseBoolean
- booleanValue
- valueOf
- valueOf
- toString
- toString
- hashCode
- hashCode
- equals
- getBoolean
- compareTo
- compare
- logicalAnd
- logicalOr
- logicalXor
- describeConstable
- How to Return a Boolean Method in Java
- How to Return a Boolean Method in Java?
- Example 1: Implementing a Simple Boolean Method
- Example 2: Adding if-else Conditional Statement in Boolean Method
- Example 3: Checking If a Number is Odd or Even Using Boolean Method
- Conclusion
- About the author
- Farah Batool
- Return a Boolean Method in Java
- Structure of a Boolean Method With a return Statement in Java
- Return a Boolean Method — Example 1
- Return a Boolean Method — Example 2
- Return a Boolean Method for a List in Java
- Related Article — Java Function
Class Boolean
The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean .
In addition, this class provides many methods for converting a boolean to a String and a String to a boolean , as well as other constants and methods useful when dealing with a boolean .
This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
Field Summary
Constructor Summary
Method Summary
Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.
Returns true if and only if the system property named by the argument exists and is equal to, ignoring case, the string «true» .
Methods declared in class java.lang.Object
Field Details
TRUE
FALSE
TYPE
Constructor Details
Boolean
It is rarely appropriate to use this constructor. The static factory valueOf(boolean) is generally a better choice, as it is likely to yield significantly better space and time performance. Also consider using the final fields TRUE and FALSE if possible.
Boolean
It is rarely appropriate to use this constructor. Use parseBoolean(String) to convert a string to a boolean primitive, or use valueOf(String) to convert a string to a Boolean object.
Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string «true» . Otherwise, allocates a Boolean object representing the value false .
Method Details
parseBoolean
Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string «true» . Otherwise, a false value is returned, including for a null argument. Example: Boolean.parseBoolean(«True») returns true .
Example: Boolean.parseBoolean(«yes») returns false .
booleanValue
valueOf
Returns a Boolean instance representing the specified boolean value. If the specified boolean value is true , this method returns Boolean.TRUE ; if it is false , this method returns Boolean.FALSE . If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean) , as this method is likely to yield significantly better space and time performance.
valueOf
Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string «true» . Otherwise, a false value is returned, including for a null argument.
toString
Returns a String object representing the specified boolean. If the specified boolean is true , then the string «true» will be returned, otherwise the string «false» will be returned.
toString
Returns a String object representing this Boolean’s value. If this object represents the value true , a string equal to «true» is returned. Otherwise, a string equal to «false» is returned.
hashCode
hashCode
equals
Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.
getBoolean
Returns true if and only if the system property named by the argument exists and is equal to, ignoring case, the string «true» . A system property is accessible through getProperty , a method defined by the System class. If there is no property with the specified name, or if the specified name is empty or null, then false is returned.
compareTo
compare
Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
logicalAnd
logicalOr
logicalXor
describeConstable
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
How to Return a Boolean Method in Java
In Java, you can declare a method using the void keyword or with primitive data types. The keyword is used when you do not want to return anything from the method. However, if you return an integer type value, then declare the method with the int keyword. Similarly, “boolean” is also a primitive data type in Java, and it is used to declare a method when you want to return a boolean value.
This blog will explain the procedure to return a boolean method in Java.
How to Return a Boolean Method in Java?
As we discussed earlier, the return type of method is mentioned in the method declaration. If a method is declared with a boolean return type, it gives a boolean value.
Syntax
Follow the syntax for returning a boolean method in Java.
Here, “abc()” is a boolean method that returns the boolean value “false”.
Now, let’s head toward the implementation of the Boolean method in Java.
Example 1: Implementing a Simple Boolean Method
We will create a boolean method named “value()” that contains a boolean variable “a” with the value “true”. The return statement of this method will be a boolean as the method is declared as a boolean type:
We will call the boolean method value() in the main() method to print out the returned value:
The output displayed the “true” as the returned value:
Let’s see how the boolean method works with conditional statements.
Example 2: Adding if-else Conditional Statement in Boolean Method
Here, we will create a boolean method named “isGreater()” with an integer type parameter “num”. If num is greater than “50”, the method will return “true” else “false”:
public static boolean isGreater ( int num ) {
if ( num > 50 ) {
return true ;
}
else {
return false ;
}
}
We will call the isGreater() method by passing a number “85” in main() method, and check if the returned value is equals to true, then it will print “True”, else display “False”:
public static void main ( String [ ] args ) {
if ( isGreater ( 85 ) == true ) {
System. out . println ( «True» ) ;
} else {
System. out . println ( «False» ) ;
}
Output
Look at one more example to understand the concept.
Example 3: Checking If a Number is Odd or Even Using Boolean Method
First, we will create a boolean method named “isOdd()” that returns the boolean value true or false. The statement “return (num % 2 != 0)” will return true, if the result is not equals to 0, otherwise it returns false:
Now, in the main() method, we will create an integer type variable named “number” assigned with value “89”. The “isOdd()” method will accept the created integer as an argument. The given method will print out the specified statements according to the evaluation of the given condition:
public static void main ( String [ ] args ) {
int number = 89 ;
if ( isOdd ( number ) == true ) {
System. out . print ( «89 is an odd Number» ) ;
} else {
System. out . print ( «89 is an even Number» ) ; }
}
The output shows “True” as the isOdd() method returned true:
We gathered all the instructions to return a boolean method in Java.
Conclusion
In Java, you must declare a method of the boolean type in order for it to return a boolean value. The boolean method will return the boolean value, true or false. You can either return the variable containing a boolean value or use conditional statements to decide the returned value. In this blog, we explained the procedure to return a boolean method in Java with detailed examples.
About the author
Farah Batool
I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.
Return a Boolean Method in Java
- Structure of a Boolean Method With a return Statement in Java
- Return a Boolean Method — Example 1
- Return a Boolean Method — Example 2
This article will introduce methods to return a boolean method in Java.
Structure of a Boolean Method With a return Statement in Java
Consider the code snippet below.
public boolean CheckPassword(String pass) >
- public : this is a modifier that shows that the class, field, method, and constructor can be accessed by all codes regardless of the location.
- boolean : This identifies the type of value expected to return after the method performs the specified tasks.
- checkPassword() : This the name of the method.
- String : This declares the parameter types that the method will accept.
From the method structure above, a method’s return type is declared in the method declaration. A method that declares the return type void will not contain a return statement. Any method that is not declared void must contain the return statement used to exit the method.
Return a Boolean Method — Example 1
The sample code below checks whether a number is even or odd. The boolean method returns true if it is even and false when it is odd.
public class booleanMethod public static boolean isEven(int mynumber) return (mynumber % 2 == 0); > public static void main(String[] args) int mynumber = 130; if(isEven(mynumber) == true) System.out.print("Even Number"); else System.out.print("Odd Number"); > >
In the code above, the first step is to declare the boolean method and the return value expected. The boolean method returns a value that guides how the code login is implemented in the next method. The public declaration allows the code to be accessed by other methods to check whether the numbers passed are even or odd.
In the second method, each time an int is passed to check whether it is even or odd, the boolean method assigns a boolean value to the outcome. This boolean value is then used to determine the output of the method.
Return a Boolean Method — Example 2
The code sample below checks whether a student’s score is above or below 72.
public class booleanMethod public static boolean isAbove (int thescore) return (thescore > 72 ); > public static void main(String[] args) int thescore = 56; if(isAbove(thescore) == true) System.out.print("Above average"); else System.out.print("Below average"); > >
The first method declares the return value as boolean and the acceptable parameter as int. This method’s return statement is of boolean type based on whether the parameter is above or below 72. If the int value passed is below 72, the method returns false , and if the value passed is above 72, the method returns true .
The method below uses the value returned from the boolean method to determine whether the given score is above or below the average score. This simplifies the code and ensures that a developer doesn’t have to repeat each value’s comparison process.
Return a Boolean Method for a List in Java
The sample code below checks how many students scored above 75 marks in a class using the boolean method’s return statement.
import java.util.*; import java.util.*; public class booleanMethod public static boolean isAbove (int thescore) return (thescore > 75 ); > public static void main(String[] args) int aboveAverage=0; int belowAverage=0; List Integer> classscores= new ArrayList<>(); classscores.add(90); classscores.add(63); classscores.add(72); classscores.add(75); classscores.add(81); classscores.add(52); classscores.add(69); classscores.add(78); for (int i=0; iclassscores.size(); i++) if(isAbove(classscores.get(i))==true) aboveAverage++; >else belowAverage++; > > System.out.print( aboveAverage + " scored above 75 and "+ belowAverage + " scored below 75"); > >
3 scored above 75 and 5 scored below 75