🎓 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
Arithmetic operators in Java are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. These operators are fundamental for manipulating numerical data and are frequently used in Java programming.
What are Arithmetic Operators?
Arithmetic operators are symbols used within expressions to perform basic mathematical operations. They operate on numerical values (constants and variables) and return a single numerical value.
Types of Arithmetic Operators in Java
Java provides the following 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 = 20;
int sum = a + b;
System.out.println("Sum: " + sum); // Output: Sum: 30
}
}
2. Subtraction (-)
The subtraction operator subtracts the second operand from the first.
Example:
public class SubtractionExample {
public static void main(String[] args) {
int a = 20;
int b = 10;
int difference = a - b;
System.out.println("Difference: " + difference); // Output: Difference: 10
}
}
3. Multiplication (*)
The multiplication operator multiplies two operands.
Example:
public class MultiplicationExample {
public static void main(String[] args) {
int a = 10;
int b = 20;
int product = a * b;
System.out.println("Product: " + product); // Output: Product: 200
}
}
4. Division (/)
The division operator divides the first operand by the second. Note that division by zero will throw an ArithmeticException.
Example:
public class DivisionExample {
public static void main(String[] args) {
int a = 20;
int b = 10;
int quotient = a / b;
System.out.println("Quotient: " + quotient); // Output: Quotient: 2
}
}
5. Modulus (%)
The modulus operator returns the remainder when the first operand is divided by the second.
Example:
public class ModulusExample {
public static void main(String[] args) {
int a = 20;
int b = 3;
int remainder = a % b;
System.out.println("Remainder: " + remainder); // Output: Remainder: 2
}
}
6. Increment (++)
The increment operator increases the value of an operand by 1. It can be used as a prefix (++a) or a suffix (a++).
Example:
public class IncrementExample {
public static void main(String[] args) {
int a = 10;
++a; // Prefix increment
System.out.println("Prefix Increment: " + a); // Output: Prefix Increment: 11
a = 10;
a++; // Suffix increment
System.out.println("Suffix Increment: " + a); // Output: Suffix Increment: 11
}
}
7. Decrement (--)
The decrement operator decreases the value of an operand by 1. It can be used as a prefix (--a) or a suffix (a--).
Example:
public class DecrementExample {
public static void main(String[] args) {
int a = 10;
--a; // Prefix decrement
System.out.println("Prefix Decrement: " + a); // Output: Prefix Decrement: 9
a = 10;
a--; // Suffix decrement
System.out.println("Suffix Decrement: " + a); // Output: Suffix Decrement: 9
}
}
Conclusion
Arithmetic operators in Java are used to perform basic mathematical operations. They allow for efficient manipulation of numerical data and are integral to many programming tasks. By understanding and using these operators correctly, you can perform calculations and manage data effectively in your Java programs.
❮ Previous Chapter Next Chapter ❯
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