Variables and Constant Implementation
Now we are aware of the variables and constant. Now lets have a look in real Java code.
Follow the steps:
The output will be
Now try to change the value of the constant you find error. The final create some special effect to tell the compiler it is a constant.
- Create a new project in Eclipse
- Named it "VariableAndConstant"
- Create a Class
- Enter the code in this class
-
public class Main { public static void main(String []args){ int var = 3; System.out.println(var); var = 5; System.out.println(var); System.out.println(var); } } - right click on project and run it.
The output should be
3
5
5
It is all because you set 3 in beginning and then you change it as 5 and use it two times.
Now it is your turn to change the variable and print. Create another and print it.
Now we are going to write second program for the constant tricks.
Create another project and create a class Main in it.
Write the following code.
Now it is your turn to change the variable and print. Create another and print it.
Now we are going to write second program for the constant tricks.
Create another project and create a class Main in it.
Write the following code.
public class Main {
public static void main(String []args){
final int constant = 17;
System.out.println(constant);
System.out.println(constant);
System.out.println(constant);
}
}
The output will be
17
17
17
Now try to change the value of the constant you find error. The final create some special effect to tell the compiler it is a constant.
No comments:
Post a Comment