Unary Operators can be simply defined as an operator that takes only one operand and does a plain simple job of either incrementing or decrementing the value by one. Added, Unary operators also perform Negating operations for expression, and the value of the boolean can be inverted.
This Java program demonstrates the usage of unary operators (++ and --) in Java. In this program, we first declare an integer variable a and assign it the value of 10. We then print the value of a using System.out.println() method. Next, we use the post-increment operator (a++) to increment the value of a by 1 after it is used in the statement. We then print the value of a again, which is 10 because the post-increment operator returns the value of a before incrementing it.
We then print the value of a again, which is now 11 because the value of a was incremented by the post-increment operator in the previous statement. Finally, we use the pre-increment operator (++a) to increment the value of a by 1 before it is used in the statement. We print the value of a again, which is now 12 because the pre-increment operator increments the value of a before it is used in the statement.
public class Unary { public static void main(String args[]) { //Unary Operators in Java ++ -- int a=10; System.out.println(a); //a++; //a=a+1 System.out.println(a++); System.out.println(a); System.out.println(++a); } }
10 10 11 12To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions