Java defines several operators. To perform the different types of operations on variables and their values this operators are use.

Logic allows you to control what part of a function is evaluated based on changes
to variables. Using logic, you’ll be able to change which statements in your code
will run. Simply put, everything you write must cover each situation you plan
to cover. Logic is controlled through a few simple systems, primarily the if keyword and variations of if.

Boolean:

In Java booleans, or bools for short, are either true or false. It’s easiest to think of
these as switches either in on or in off position. To declare a var as a bool, you
use something like the following.

public class Example
{
public bool SomeBool;
}

Equality Operators:

Equality operators create boolean conditions. There are many ways to set a
boolean variable. For instance, comparisons between values are a useful means to set variables. The most basic method to determine equality is using the
following operator: ==.

There’s a difference between use of a single and a double equals to symbol. = is used to assign a value whereas == is used to compare values. When you need to compare two values you can use the following concept.
You’ll need to remember that these operators are called equality operators, if you need to talk to a programmer. The syntax here may look a bit confusing at first, but there are ways around that.

void Func ()
{
SomeBool = (1 == 1);
}

There are other operators to be aware of. You will be introduced to the other
logical operators later in the chapter. In this case, we are asking if two number
values are the same. To make this a more clear, we can break out the code into more lines. Now, we’re looking at a versus b. Clearly, they don’t look the same; they are different letters after all. However, they do contain the same integer value, and that’s what’s really being compared here.

void Func ()
{i
nt a = 1;
int b = 1;
SomeBool = (a == b);
}

Evaluations have a left and a right side. The single equal to operator (=) separates the different sides. The left side of the = is calculated and looks to the value to the right to get its assignment. Because 1 == 1, that is to say, 1 is equivalent to 1, the final result of the statement is that SomeBool is true.

Relational Operators :

Bool values can also be set by comparing values. The operators used to compare two different values are called relational operators. We use ==, the is equal symbol, to check if values are the same; we can also use !=, or not equal, to check of two variables are different. This works similarly to the ! in the previous section. Programmers more often check if one value is greater or lesser than another value. They do this by using >, or greater than, and <, or less than. We can also use > =, greater or equal to, and < =, less than or equal to.

operators

Let’s see a few examples of how to use operators in java.

public class Test 
{
 public static void main(String args[]) 
{ 
int a = 10; 
int b = 20; 
System.out.println("a == b = " + (a == b) ); 
System.out.println("a != b = " + (a != b) ); 
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) ); 
System.out.println("b >= a = " + (b >=a) );
System.out.println("b <= a = " + (b <= a) ); 
} 
} 

Output:

a == b = false a != b = true a > b = false
a < b = true b >= a = true
b <= a = false

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top