Java Tutorials - Data Operations With Example.
Hello guys!! Welcome to our next tutorial on Java programming. In last tutorial we learned different data types in Java. Today we are going to see different data operators and how we can perform operations using these operators. So, let's start.
Data operations in Java :
We have mainly 4 different types of operators in Java as follows :
- Arithmetic operators
- Unary operators
- Relational operators
- Logical operators
Arithmetic operators:
To perform arithmetic operations, we have
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulus (%)
Addition, subtraction, multiplication and division are the operators we use generally but the Modulus is something you may not be familiar with if you are not from programming background. Basically, what modulus does is that it performs division but rather than giving the quotient what modulus gives you is remainder of that division.
For example: 5%3
Here, rather than getting 1 what you are going to be getting is 2 as the remainder value.
Unary operators:
• Increment operator (++)
• Decrement operator (--)
Let’s say there’s a specific value that you have initialized let’s say:
A=10
And somewhere in your program you want to increment it after a certain set of operations. So, what you are going to do is increment the value of A with the increment operations. We will not go deep here because we will be using the increment operator with the loop statements and I will show how it plays an essential role as a part of control statement.
Relational operators
• Less than (<)
• Less than or equal to (<=)
• Greater than (>)
• Greater than or equal to (>=)
• Not equal to (!=)
• Equals (==)
This is something you may be familiar with. We have seen this type of operators in math but with little bit of different symbols.
Logical operators
• And (&&)
• Or (II)
• Not (!)
Here, you are going to be working with different conditions. You are going to be using ‘And’ and ‘Or’ operations on different conditions. This works same as the truth tables of ‘And’, ‘Or’ and ‘Not’ gates in logic gates.
Let’s see an example:
Let us assume the following values for the variable:
- A=10
- B=15
- C=25
- D=true
After applying various operations on it :
(A+B)-C/5 = 20
A++ = 11
C>=A = false
(B<C)&&(A>C) = false
!D = false
Let's apply this in Java using an example.
Ramesh comes to your store to purchase following items :
Product | Quantity |
---|---|
Bread | 1 |
Coke | 2 |
Butter | 3 |
Costs of the products are given below :
Product | Cost |
---|---|
Bread | 150 |
Coke | 100 |
Butter | 400 |
When Ramesh came to your store there was 10% of discount on all product. And finally a service tax of 5% is applicable on total cost.
So, compute total cost using Java program.
Now let's go ahead and write a code to compute this value. We have 3 items that we are going to use them as variables. Since they don't have decimal value so we can make them integer. And for price we are going to use double. We are going to declare variables as well as initialize them with a corresponding a value. As you can see in the screenshot given below.
Now, let's write the expression for the total value . Always remember that the expression to he right hand side of assignment is computed first and then the corresponding value is stored in the variable to the left. So, below are the expressions to compute the final value.
Now run the code using right click anywhere on the code > run as > java application. As you can see we get the total value as the output.
So, that's enough for today. In our next tutorial we are going to see control statements in Java.
If you have any questions please comment below.
Important :
All the images used in this article are the screenshots taken in my phone and my pc and some of them are cropped using Paint.
References :
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
- Book : The Java programming language by Arnold.
Don't forget to upvote and resteem.
Follow @pratique007
This post has received a 1.26 % upvote from @boomerang thanks to: @pratique007
@boomerang $2.16 for $2.0 . that's not fair
Congratulations! This post has been upvoted from the communal account, @minnowsupport, by pratique007 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.
If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.