Operators are used to perform operations on values. The concept of operators is very simple. First, we are studying mathematical operators. Following mathematical operators are available in java:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulo (%)
I won’t tell you what addition is.
Same as above.
Multiplication and Division:
The modulo operator displays the remainder when a number is divided by another number. For example, 5 % 3 will be equal to 2 because when 5 is divided by 3, the remainder is 2. Similarly, 223434 % 2 is equal to 0 because when 223434 is divided by 2, the remainder is zero.Similarly, 3 % 5 will be equal to 3 because when 3 is divided by 5, the remainder is 3 itself. Examples are given below:
You can also use brackets in java, They function just same as they did in mathematics.
Consider the following program that calculates and display the area and perimeter of a rectangle with length=50 and width=30
This equal sign (=) is called the assignment operator in programming. It is called assignment operator because it assigns a value to a variable. For example, int a=5; in this example the = is assigning the value 5 to a. There is no need of example because you have already used it in the above examples.
This is used to join two Strings. + is the concatenation operator. Let’s take an example:
It can also be used to join numbers with Strings:
Let’s consider another example to calculate the area of a circle with radius 5:
In the above example, you found the use of double. We could not store 3.1415 and 70.5375 in int, so we used double because double can store decimal values.
In this lesson, you learnt about mathematical operators, concatenation operator and the assignment operator. There are many more operators available in java. We will learn about them in the upcoming lessons.
- Write a program to calculate and display the volume and surface area of a cube whose one side is equal to 2cm.
- Write a program to calculate and display the surface area and volume of a cylinder with diameter 6cm and height 5cm
*You can find the mathematical formulas for the above two on google. Just Google “What is the formula for…..