[Go to site: main page, start]

0% found this document useful (0 votes)
10 views16 pages

Java Arithmetic Operators Explained

Uploaded by

hansmalacad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

Java Arithmetic Operators Explained

Uploaded by

hansmalacad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA

ARITHMETIC
OPERATORS
Your Steps into Coding
Welcome programmers! Today, we're diving into the
exciting world of Java to learn about something
fundamental yet powerful: arithmeticoperators. Think
of these as the basic tools in yourcoding toolbox,
allowingyou to perform simple maths and
calculations. Don't worry, we'll keep it super easy to
understand, with clear explanations and plenty of
examples.

Mastering these operators is a crucial step in your


Java journey. Its not just about numbers; it's about
building a strong foundation for more complex
programming tasks later on. Let's get started and
make some Java magic happen!
What Are Arithmetic Operators?
In Java, just like in everyday maths, arithmetic operators are
symbols that tell the computer to perform mathematical
calculations. They allow you to add numbers, subtract them,
multiply them, divide them, and even find remainders. These
operators are the backbone of any numerical operation you'll
want to perform in your programmes. Understanding these
operators is key to writing effective code, whether you're building
a calculator app or managing data in a game. They’re simple to
use but incredibly versatile, forming the basis of almost every
programme that deals with numbers.
THE BIG FIVE:
YOUR ESSENTIAL
OPERATORS
Let's meet the main arithmetic operators you'll be using in [Link] are five primary
ones, and they’re likely already familiar to you from school maths. We'll go through
each one, explaining what it does and how to use it in your code.

Addition (+) Subtraction (-) Multiplication (*)


Adds two numbers Subtracts one number from Multiplies two numbers.
together. another.

Division (/) Modulus (%)


Divides one number by another. Returns the remainder of a division.
Addition (+) and Subtraction (-)

The Addition Operator (+) TheSubtraction Operator


The plus sign is used for addition. It's just like you'd (-)
expect: it adds two numbers together. The minus signisfor subtraction. It takes one number
Simple! away from another. Again, very straight forward.

int sum = 2 + 2; // sum will be 4 int difference = 15 - 10; // difference will be 5

These two operators are your starting point for any numerical manipulation in Java. They're intuitive and work
exactly as you'd imagine from your school days!
Multiplication (*) and Division (/)

Multiplication (*) Division (/)


In Java,we use an asterisk (*) for multiplication. For division, we use a forward slash (/). This
It multiplies two numbers. Remember, it's not divides one number by another. Watch out for
an 'x'! whole numbers!

int product = 4 * 3; // product will be 12


int quotient = 10 / 2; // quotient will be 5

int tricky = 10 / 3; // tricky will be 3 (integer


division!)

A quick note on division: if you divide two whole numbers (integers), Java will give you a whole
number as the result, ignoring any decimal part. For example, 10 / 3 equals 3, not 3.33! If you want
decimal results, you'll need to use 'double' or 'float' numbers, which we'll cover later.
THE MODULUS
OPERATOR (%):
FINDING REMAINDERS
The modul us operator, represented by the percent
sign(%), is a bit different but superuseful! It doesn't
give you the result of a division but rather the
remainder after division. Think about dividing 7
by 3. You get 2, with 1 left over. That '1' is what
the modulus operator gives you.

int remainder = 7 % 3; // remainder will be 1

The modulus operator is great for tasks like checking if a number is even or odd (an even
number divided by 2 will have a remainder of 0) or for cyclical operations.
PUTTING IT ALL
TOGETHER:
A SIMPLE EXAMPLE
Putting It All Together: A Simple Example
Let's see these operatorsin action with a short Java [Link] example will calculate
the area of a rectanglenand the perimeter, showcasing addition and multiplication.

Code Example

Code Output:

Notice how we used both multiplication (*) and addition (+) to get our results. This demonstrates how
operators combine to perform more complex calculations. You're already writing Java code that does real
work!
OPERATOR PRECEDENCE:
THE ORDER
OF OPERATIONS
Just like in maths, Java has rules about which operators get calculated first.
This is called "operator precedence". If you don't follow these rules, your
programm might give you unexpected results!

Parentheses () Multiplication (*),


Calculated first. Use them to Division (/), Modulus (%)

force specific order. Calculated next, from left to right.

Assignment (=) Addition (+), Subtraction (-)

The very last step. Calculated last, from left to right.

A handy trick: if you're ever unsure about the order, use parentheses () to make your
intentions clear. Whatever is inside the parentheses will always be calculated first!
PRACTICE MAKES
PERFECT!
The best way to get good at using arithmetic operators is to practice!
Try these small challenges in your Java environment:

Calculate Your Age in Split the Bill Convert Celsius to


Months Fahrenheit
If you are 25 years old, write If a $50 meal is split among 3 The formula is F = C * 9 / 5 +
a programme to calculate friends, how much does each
32. Convert 20 degrees
how many months you have person pay? What is the
lived. remainder? Celsius.

Don't be afraid to experiment and make mistakes 3 that's how you learn! Each

challenge will solidify your understanding of these fundamental operators.


Your Step Towards Java
Mastery.

Congratulations! You've just taken your significant step


into Java programming by understanding arithmetic
operators. You now know how to perform basic
calculations, which is an absolutely essential skill for any
programmer. Remember, coding is like learning a new
language. You start with basic words and grammar, and
then you build up to more complex sentences and stories.
The +, -, *, /, and % operators are some of your very first
words in the Java language. Keep practising, keep
exploring, and you'll be amazed at what you can create!
Happy coding!

You might also like