Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

The Complete Guide to Basic Operators in Java

Author: Ragul Ram
by Ragul Ram
Posted: Aug 04, 2022

Basic Operators in Java

Java has a robust set of operators for manipulating variables.

Operators of Java
  • Arithmetic Operators

  • Relational Operators

  • Bitwise Operators

  • Assignment Operators

  • Logical Operators

Arithmetic Operators

Arithmetic operators, like algebraic operators, are utilized in mathematical expressions.

  • Addition: Adds two operands ( + )

  • Subtraction: Subtracts two operands ( - )

  • Multiplication: Multiplies two operands ( * )

  • Division: Divides the first operand by the second ( / )

  • Modulus: Returns the remainder when the first operand is divided by the second ( % )

Sample Code:

public class OperatorExample{

public static void main(String args[]){

System.out.println(10*10/5+3-1*4/2);

}}

Relational Operators

The relational operators define the relationship between two operands.

  • The '==' operator determines whether or not the two operands are equal. If this is the case, it returns true. If not, it returns false. 5==5 will, for example, return true.

  • The '!=' operator determines whether or not the two operands are equal. If it does not, it returns true. If not, it returns false. It is the boolean equivalent of the '==' operator. 5!=5 will, for example, return false.

  • The '>' operator determines if the first operand is greater than the second. If this is the case, it returns true. If not, it returns false. 6>5 will, for example, yield true.

Sample Code:

class

}

Bitwise Operators

Java includes a number of bitwise operators for working with integer types such as long, int, short, char, and byte. Bitwise operators operate on the binary representation of numbers bit by bit.

  • & (bitwise 'and' ): The & operator performs a binary AND operation on the operands bit by bit.

  • | (bitwise 'or' ): The bitwise | operator performs binary OR operations on the operands bit by bit.

  • ( bitwise 'XOR' ): The bitwise operator performs binary XOR operations on the operands bit by bit.

Sample Code:

public class bitwise {

public static void main(String args[]) {

int A = 0x014;

int B = 0x016;

System.out.println("A & B=" + (A & B));

System.out.println("A | B=" + (A | B));

System.out.println(" ~A =" + (~A));

}

}

Assignment Operators

A variable's value is assigned using the assignment operator.

  • " = " Simple assignment operator. Assigns values from right side operands to left side operands. C = A + B will assign value of A + B into C

  • " += " Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand. C += A is equivalent to C = C + A

  • " -= " Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C – A

  • " *= " Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. C *= A is equivalent to C = C * A

Logical Operators

Operations like logical AND, logical OR, and others are carried out via logical operators. Assume that variable a has the value true and variable b has the value false.

  • Logical AND ( "&&'' ): If both the left and right operands are true, this operator returns true; otherwise, it returns false. For instance, a && b is false.

  • Logical OR ( ''||'' ): This operator returns true if either of the left or right operands is true. When both the left and right operands are false, it returns false. For instance, a || b is True.

  • Logical NOT (''! '' ): This is a unary operator that can only be used with one operand. This will return true if the operand is false and false if it is true. For instance,!a is false, and! b is true.

About the Author

The greatest Automation Anywhere training is offered in Chennai by Login360. We provide a variety of software-related courses along with complete placement assistance.

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Ragul Ram

Ragul Ram

Member since: Aug 01, 2022
Published articles: 2

Related Articles