Topics Index

Print variable value - Java Example Program

Print variable value
class PrintVariableValue
    public static void main(String arg[])
        int numberOfStudents = 34; // LINE A
        int highestMarks = 98;
        System.out.println("The class has " + numberOfStudents + " students and the highest marks are " + highestMarks + ".");    

The class has 34 students and the highest marks are 98.


Here we declared variables numberOfStudents and highestMarks. Both of them are of type int. We have concatenated varialables and strings to print the formatted output.

  • Change the numberOfStudents to 56 and highestMarks to 25 and observe the output.
  • Add one more double variable averageMarks and set it to 53.2. Print the average marks in the format "Average marks are 53.2".
  • In LINE A, change the variable name from numberOfStudents to numberofstudents (change O and S from capitals to small letters) but do not change it in the print statement. Observe the compilation error you get. Note that the variable names are case-sensitive.
  • Try declaring the variables with names like 3idiots, v@r## and number of students and see what compilation errors you get.

© meritcampus 2019

All Rights Reserved.

Open In App