This blog is for everyone who want to learn Java as well as want quick refresh of Java. This is really easy manner to learn something in easy way, whole blog is explained in very simple language so that even the person who don't know anything about Java can understand it.
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................................
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment