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

JAVA 8 FUNTIONAL INTERFACE

JAVA 8 FUNTIONAL INTERFACE  Funtional Interface :     An interface which has only one abstract method can be called a funtional interface. Comparable , Runnable , Callable all these interfaces has only one abstract method and can be consider as funtional interface.    How it works :        Once we will create a interface with one abstract method then java internally predicates the input type and based on the Interface reference it apply the logic mentioned after lambda expression    lets consider we have created an interface as below @FunctionalInterface public interface FuntionalExample { public int cal ( int a , int b) ; } And below class to test our funtional Interface. public class FuntionalInterfaceExample { public static void main (String args[]) { FuntionalExample addition=( int a , int b) -> a+b ; FuntionalExample subtraction=( int a , int b) -> a-b ; FuntionalExample multipl...

JAVA MEMORY LEAK, UTILISATION AND MONITORING USING JFR using Mission Control

JAVA MEMORY LEAK, UTILISATION AND MONITORING USING JFR using Mission Control Java flight recording(JFR) help us to analyse and find the root cause of any slowness in our program along with CPU usage , hot methods and garbage collection , profiling etc. To visualise we need to feed .jfr file to JDK mission control present in JDK bin folder. After successful compilation , we should run the program with below option which will generate the .jfr and feed to mission control.   command :  j ava -XX:+UnlockCommercialFeatures -XX:+FlightRecorder  -XX:StartFlightRecording=duration=200s,filename=flight.jfr -cp ./out/ path-and-class-name Below are some example to understand how this JFR can be helpful. 1. Lets consider we have created a java program in which we have used LinkList to store the elements and in same program we are using contains method inside a for loop of 1 million , in this case each time this contains method will be called then 1 million records will be sc...

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...