Sum of Inputs
This is a program to learn different ways of getting the desired console output
Learning Objectives
To learn java concepts related to :
- understanding simple calculations
- introduction to variables
- different ways of passing argument to println()method.
Program Approach
The objective is to demonstrate single argument passing, doing arithmetic calculations while passing the argument and concatenation of two different data types in print or println methods
We will be firstly calculating the sum and storing it in a variable.Then we will be passing that variable as an argument in the println() method.In the next phase we will be doind addition inside the println() method only.Then we will be using the + sign to concatenate two different data types and displaying them on the screen
In the above program the variables a, b and c have been used to store integer values.
System.out.println(c);
In this statement we have passed an integer value as an argument in the println() method. println() is a method where we are allowed to pass different kinds of data types such as String, int, float, double, char, boolean etc. Such methods are also known as overloaded methods.
System.out.println(a+b);
In this statement we have calculated the sum of the values stored in the variables a and b and we have passed it directly as an argument in the println() method.
System.out.println(“Sum of Inputs :”+(a+b));
In this statement we have calculated the sum of the values stored in the variables a and b. Then we have passed it directly as an argument in the println() method and we have also concatenated the sum which will be an integer type to a message which will be string type. The expression a+b must be enclosed within round brackets. This allows integer summation before concatenation with the message.
Source Code
Run Output
Code Understanding
System.out.println(c);
Suggested Filename(s): SumOfInputs
admin| Created: 16-Aug-2016 | Updated: 8-Dec-2017|