BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.subtractExact() Method

By Chaitanya Singh | Filed Under: java

Java Math.subtractExact() method returns the difference of its arguments. It subtracts the value of second argument from the first argument and returns the result.

public class JavaExample
{
  public static void main(String[] args)
  {
    int i = 20, i2 = 10;
    long l = 10000L, l2 = 15000L;
    System.out.println(Math.subtractExact(i, i2));
    System.out.println(Math.subtractExact(l, l2));
  }
}

Output:

10
-5000

Syntax of Math.subtractExact() method

Math.subtractExact(100, 50); //returns 50

subtractExact() Description

public static int subtractExact(int x, int y): Returns the difference of its two integer arguments. It throws ArithmeticException, if one of the argument is Integer.MIN_VALUE.

public static long subtractExact(long x, long y): Returns the difference of its two long arguments. It throws ArithmeticException, if one of the argument is Long.MIN_VALUE.

subtractExact() Parameters

It takes two parameters:

  • x: First argument
  • y: Second argument

subtractExact() Return Value

  • It subtracts second argument y from the first argument x and returns (x-y) as result.

Example 1: Difference of two int arguments

public class JavaExample
{
  public static void main(String[] args)
  {
    int x = 10, y = 20; //two int variables
    System.out.println("Difference: "+Math.subtractExact(x, y));
  }
}

Output:

Java Math.subtractExact() Example Output_1

Example 2: Difference of two long arguments

public class JavaExample
{
  public static void main(String[] args)
  {
    long x = 7000L, y = 5000L; //two long variables
    System.out.println("Difference: "+Math.subtractExact(x, y));
  }
}

Output:

Java Math.subtractExact() Example Output_2

Example 3: Integer Overflow

public class JavaExample
{
  public static void main(String[] args)
  {
    int x = 100;
    int y = Integer.MIN_VALUE;
    System.out.println(Math.subtractExact(x, y));
  }
}

Output:

Java Math.subtractExact() Example Output_3

Example 4: Long Overflow

public class JavaExample
{
  public static void main(String[] args)
  {
    long x = 10000L;
    long y = Long.MIN_VALUE;
    System.out.println(Math.subtractExact(x, y));
  }
}

Output:

Java Math.subtractExact() Example Output_4

Recommended Posts

  • Java Math.addExact()
  • Java Math.nextDown()
  • Java Math.nextUp()
  • Java Math.nextAfter()
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap