Program to demonstrate basic assignment operator usage |
class AssignmentOperator { public static void main(String args[]) { int a; a=5; //Here we are using assignment operator '=' to assign value '5' to variable 'a' System.out.println(a); } } Output of this program: 5 |
Program to demonstrate assignment operator assigning a single value to more than one variable in a single statement |
class AssignmentOperator2 { public static void main(String args[]) { int a,b,c; a=b=c=5; //Here we are using assignment operator '=' to assign value '5' to variables 'a','b' and 'c' in a single statement System.out.println(a); System.out.println(b); System.out.println(c); } } 5 5 5 |
Program to demonstrate '?' operator |
class QuestionOperator { public static void main(String args[]) { int i=10; int k= i<0 ? -i: i; // i<0 is true then -i after the question mark will be assigned to variable 'k' else value of i will be assigned System.out.println("k="+k); } } Output of this program: k=10 |
Another Program to demonstrate the '?' operator |
class QuestionOperator { public static void main(String args[]) { int i=-10; int k= i<0 ? -i: i; // i<0 is true then -i after the question mark will be assigned to variable 'k' else value of i will be assigned System.out.println("k="+k); } } Output of this program: k=10 |