Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

Arithmetic operators are fundamental in programming and mathematics, used to perform various mathematical operations. Here are the main arithmetic operators along with detailed descriptions and examples:

1. Addition (+)

  • Description: Adds two operands.

Example:

				
					result = 5 + 3  # result is 8

				
			

2. Subtraction (-)

  • Description: Subtracts the second operand from the first.

Example:

				
					result = 5 - 3  # result is 2

				
			

3. Multiplication (*)

  • Description: Multiplies two operands.

Example:

				
					result = 5 * 3  # result is 15

				
			

4. Division (/)

  • Description: Divides the first operand by the second. The result is a floating-point number (even if both operands are integers).

Example:

				
					result = 5 / 2  # result is 2.5

				
			

5. Floor Division (//)

  • Description: Divides the first operand by the second, and then rounds down to the nearest integer.

Example:

				
					result = 5 // 2  # result is 2

				
			

6. Modulus (%)

  • Description: Divides the first operand by the second and returns the remainder.

Example:

				
					result = 5 % 2  # result is 1

				
			

7. Exponentiation ()**

  • Description: Raises the first operand to the power of the second operand.

Example:

				
					result = 5 ** 3  # result is 125

				
			

Detailed Examples and Usage

1. Addition (+)

  • Used to sum numbers.
  • In context: Calculating the total price of items in a shopping cart.
				
					item1_price = 10.5
item2_price = 15.3
total_price = item1_price + item2_price  # total_price is 25.8

				
			

2. Subtraction (-)

  • Used to find the difference between numbers.
  • In context: Calculating the remaining balance after a withdrawal from a bank account
				
					balance = 100.0
withdrawal = 40.0
remaining_balance = balance - withdrawal  # remaining_balance is 60.0

				
			

3. Multiplication (*)

  • Used to find the product of numbers.
  • In context: Calculating the area of a rectangle.
				
					length = 5
width = 3
area = length * width  # area is 15

				
			

4. Division (/)

  • Used to divide numbers.
  • In context: Calculating the average score of a test.
				
					total_score = 85
number_of_tests = 2
average_score = total_score / number_of_tests  # average_score is 42.5

				
			

5. Floor Division (//)

  • Used when you need an integer result.
  • In context: Distributing items equally among people and finding out how many each person gets.
     
				
					total_items = 10
number_of_people = 3
items_per_person = total_items // number_of_people  # items_per_person is 3

				
			

6. Modulus (%)

  • Used to find the remainder.
  • In context: Determining if a number is even or odd.
				
					number = 7
remainder = number % 2  # remainder is 1, so the number is odd

				
			

7. Exponentiation ()**

  • Used to raise a number to a power.
  • In context: Calculating compound interest in finance.
				
					principal = 1000
rate = 0.05
time = 2
amount = principal * (1 + rate) ** time  # amount is 1102.5

				
			

Operator Precedence

  • When multiple arithmetic operations are used in a single expression, the order of operations follows the standard mathematical precedence:

    1. Parentheses ()
    2. Exponentiation **
    3. Multiplication *, Division /, and Floor Division //
    4. Addition + and Subtraction -
Example of Operator Precedence
				
					result = 5 + 2 * 3 ** 2 - 8 / 4
# According to precedence:
# 3 ** 2 = 9
# 2 * 9 = 18
# 8 / 4 = 2
# 5 + 18 - 2 = 21
# So, result is 21

				
			

Understanding and correctly using arithmetic operators is fundamental in programming and problem-solving across various domains.

Scroll to Top