Sunday, November 11, 2012

Get benefit from "? :" Ternary (shorthand for if-then-else statement) operator !!!!!


Here I am going to  write simple code of if else condition.

_______________________________________________


public class Newoperator {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int x =1;
int y=2;

if(x<y){
x =1;
}else{
x =0;
}
System.out.println(x);
}

}   
______________________________________________

Ooutput:

1
_____________________________________________

Now using new operator that is "?" and ":"  
__________________________________________________

 public class Newoperator {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int x =1;
int y=2;


                x = (x<y) ? 1 : 0;

System.out.println(x);
}
}

_________________________________________________

Output:
1
_______________________________________________

What do you think, what magic happen with this new code, this is all about using ? and :

You can apply same in your regular code to reduce your compile time and make your software efficient.

Try on your own and see the difference................................

No comments:

Post a Comment