What is ?: this operator called in java ? how dose it function?

0 votes
I have never come across ?: operator in java. If anybody can help?
Jun 11, 2018 in Java by Daisy
• 8,090 points
163 views

1 answer to this question.

0 votes

? :  is the Conditional operator and is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable.

Syntax:

variable y = (expression) ? value if true : value if false
Example:
public class Test 
{

   public static void main(String args[])    
   {
      int x, y;
      x = 10;
      y = (x == 1) ? 20: 30;
      System.out.println( "Value of y is : " +  y );

      y = (x == 10) ? 20: 30;
      System.out.println( "Value of y is : " + y );
   }
}
answered Jun 11, 2018 by sophia
• 1,400 points

Related Questions In Java

0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,090 points
1,416 views
0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14, 2019 in Java by Sirajul
• 55,620 points
334 views
0 votes
1 answer

What is the 'instanceof' operator used for in Java?

It's an operator that returns true if ...READ MORE

answered May 23, 2018 in Java by Rishabh
• 3,580 points
142 views
0 votes
3 answers

What is ^ operator in java?

It's bitwise XOR, Java does not have ...READ MORE

answered Oct 31, 2018 in Java by Sushmita
• 6,890 points
67 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,420 points

edited Nov 6, 2018 by Omkar 27,016 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,510 points
3,998 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
9,558 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
1,726 views
0 votes
1 answer

Is it possible to run a java program from command line on windows?How?

  Let's say your file is in C:\myprogram\ Run ...READ MORE

answered Apr 18, 2018 in Java by sophia
• 1,400 points
719 views
0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
184 views