🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, arithmetic operators are used to perform basic mathematical operations. These operators are essential for performing calculations and manipulating numerical data. Understanding how to use these operators is fundamental for any Java programmer.
Table of Contents
- What are Arithmetic Operators?
- Types of Arithmetic Operators in Java
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Division (
/) - Modulus (
%) - Increment (
++) - Decrement (
--)
- Addition (
- Conclusion
What are Arithmetic Operators?
Arithmetic operators are symbols that represent mathematical operations such as addition, subtraction, multiplication, division, and modulus. These operators are used to manipulate numerical data and perform calculations.
Types of Arithmetic Operators in Java
Java provides several arithmetic operators:
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Division (
/) - Modulus (
%) - Increment (
++) - Decrement (
--)
1. Addition (+)
The addition operator adds two operands.
Example:
public class AdditionExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
int result = a + b;
System.out.println("Addition: " + result); // Output: Addition: 15
}
}
2. Subtraction (-)
The subtraction operator subtracts the second operand from the first operand.
Example:
public class SubtractionExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
int result = a - b;
System.out.println("Subtraction: " + result); // Output: Subtraction: 5
}
}
3. Multiplication (*)
The multiplication operator multiplies two operands.
Example:
public class MultiplicationExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
int result = a * b;
System.out.println("Multiplication: " + result); // Output: Multiplication: 50
}
}
4. Division (/)
The division operator divides the first operand by the second operand. It is important to note that when using integer division, the result is an integer.
Example:
public class DivisionExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
int result = a / b;
System.out.println("Division: " + result); // Output: Division: 2
}
}
5. Modulus (%)
The modulus operator returns the remainder when the first operand is divided by the second operand.
Example:
public class ModulusExample {
public static void main(String[] args) {
int a = 10;
int b = 3;
int result = a % b;
System.out.println("Modulus: " + result); // Output: Modulus: 1
}
}
6. Increment (++)
The increment operator increases the value of the operand by 1. It can be used as a prefix or postfix operator.
Example:
public class IncrementExample {
public static void main(String[] args) {
int a = 10;
a++;
System.out.println("Increment: " + a); // Output: Increment: 11
}
}
7. Decrement (--)
The decrement operator decreases the value of the operand by 1. It can be used as a prefix or postfix operator.
Example:
public class DecrementExample {
public static void main(String[] args) {
int a = 10;
a--;
System.out.println("Decrement: " + a); // Output: Decrement: 9
}
}
Conclusion
Arithmetic operators in Java are fundamental for performing basic mathematical operations. Understanding and using these operators correctly is crucial for effective programming and data manipulation. They provide a foundation for more complex mathematical computations and algorithms in Java applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment