🎓 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
In Java programming, manipulating numeric values is a fundamental aspect, and sometimes, you might find yourself needing to convert a positive integer to its negative counterpart. This conversion is a straightforward process, but understanding how to do it correctly is essential for various applications, such as mathematical computations, data transformations, or algorithm implementations. In this blog post, we'll explore how to convert a positive integer to a negative integer in Java.
1. Simple Negation Using the Unary Minus Operator
The most direct way to convert a positive integer to a negative is by using the unary minus (-) operator. This operator changes the sign of the number.
Example:
public class PositiveToNegative {
public static void main(String[] args) {
int positiveNumber = 50;
int negativeNumber = -positiveNumber;
System.out.println("Positive Number: " + positiveNumber);
System.out.println("Converted Negative Number: " + negativeNumber);
}
}
Output:
Positive Number: 50 Converted Negative Number: -50In this example, the unary minus operator is used to convert the positive number 50 to -50.
2. Using Math.negateExact()
Example:
public class PositiveToNegative {
public static void main(String[] args) {
int positiveNumber = 50;
int negativeNumber = Math.negateExact(positiveNumber);
System.out.println("Positive Number: " + positiveNumber);
System.out.println("Converted Negative Number: " + negativeNumber);
}
}
Output:
Positive Number: 50 Converted Negative Number: -50Using Math.negateExact() not only converts the number but also throws an ArithmeticException if the result overflows an int.
Handling Edge Cases
Example of Integer Overflow:
public class PositiveToNegative {
public static void main(String[] args) {
int positiveNumber = Integer.MAX_VALUE;
int negativeNumber = -positiveNumber;
System.out.println("Positive Number: " + positiveNumber);
System.out.println("Converted Negative Number: " + negativeNumber);
}
}
Output:
Positive Number: 2147483647 Converted Negative Number: -2147483647In this case, negating Integer.MAX_VALUE doesn't result in overflow, but if you were to negate Integer.MIN_VALUE, it would.
Conclusion
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