Java Operators Homework Help

Java Operators:

Operators is a symbols used to perform some operations with values. We have following list of operators supported by java.

  • Basic Arithmetic Operators
  • Bitwise Operators
  • Relational Operators
  • Assignment Operators
  • Logical Operators
  • Ternary Operator

Arithmetic Operators:

It is used to do common mathematical operations. If we consider x=40, y=20 then the result should be like following table.

Operator

Name

Description

Example

Answer

+

Addition

Add two values

x+y

60

-

Subtraction

Subtract values from one another

x-y

20

*

Multiplication

Multiple two values

x*y

800

/

Division

Divide values from one another

x/y

2

%

Modulas

Returns the reminder

x%y

0

++

Increment

Increase the value by 1

++x

41

--

Decrement

Decrease value by 1

--x

39

Relational Operators:

We consider x=40, y=20 then the result should be like following table.

Operator

Name

Description

Example

Answer

==

Equal to

Check if two values are equal

x==y

False

!=

Not equal to

Check if two values are not equal

x!=y

True

>

Greater than

Check left side value is larger than right side value

x>y

True

<

Less than

Check left side value is less than right side value

x<y

False

>=

Greater than equal to

Check left side value is larger or equal to right side value

x>=y

True

<=

Less than equal to

Check left side value is less than or equal to right side value

x<=y

False

Bitwise Operators:

It performs bit by bit level. If we consider x=11, y=22 the result is

Operator

Name

Description

Example

Answer

&

Bitwise and

copies a bit to the result if both operands are exist.

x&y

2 00000010

|

Bitwise or

copies a bit to the result if any one operands are exist.

x|y

31 00011111

^

Bitwise XOR

copies a bit to the result if not both operands but set in one.

x^y

29 00011101

~

Bitwise Compliment

effect of 'flipping' bits

~x

-12 11110100

<<

Left shift

The left side value is moved left by the number of bits specified by the right side value

x<<2

44 00101100

>>

Right shift

The right side value is moved right by the number of bits specified by the right side value

x>>2

2 00000010

Logical Operators:

If we consider A holds true b holds false means the result is

Operator

Name

Description

Example

Answer

&&

Logical and

If both the operands are non-zero, then it becomes true.

x&&y

False

||

Logical or

If any one of the operands are non-zero, then it becomes true.

x||y

True

!

Logical not

If both conditions makes true logical not make it false

!(x&&y)

True

Assignment Operators:

Operator

Name

Description

Example

=

Simple assignment

Assign values right side to left side

x=y

+=

Add AND assignment

Add right to left and assign to left operand

x+=y

-=

subract AND assignment

subract right to left and assign to left operand

x-=y

*=

Multiply AND assignment

multiply right with left and assign to left operand

x*=y

/=

Divide AND assignment

divide right with left and assign to left operand

x/=y

%=

Modulus AND assignment

Modulus of two operand and assign to left operand

x%=y

<<=

Left shift AND assignment

Left shift AND assign value

x<<=3

>>=

Right shift AND assignment

Right shift AND assign value

x>>=3

&=

Bitwise AND assignment

Bitwise AND assign value

x&=3

|=

Bitwise inclusive or assignment

Bitwise inclusive or assign value

x|=3

^=

Bitwise exclusive or assignment

Bitwise exclusive or assign value

x^=3

Ternary Operator (?:):

The conditional operator is called ternary operator. It consists of three operands. Depends on Boolean result it assign value to operand.

variable x = (expression) ? value if true : value if false

Example:

public class sample
 {
 public static void main(String args[]) {
 int x, y;
 x = 20;
 y = (x != 1) ? 30: 40;
 System.out.println( " y is : " + y );
 y = (x == 10) ? 30: 40;
 System.out.println( " y is : " + y );
 }
}

Output:

y is : 30

y is :40