Another Basic Example
1. Open the editor, and type the following code
2. Compile the program using the command “javac prog2.java”
3. Once it is compiled use the “java prog2” statement to run it.
4. The output “Sum is 30” is displayed on the screen.
How does it work ?
Don't forget
1. Open the editor, and type the following code
class prog2
{
public static void main(String args[])
{public static void main(String args[])
int a,b,c;
a=10;
b=20;
c=a+b;
System.out.println(“Sum is “ + c);
}
}
2. Compile the program using the command “javac prog2.java”
3. Once it is compiled use the “java prog2” statement to run it.
4. The output “Sum is 30” is displayed on the screen.
How does it work ?
The program declares 3 integer variables namely a,b and c. The values of a and b are initialized to 10 and 20 respectively. The value of “c” is calculated as “a+b” and the result is printed using the statement “System.out.println()”. In the statement – “Sum is + c”, the “+” is working as a concatenation operator not as an arithmetic addition operator.
Don't forget
- To open the command prompt to compile and run the program.
- To name the program file as " prog2.java"(just in this example), because the name of the program and class should be the same.
0 comments:
Post a Comment