The Artima Developer Community
Sponsored Link

Java Buzz Forum
Ternary operator in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Ternary operator in java Posted: Apr 27, 2016 3:58 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Ternary operator in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Ternary operator also known as conditional operator in java.
  • Ternary operator used to evaluate boolean expression and it consists of three operands.




Ternary operator in java syntax:

  1. variable x = (expression) ? value if true : value if false

  • Value of x will be decided based on the condition. if condition is true then it assigns first value to the variable
  • If Expression is false then it assigns second value to the variable.

Java simple example program on ternary operator / conditional operator in java


  1. package operatorsinjava;
  2. public class TernaryOperator {
  3.  
  4.     /**
  5.      * @ www.instanceofjava.com
  6.      * 
  7.      */
  8.  public static void main(String[] args) {
  9.         
  10.  // checking condition 2>3 and if true assign 3 else 3
  11.   int x= (2>3)?3:2;
  12.         
  13.   System.out.println(x);
  14.         
  15.  // checking condition 5<4 if true assign 4 else 3
  16.  int y= (5<4)?4:3;
  17.         
  18.  System.out.println(y);
  19.  
  20. }
  21.  
  22. }
Output:

  1. 2
  2. 3

Ternary operator in java for boolean:

Java simple example program on ternary operator / conditional operator for boolean

  1. package operatorsinjava;
  2. public class TernaryOperator {
  3.  
  4.     /**
  5.      * @ www.instanceofjava.com
  6.      * 
  7.      */
  8.  public static void main(String[] args) {
  9.         
  10.  
  11.  boolean x= true ? true: false;
  12.         
  13.  System.out.println(x);
  14.         
  15.  boolean y= false ? true : false;
  16.             
  17.  System.out.println(y);
  18.         
  19.  boolean z= false ? false : true;
  20.         
  21.  System.out.println(z);
  22.  
  23. }
  24.  
  25. }
Output:

  1. true
  2. false
  3. true

Ternary operator in java for Strings:

Java simple example program on ternary operator / conditional operator for String

  1. package operatorsinjava;
  2. public class TernaryOperatorForStrings{
  3.  
  4.     /**
  5.      * @ www.instanceofjava.com
  6.      * 
  7.      */
  8. public static void main(String[] args) {
  9.         
  10.  String str= "java".equals("java") ? "Java Questions" : "Java Programs";
  11.         
  12.  System.out.println(str);
  13.         
  14.  String str1= (1==3)? "its true" : "its false";
  15.         
  16.  System.out.println(str1);
  17.        
  18. }
  19.  
  20. }
Output:

  1. Java Questions
  2. its false

Ternary operator in java for null;

Java simple example program on ternary operator / conditional operator for null check

  1. package operatorsinjava;
  2. public class TernaryOperatorForStrings{
  3.  
  4.     /**
  5.      * @ www.instanceofjava.com
  6.      * 
  7.      */
  8. public static void main(String[] args) {
  9.         
  10. String str= null;
  11.  
  12. String str1= (str==null) ? "its true" : "its false";
  13.  
  14. System.out.println(str1);
  15.         
  16. String str2= (str1!=null) ? "its true" : "its false";
  17.  
  18. System.out.println(str2);

  19. }
  20.  
  21. }
Output:

  1. its true
  2. its true

ternary operator in java


    Read: Ternary operator in java

    Topic: Java Tutorial : Java StringBuilder [append(StringBuffer sb) method] Previous Topic   Next Topic Topic: Jetty Runner Example

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use