alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Java
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Java - Thing to know before proceed - Operator Tutorial

 An operator is used to perform operations between two operands. Operands can be variables or values and Operator can be +, -, *, / etc.

There are 8 types of an operator in java

  • Arithmetic Operator,

  • Relational Operator,

  • Shift Operator,

  • Logical Operator,

  • Unary Operator,

  • Ternary Operator

  • Bitwise Operator,

  • Assignment Operator.

 

  1. Arithmetic Operator

  • It is used to perform basic mathematics operation i.e addition, subtraction, division, multiplication, and modulus

public class ArithmeticOperator{

public static void main(String args[]){

int x=3;
int y=2;
System.out.println(x+y);//5
System.out.println(x-y);//1
System.out.println(x*y);//6
System.out.println(x/y);//1
System.out.println(x%y);//1
}
}

Run Program

  1. Relational Operator

  • It is used to compare the value of two operands and return the result in the form of the booleans - True or False.

  • Comparison Operator- <, >, <=, >=, instanceof

  • Equality Operator- ==, !=

    public class RelationalOperatorExample{
    
    public static void main(String args[]){
    
    int x=7,y=3;
    
    System.out.println(x==y); //false
    System.out.println(x!=y); //True
    System.out.println(x<=y); //false
    System.out.println(x>=y); //true
    System.out.println(x>y);  //true
    System.out.println(x<y);  //false
    
    }}

    Run Program

  1. Shift Operator

  • Left Shift Operator

left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times.

public class LeftShiftOperatorExample{

public static void main(String args[]){
System.out.println(2<<2);//2*2^2=2*4=40
System.out.println(2<<3);//2*2^3=2*8=80
System.out.println(3<<2);//3*2^2=3*4=80
}}

Run Program

  • Right Shift Operator

Right shift operator >> is used to shift all of the bits in a value to the right side of a specified number of times.

public class RightShiftOperatorExample{

public static void main(String args[]){
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
System.out.println(30>>2);//30/2^2=30/4=7

}}

Run Program

 

  1. Logical Operator

a) && Operator

  • In logical && Operator, If anyone condition is false the output will be false.

  • Logical && Operator doesn’t check the second condition if the first condition is false

  • Logical && Operator only check the second condition, if the first condition is true.
    public class LogicalAndOperatorExample{
    
    public static void main(String args[]){
    
    int x=15;
    int y=25;
    int z=35;
    System.out.println(x<y&&x<z);
    //true && true =true
    System.out.println(x<y&&x>z);
    //true && false =false
    System.out.println(x>y&&x<z);
    //false && true =false
    System.out.println(x>y&&x>z);
    //false && false =false
    }}

    Run Program

b) || Operator

  • In logical || Operator, If anyone condition is true the output will be true.

  • Logical || Operator doesn’t check the second condition, if the first condition is true

  • Logical || Operator only check the second condition, if the first condition is false.

public class LogicalOperatorExample{

public static void main(String args[]){
int x=15;
int y=25;
int z=35;
System.out.println(x<y||x<z);
//true && true =true
System.out.println(x<y||x>z);
//true && false =true
System.out.println(x>y||x<z);
//false && true =true
System.out.println(x>y||x>z);
//false && false =false
}}

 

Run Program

  1. Unary Operator

  • It is used to perform incrementing/decrementing value by one using ++ and --.

public class UnaryOperator{

public static void main(String args[]){
int x=1;
System.out.println(x++);//1
//(p1-Divide x++, into x and ++,
//x will print first 1, then after printing it will get incremented by 1 making it 2)
System.out.println(++x);//3
//(p2-Divdie ++x, into ++ and x,
//++ will increment step-p1 value first making it 3, then x will print the value 3)
System.out.println(x--);//3
//(p3-Divide x--, into x and --, x will print first the previous step-p2 value,
//then after printing it will get decremented by 1 making it 2)
System.out.println(--x);//1
//(p4-Divide --x, into -- and x, -- will first decrement step-p3 value making it 1,
//then after it will print x vlaue i.e 1)
}}

Run Program

  • Making Value negative using ~

public class UnaryOperator{

public static void main(String args[]){
int x=-2;
int y=2;
System.out.println(~x);//1
//(-2,-1,0,1 So 1 will be output , negating of negative value start from 0)
System.out.println(~y);
//(2,1,0,-1,-2,-3 So 1 will be output , negating of positive value start from -1)
}}

Run Program

  • inverting the value of a Boolean using!

public class UnaryOperator{

public static void main(String args[]){
boolean x=true;
boolean y=false;
System.out.println(!x);//1
//(This will invert x true value to false)
System.out.println(!y);
//(This will invert y false value to true)
}}

Run Program

  1. Ternary Operator

The ternary operator is used as a one-liner replacement for the if-then-else statement.

public class TernaryOperator{

public static void main(String args[]){
int x=5;
int y=6;
int z=(x<y)?x:y;
System.out.println(z);
}}

Run Program

  1. Bitwise Operator

This operator will check both the condition

public class BitwiseAndOperator{

public static void main(String args[]){
int x=15;
int y=25;
int z=35;
System.out.println(x<y&x<z);
//true && true =true
System.out.println(x<y&x>z);
//true && false =false
System.out.println(x>y&x<z);
//false && true =false
System.out.println(x>y&x>z);
//false && false =false
}}

Run Program

public class BitwiseOrExample{

public static void main(String args[]){
int x=15;
int y=25;
int z=35;
System.out.println(x<y|x<z);
//true && true =true
System.out.println(x<y|x>z);
//true && false =true
System.out.println(x>y|x<z);
//false && true =true
System.out.println(x>y|x>z);
//false && false =false

}}

Run Program

  1. Assignment Operator

         

public class AssignmentOperator{

public static void main(String args[]){
int a=10;
int b=20;
a+=4;//a=a+4(a=10+4)
b-=4;//b=b-4(b=20-4)
System.out.println(a);
System.out.println(b);

}}

Run Program

 

Java

Java

  • Introduction
  • Installation and running a first java program
    • JDK Installation
    • Environment Variable Path Setup
    • Running a first java program
  • 1st Java Program Explanation
    • Program Syntax
  • JDK,JRE,JVM
    • JRE
    • JDK
    • JVM
  • Thing to know before proceed
    • Variable
    • Datatype
    • Operator
    • Keyword
    • Access Modifier
  • Decision Control and looping statement
    • If-else
    • Switch
    • For
    • While
    • do-while
    • break
    • continue
  • Object-Oriented Programming (OOPS) Concept
    • About OOPs
    • Object & Class
    • Inheritance
    • Polymorphism (Runtime & Compile Time)
    • Abstraction (Using abstract and interface)
    • Encapsulation

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.