Java Conditional Operators

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.


Screenshots of Conditional Operator Java Program

Simple Java Program Conditional operators:

//Simple Java Program  Conditional Operator

public class ConditionalOp {

    public static void main(String[] args){
        int num1 = 20;
        int num2 = 10;
        if((num1 == 20) && (num2 == 10))
            System.out.println("num1 is 20  AND  num2 is 10");
        if((num1 == 20) || (num2 == 10))
            System.out.println("num1 is 20  OR  num2 is 10");

Output Here In cmd Window

Another conditional operator is '?:', which can be thought of as shorthand for an if-then-else statement This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true, assign the value of num1 to result. Otherwise, assign the value of num2 to result."

See The Simple Java Conditional Operators Program

//simple java program of ConditionalOp with using Scanner class

import java.util.Scanner; 
 public class ConditionalOprtr {
   public static void main(String args[]){
  //create Scanner object to obtain input from keyboard
     Scanner input=new Scanner(;
     System.out.print("Enter the Three Number : ");  //input from Keyboard
     int  num1=input.nextInt();   //Read First number
     int  num2=input.nextInt();   //Read Second number
     int  num3=input.nextInt();   //Read third number
     int result = (num1>num2)? ((num1>num3)?num1:num3) : ((num2>num3)?num2:num3);
     System.out.println( "Greater Number is :="  +result);


Download Source code 'Java Conditional Operators Program'

Output Here In cmd Window

Java Operators
Arithmetic Operators
Assignment Operator
Bitwise Operators
instanceof Operator
Relational Operators
Unary Operators
Conditional Operators

References : oracle

share on :        :  

Love to hear your Views / Guidance / Recommendations on this Post…

Explore the Technology World

Computer Programming Terminologies Computer Programming Terminology is a general word for the group of specialized words or meanings relating to Computer Programming, and also the study of such t ...Read MoreNew features in JDK 12 with IntelliJ IDEADeveloped by JetBrains (formerly known as IntelliJ) ,IntelliJ IDEA is a Java integrated development environment (IDE) for developing computer software. It is av ...Read MoreGoogle Pay API Implementation Demo for AndroidIn this youtube Channel by Android Developers ,Learn how to implement the Google Pay API for Android with just a few lines of code and how it works with your ex ...Read MoreUnit Testing in .NET Core and Azure Devops pipelineUnit testing is a software testing method by which individual units of source code, usage procedures,sets of one or more computer program modules together with ...Read MoreThe mind behind Linux & GIT : Linus TorvaldsLinus Torvalds transformed technology twice — first with the Linux kernel, which helps power the Internet, and again with Git, the source code management ...Read MoreTensorflow and deep learningWith Tensorflow ,deep machine learning transitions from an area of research into mainstream software engineering. ...Read MoreJava program to return all possible permutations from collection of distinct numbersexample of Java program to return all possible permutations from collection of distinct numbers ...Read MoreReal Time Analytics with Spring Application Development FrameworkSpring is the most popular application development framework for enterprise Java.Millions of developers use Spring to build simple, truly portable, fast and fle ...Read MorePath Setting in Java When an application is launched from command prompt Windows OS searches for the executable program in the current working directory. The prompt throws an error ...Read MoreFacebook | whatsapp|Github | Twitter | LinkedIn | Youtube| reddit About | Cookies-Policy | Disclaimer | Site Map | Contact

Call / Visit for New Batch


Why Join Us

  • ➯ Free Demo Classes
  • ➯ No Registration Fee
  • ➯ Interview Questions
  • ➯ Study Materials
  • ➯ Softwares
  • ➯ Aptitude & Reasoning
  • ➯ Placement Assitance


  • MyJava Training Institute ,
  • Plot No.243 , shop No 14 ,
  • Secnd Floor , Zone-II ,
  • M.P.Nagar , Bhopal,
  • Madhya Pradesh ( 462011 )
  • ☎  08871209400 , 7987028543
  • 🌐
FacebookGoogle +TwitterYouTube

MyJava - java Training Institute