Saturday, April 24, 2010

Java ternary operator  

0 comments

public class JavaTernaryOperator
{
public static void main(String[]args)
{
int a=0;
int b=4;
int c=5;

//THIS IS EXAMPLE OF TERNARY OPERATOR
a=(b>c?b:c);

System.out.println(a);
}
}


************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************

In the java code above, example of ternary operator is a=(b>c?b:c);.So, what it mean ?

This is the answer :
Value a is equal to b if b larger than c or value a is equal to c if b smaller than c.So, java code above has same meaning with java code below.

::Click Here To Download This Java Source Code::




public class JavaTernaryOperator
{
public static void main(String[]args)
{
int a=0;
int b=4;
int c=5;

if(b>c)
{
a=b;
}
else
{
a=c;
}

System.out.println(a);
}
}


Now, you can choose which one you want to use when write your java program. It seem when we use ternary operator we can make our program more shorter.

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories



0 comments: to “ Java ternary operator

Extension Factory Builder