Operator in Java Program – 3
Operator is very important for all java program languages. Operator can calculate, check, assign, increase and decrease the value of variable. So operator is heart of java program. I give simple example of operator in java program.
ans = (x%5==0 && x%7==0 ) ? “Divided” : “Not Divided”;
In this example ans, x and y are variable;
= (assignment) and > (Relational) and && (logical) are operators.
So we can easily understand the important of operator in java program language. In this blog we will discuss on Logical [ &&, || And ! ] operators.
- Logical operator
&&, || and ! are logical operator in java programming languages. These operators help for checking relation of two or more condition. We are familiar with &&, || and ! logical operator in C and Cpp. They work same as C and Cpp. We can also call short circuit operator in java. Let’s see simple logic of logical operator.
&& – Or => Logical Chart
|No||Statement||And [ && ]||Or [ || ]|
|1||Condition 1 True , Condition 2 True||True||True|
|2||Condition 1 True , Condition 2 False||False||True|
|3||Condition 1 False, Condition 2 True||False||True|
|4||Condition 1 False, Condition 2 False||False||False|
This is basic chart of logical operator in java program. Let’s see program on logical operator. In this blog we also introduce command line argument. Command line argument uses for assigning run time value in java.
Program on Logical Operator and command line argument:
public static void main(String args)
int x = Integer.parseInt(args);
String ans = (x%5==0 && x%7==0) ? “Divided” : “Not Divided”;
System.out.println(x + “ is ” + ans);
If we Insert 20 in X then O/P will be : 20 is Not Divided.
If we insert 35 in X then O/P will be: 35 is Divided.
Video available on YouTube.
If you have problem on this blog, you can directly comment or subscribe on website.
1241total visits,1visits today