Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Operators in Java

Operators in Java

Table of Contents

  • Operators in Java
  • Arithmetic operators
  • Relation operators
  • Logical operators
  • Bitwise operators
  • Assignment Operators
  • Misc. operator
    • Conditional operator
    • instanceOf operator

Operators in Java

Operators in Java

An operator is a symbol that instructs the compiler to carry out a specific operation. Java offers a wide range of operators to handle different kinds of operations. When performing arithmetic operations, we occasionally use the plus (+) operator for addition, the multiply(*) operator for multiplication, etc. The fundamental building block of every programming language is the operator.

Java operators can be divided into following categories:

  • Arithmetic operators
  • Relation operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Conditional operators
  • Misc operators

Arithmetic operators

Arithmetic operators are used to perform arithmetic operations like: addition, subtraction etc and helpful to solve mathematical expressions. The below table contains Arithmetic operators.

OperatorDescription
+adds two operands
-subtract second operands from first
*multiply two operand
/divide numerator by denumerator
%remainder of division
++Increment operator increases integer value by one
--Decrement operator decreases integer value by one

Example:

Lets create an example to understand arithmetic operators and their operations.


class Arithmetic_operators1{
public static void main(String as[])
    {
        int a, b, c;
        a=10;
        b=2;
        c=a+b;
        System.out.println("Addtion: "+c);
        c=a-b;
        System.out.println("Substraction: "+c);
        c=a*b;
        System.out.println("Multiplication: "+c);
        c=a/b;
        System.out.println("Division: "+c);
        b=3;
        c=a%b;
        System.out.println("Remainder: "+c);
        a=++a;
        System.out.println("Increment Operator: "+a);
        a=--a;
        System.out.println("decrement Operator: "+a);    
} }

Relation operators

To test comparisons between operands or values, relational operators are used. It can be used to determine whether two values are greater than, less than, or equal to each other.

The following table shows all relation operators supported by Java.

OperatorDescription
==Check if two operand are equal
!=Check if two operand are not equal.
>Check if operand on the left is greater than operand on the right
<Check operand on the left is smaller than right operand
>=check left operand is greater than or equal to right operand
<=Check if operand on left is smaller than or equal to right operand

Example:

In this example, we are using relational operators to test comparison like less than, greater than etc.


class Relational_operators1{
public static void main(String as[])
    {
        int a, b;
        a=40;
        b=30;
        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) );    
    }
}
    

Logical operators

Logical Operators are used to check conditional expression. For example, we can use logical operators in if statement to evaluate conditional based expression. We can use them into loop as well to evaluate a condition.

Java supports following 3 logical operator. Suppose we have two variables whose values are: a=true and b=false.

OperatorDescriptionExample
&&Logical AND(a && b) is false
||Logical OR(a || b) is true
!Logical NOT(!a) is false

Example:

In this example, we are using logical operators. There operators return either true or false value.


class Logical_operators1{
public static void main(String as[])
    {
        boolean a = true;
        boolean b = false;
        System.out.println("a && b = " + (a&&b));
        System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b));
    }
}
    

Bitwise operators

Bitwise operators are used to perform operations bit by bit.

Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byte.

The following table shows all bitwise operators supported by Java.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
<<left shift
>>right shift

Now lets see truth table for bitwise &, | and ^

aba & ba | ba ^ b
00000
01011
10011
11110

The bitwise shift operators shifts the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value are to be shifted. Both operands have the same precedence.

Example:

Lets create an example that shows working of bitwise operators.

a = 0001000
b = 2
a << b = 0100000
a >> b = 0000010 

class Bitwise_operators1{
public static void main(String as[])
    {
        int a = 50; 
        int b = 25; 
        int c = 0;

        c = a & b;        
        System.out.println("a & b = " + c );

        c = a | b;        
        System.out.println("a | b = " + c );

        c = a ^ b;        
        System.out.println("a ^ b = " + c );

        c = ~a;           
        System.out.println("~a = " + c );

        c = a << 2;      
        System.out.println("a << 2 = " + c );

        c = a >> 2;       
        System.out.println("a >>2  = " + c );

        c = a >>> 2;     
        System.out.println("a >>> 2 = " + c );
    }
}
    

Assignment Operators

A variable’s value is assigned using assignment operators. It can also be used in conjunction with mathematical operators to carry out calculations and then assign the outcome to a variable. The following assignment operators are supported by Java:

OperatorDescriptionExample
=assigns values from right side operands to left side operanda = b
+=adds right operand to the left operand and assign the result to lefta+=b is same as a=a+b
-=subtracts right operand from the left operand and assign the result to left operanda-=b is same as a=a-b
*=mutiply left operand with the right operand and assign the result to left operanda*=b is same as a=a*b
/=divides left operand with the right operand and assign the result to left operanda/=b is same as a=a/b
%=calculate modulus using two operands and assign the result to left operanda%=b is same as a=a%b

Example:

Lets create an example to understand use of assignment operators. All assignment operators have right to left associativity.


class Assignment_operators1{
public static void main(String as[])
{
    int a = 30;
    int b = 10;
    int c = 0;

      c = a + b;
System.out.println("c = a + b = " + c );

      c += a ;
System.out.println("c += a  = " + c );

      c -= a ;
System.out.println("c -= a = " + c );

      c *= a ;
System.out.println("c *= a = " + c );

      a = 20;
      c = 25;
      c /= a ;
System.out.println("c /= a = " + c );

      a = 20;
      c = 25;
      c %= a ;
System.out.println("c %= a  = " + c );

      c <<= 2 ;
System.out.println("c <<= 2 = " + c );

      c >>= 2 ;
System.out.println("c >>= 2 = " + c );

      c >>= 2 ;
System.out.println("c >>= 2 = " + c );

      c &= a ;
System.out.println("c &= a  = " + c );

      c ^= a ;
System.out.println("c ^= a   = " + c );

      c |= a ;
System.out.println("c |= a   = " + c );
    }
}
    

Misc. operator

There are few other operator supported by java language.

Conditional operator

It is also known as ternary operator because it works with three operands. And It is short alternate of if-else statement. It can be used to evaluate Boolean expression and return either true or false value

epr1 ? expr2 : expr3

Example:

In ternary operator, if epr1 is true then expression evaluates after question mark (?) else evaluates after colon (:). See the below example.


class Conditional_operators1{
public static void main(String as[])
{
int a, b;
      a = 20;
      b = (a == 1) ? 30: 40;
System.out.println( "Value of b is : " +  b );

      b = (a == 20) ? 30: 40;
System.out.println( "Value of b is : " + b );
}
}
    

instanceOf operator

It is a java keyword and used to test whether the given reference belongs to provided type or not. Type can be a class or interface. It returns either true or false.

Example:

Here, we created a string reference variable that stores “onlineexamguide”. Since it stores string value so we test it using is instance operator to check whether it belongs to string class or not. See the below example.


class instanceof_operators1{
public static void main(String as[])
{
      String a = "onlineexamguide";
boolean b = a instanceof String;
System.out.println( b );
}
}
    

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.