Operators Controllers Loops

Incrementing (++) and Decrementing (--):

The ++ operator increments its operand by one and the -- operator decrements its operand by one.

Ex.

public class app
{
public static void main(String[] args)
{
int a=0, b=1;
a++; // Increment operation. As a = a +1
b--; // Decrement operation. As b = b -1
System.out.println("A = "+ a +"\nB = "+ b);
}
}

Output:

A = 1
B = 0

Topñ

Unary Not: ~ and !

The ~ operator is the bitwise unary Not operator and ! is the logical unary Not operator. So , the ~ operator flips all the bits of numeric arguments and the ! operator flips true values to false and false value to true.

public class app
{
public static void main(String[] args)
{
short short1 = 32767;
boolean boolean1 = true;
System.out.println("Most negative short = "+ ~short1); // ~ operator is used in integer value
System.out.println("!true = "+ !boolean1); // ! operator is used in boolean value
}
}

Output:

Most negative short = -32768
!true = false

Topñ

Addition Subtraction Multiplication Division: + - * /

public class app
{
public static void main(String[] args)
{
int a = 2, b = 6, add, subt, mult, div;
add = a + b; // add = 2 + 6 = 8
subt = b - a; // subt= 6 - 2 = 4
mult = a * b; // mult = 6 * 2 = 12
div = b / a; // div = 6 / 2 = 3
System.out.println("a = "+ a +"\tb = "+b);
System.out.println("Addition of a and b = "+ add);
System.out.println("Subtraction of a and b = "+ subt);
System.out.println("Multiplication of a and b = "+ mult);
System.out.println("Division of a and b = "+ div);
}
}

Output:

a = 2 b = 6
Addition of a and b = 8
Subtraction of a and b = 4
Multiplication of a and b = 12
Division of a and b = 3

Topñ

Shift Operators: >>, >>>, and <<

Syntax:

new_value = value << number_places;
new_value = value >> number_places;
new_value = value >>> number_places;

Ex.

public class app
{
public static void main(String[] args)
{
int value = 16, negvalue = -1;
System.out.println(value + " << 2 = "+ (value << 2));
System.out.println(value + " >> 2 = "+ (value >> 2));
System.out.println(negvalue + " >> 2 = "+ (negvalue >> 2));
System.out.println(negvalue + " >>> 22 = "+ (negvalue >>> 22));
}
}

Output:

16 << 2 = 64
16 >> 2 = 4
-1 >> 2 = -1
-1 >>> 22 = 1023

Topñ

Relational Operators: > , >= , < , <= , == , !=

You can use relational operators to create logical conditions that you can test with conditional statement such as the if statement.

Ex.

public class app
{
public static void main(String[] args)
{
int budget = 1;
if ( budget < 0 ) // Relational operators (<) is used in if statement
{
System.out.println("Uh oh.");
}
else {
System.out.println("Still solvent.");
}
}
}

Output:

Still solvent

Relational Operators Description
> Greater than
>= Greater than or equal to
< Less then
<= Less than or equal to
== Equal to
!= Not equal to

Topñ

Bitwise and Bitwise Logical And, Xor, Or: &, ^, |

Ex.

public class app
{
public static void main(String[] args)
{
int value = 12;
int bit3setting = value & 1 <<3;
if ( bit3setting != 0){
System.out.println("Bit 3 is set.");
}
else {
System.out.println("Bit 3 is not set.");
}
}
}

Output:

Bit 3 is set.

X Y X|Y (Or) X&Y (And) X^Y (Xor)
0 0 0 0 0
1 0 1 0 1
0 1 1 0 1
1 1 1 1 0

public class App
{
public static void main(String[] args)
{
int buget=1;
boolean fired=false;

if(buget<0 | fired==true){
System.out.println("Uh oh.");
}
else {
System.out.println("Still Solvent");
}
}
}

Output

2419430 kbps Topñ

Logical && and ||:

Topñ

The if-then-else operator: ?:

There's java operator that acts much like an if-else statement - the ternary operator (?:). This operator is called a ternal operator because it takes three operands - a condition and two values.

Syntax: value = condition ? value1 : value2;

If condition is true then value = value1 and when condition is false, value=value2.

Ex.

public class app
{
public static void main(String[] args)
{
int value = 15;
String digit, chars[]={"a","b","c","d","e","f"};
digit = value < 10 ? String.valueOf(value) : chars[value - 10];
System.out.println(value +" = 0x"+digit);
}
}

Output:

15 = 0xf

Topñ

Assignment operators: =

Ex

public class app
{
public static void main(String[] args)
{
int value1, value2, value3;
value1=value2=value3=12;
System.out.println("Value1 = "+ value1);
System.out.println("Value2 = "+ value2);
System.out.println("Value3 = "+ value3);
}
}

Output:

Value1 = 12
Value2 = 12
Value3 = 12

We can combine many operators with the assignment operator (=). For example, += is the addition assignment operators, which means value+=2 is a shortcut for value = value + 2.

Ex.

public class app
{
public static void main(String[] args)
{
int value = 10;
value *= 2; // value = value * 2 => value = 10 * 2 => value = 20
System.out.println("Value * 2 = "+ value);
}
}

Output:

Value * 2 = 20

There are quite a few combination assignment operators.

Assignment Operators Description
% module assignment
&= bitwise And assignment
*= multiplication assignment
/= division assignment
^= bitwise Xor assignment
|= bitwise Or assignment
+= addition assignment
<<= shift left assignment
<= less than or equal to
- = subtraction assignment
>>= shift right assignment
>>>= shift right zero fill assignment

Topñ

Using the Math Class:

We can use the java.lang.Math class to perform a great many operations.

Ex.

public class app
{
public static void main(String[] args)
{
System.out.println("3 * 3 * 3 * 3 = "+ Math.pow(3 ,4));
}
}

Output:

3 * 3 * 3 * 3 = 81.0

Here are the constants and methods of the Math class: Click here.

Topñ

Comparing String

When you're working with the String class, there are some methods you can use much like operators.

Functions Description
string1.equals( string2 ) True if string1 equals string2
string1.equalsIgnoreCase( string2 ) True if string1 equals s2 (Ignoring case)
String1.compareTo( string2 ) Return a value less than zero is string1string2.

Ex.

public class app
{
public static void main( String[] args)
{
String s1="abc";
String s2="abc";
String s3="ABC";
String s4="bcd";
if (s1.equals(s2)){
System.out.println("s1 == s2");
}
else {
System.out.println("s1 != s2");
}

if (s1.equalsIgnoreCase(s3)){
System.out.println("s1 == s3 when ignoring case");
}
else {
System.out.println("s1 != s3 when ignoring case");
}

if (s1.compareTo(s4) < 0){
System.out.println("s1 < s2");
}
else if( s1.compareTo(s4) == 0){
System.out.println("s1 == s2");
}
else if( s1.compareTo(s4) > 0){
System.out.println("s1 > s2");
}
}
}

Output:

s1 == s2
s1 == s3 when ignoring case
s1 < s2