As you know we have started the Java tutorial series. In the previous post, we have discussed some methods. I hope you have practice on them. If you really want to learn them keep practicing on every small thing you learn in Java or any other programming languages. I cover small topics in my post because it is easier to understand and you get some time to practice what you have learned before learning something new. Today we are going to understand Booleans Expressions so let's start without wasting time.
We have discussed booleans in the introduction post of Java but that was just definition. I believe the boolean should be explained properly just like the other post because boolean is one of the important topic that should be explained properly. When I started learning to programming was quite confused about booleans as it is the most simple and easy topic in the programming but because of no proper explanations, I was facing too many problems while coding. That is why I want to explain every small thing so you don't get confused just like me :P.
If we talk about the definition of Java booleans it should be like " A Booleans data values in java only return two boolean values and that is True or False.
Like in the snap, you can see that I have created a boolean variable named checked and declared it true and then when I am printing it on the console you can see that it is printing true. If I try to assign something else it will throw some errors.
In the above screenshot, you can see that the IDE is telling me that I am assigning a String value to a boolean variable which is incompatible type. This is a very basic thing but I just wanted to add it just for sake of understanding.
Example.
Here we are using the If-else statement(we will discuss the If-else statements in java in the upcoming posts try to understand the booleans for now). We have a boolean variable which is true and then we are checking the condition with the help of if-else statement. So if the condition is true( Which is true) it will print The condition is true or if the condition is not true then it will print The condition is false. Here we can use the ! NOT operator to change the results.
Now what exactly we are doing here is telling the compiler that if the condition is not true then execute the particular statement.
Remember
!condition is always false
package com.company;
public class Main {
public static void main(String[] args) {
//@pakgamer---Hive----Topic Java Booleans
boolean check = true;
if(check) {
System.out.println("The condition is true");
}
else {
System.out.println("The condition is false");
}
int num1=7;
int num2=10;
if(num1>num2){
System.out.println(true);
}
else {
System.out.println(false);
}
}
}
That's all. Now If you like this post please do upvote and comment Thank you