Skip to main content

Java Operator Tutorial

 JAVA OPERATOR  : Operator are among core concept of writing logical condition and business logic in any programming language .

Simplest operators are Arithmetic Operator which we all know : 

+   : Addition
   : Subtraction 
*   : Multiplication 
   : Division 
% : Remainder

Now next is Assignment operator

 = : To assign any value to any reference name of any type we use this = operator . What is variable in java already explained in my previous post JAVA BASIC OVERVIEW

Example:  int input1=10;  String name = "My name is John Cena";

Along with this operator we can add Arithmetic Operator  to make assignment  simple 

Example : For int input1= input1 + 10 ; it can be simplified by using combination of Addition and equal to operator like  int input1 += 10;  this is same as input1= input1 + 10 ;

  Same way we can simplified for Subtraction, Multiplication , Division and Remainder like :

-= , *= , /= , %=

Relational Operator , mainly used in case of comparison , if comparison is successful true will be return if not false will be return.

>   : To Compare other value is greater than the comparing value or not.

<   : To Compare other value is less than the comparing value or not.

== : To compare two objects if equal or not.

!=  : To Compare other value is not equal to comparing value.

>= : To Compare other value is equal or greater than the comparing value. 

<= : To Compare other value is equal or less than the comparing value.

Logical operator  :

& : & is use for applying  multiple condition together with AND condition.

: | is use for applying  multiple condition together with OR condition.

&& : && is use for applying  multiple condition together with AND condition. Only difference & and && is in case of && if first condition is failed it won't check the next condition while in & all the condition will be check.

|| : || is use for applying  multiple condition together with OR condition. Only difference | and || is in case of || if first condition is failed it won't check the next condition while in | all the condition will be check.

++ : To do pre or post increment on any value.

-- : To do pre or post decrement on any value.

Ternary Operator 

?    :   

Example :    10 > 5 ? "YES" : "NO"; 

 Output will be "Yes" as 10 is greater than 5.

                10 > 50 ? "YES" : "NO"; 

Output will be "NO" as 10 is less than 50.


  1. BASIC OVERVIEW OF JAVA (SESSION 1)
  2. PRIMITIVE TYPES AND WRAPPER CLASS (SESSION 2)
  3. JAVA OPERATORS (SESSION 3)
  4. LOOPS IN JAVA (SESSION 4)
  5. NAMING CONVENTION WITH FACTORIAL PROGRAM (SESSION 5)


Comments

Popular posts from this blog

OOPS Concept in Java : POLYMORPHISM

 POLYMORPHISM IN JAVA :  Polymorphism means mutiple forms of Single reference. And to understand in simple way just take an example from my previous post of OOPS CONCEPT IN JAVA : INHERITANCE  , I request you guys to go through this link before proceding here. So in this post I have created a method called sum() in PARENT class which has been used by CHILD class without writing same sum() method in CHILD class. This was possible becuase of INHERITANCE concept in java.  But what if CHILD class is not satisfied with PARENT sum() and CHILD class wants to improve it like by adding some message before the calculation. To do this we have another OOPS CONCEPT IN JAVA i.e POLYMORPHISM and by applying this logic we can make same sum() method behvae differently based on Object. As I mentioned earlier POLYMORPHISM means different forms and same has been achieved here by calling same sum() method but the output is different based on Object on which it has been called. If ...

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA : Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1)  and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.    Now To represent properties and behaviour in java , there are some standard naming conventions we should follow. Class name should always starts with Uppercase letter like class Student { //Code to be executed } Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ; //Code to be executed } Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; Str...

Java Overview : Basic Java Overview

  JAVA BASIC OVERVIEW :  Java is high-level, class-based, robust, platform independent and object oriented programming language. Here each keyword has specific meaning which we need to understand.     How java internally works and execute our logic ?    We will need to write a file with extension .java, in which we will be writing our logics to be executed and this should be inside a class because java starts from main method of any class which is public inside the file named same as class name.    Example :  public class FirstJavaProgram {      public static void main(String[] args) {      //our logics      } }   This code should be saved in a file named as  FirstJavaProgram.java Whenever we are writing a java class, it's basically a blue print of what class has and it can do, it means characteristics  and behaviour , just like a simple real l...