Page 29 - CTS - CSA TP - Volume 2
P. 29
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 82 : Use various operators in JAVA
Objectives
At the end of this exercise you shall be able to
• know the use of various Operators in Java
• develop Java Programs using different operators
• compile , Execute and verify the result of the Java Programs.
Requirements
Tools/Materials
• PC/Laptop with Windows OS
• JDK Software
• Text Editor (Visual Studio/Sublime/Notepad)
Procedure
TASK 1: Java Unary Operator Example: ++ and –
CODE:
public class OperatorExample1{
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}
Explanation:
1 Variable Initialization:
• The program begins by initializing an integer variable x with the value 10.
2 Post-increment (x++):
• System.out.println(x++); is a post-increment operation. It prints the current value of x (which is 10) and then
increments x by 1.
• The output is 10 because the current value is printed before the increment.
3 Pre-increment (++x):
• System.out.println(++x); is a pre-increment operation. It increments the value of x by 1 and then prints the
updated value.
• The output is 12 because x was incremented in the previous step.
4 Post-decrement (x--):
• System.out.println(x--); is a post-decrement operation. It prints the current value of x (which is 12) and then
decrements x by 1.
• The output is 12 because the current value is printed before the decrement.
14