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.decrementExact() Method

By Chaitanya Singh | Filed Under: java

Java Math.decrementExact() method returns the argument after decreasing it by one. In this tutorial, we will discuss decrementExact() method with examples.

public class JavaExample
{
  public static void main(String[] args)
  {
    int i = 50; //int
    long l = 16004L; //long
    System.out.println(Math.decrementExact(i));
    System.out.println(Math.decrementExact(l));
  }
}

Output:

49
16003

Syntax of decrementExact() method

Math.decrementExact(50); //returns 49

decrementExact() Description

public static int decrementExact(int a): Returns the int argument a after decreasing it by one. It throws ArithmeticException, if the result overflows an int (which means if the argument is Integer.MIN_VALUE, the decrement operation would throw the exception).

public static long decrementExact(long a): Returns the long argument a after decreasing it by one. It throws ArithmeticException, if the result overflows a long (which means if the argument is Long.MIN_VALUE, the decrement operation would throw the exception).

decrementExact() Parameters

  • a: Int or long value that needs to be decremented.

decrementExact() Return Value

  • Returns argument minus one (i.e. a-1).

Example 1: Decrement int value by one

public class JavaExample
{
  public static void main(String[] args)
  {
    int i = 160; //int
    System.out.print("Value of i, decremented by one: ");
    System.out.println(Math.decrementExact(i));
  }
}

Output:

Java Math.decrementExact() example output_1

Example 2: Decrement long value by one

public class JavaExample
{
  public static void main(String[] args)
  {
    long l = 160005L; //long
    System.out.print("Value of l, decremented by one: ");
    System.out.println(Math.decrementExact(l));
  }
}

Output:

Java Math.decrementExact() example output_2

Example 3: Integer overflow while calling decrementExact()

public class JavaExample
{
  public static void main(String[] args)
  {
    int i = Integer.MIN_VALUE;
    System.out.println(Math.decrementExact(i));
  }
}

Output:

Java Math.decrementExact() example output_3

Example 4: Long overflow while calling decrementExact()

public class JavaExample
{
  public static void main(String[] args)
  {
    long l = Long.MIN_VALUE;
    System.out.println(Math.decrementExact(l));
  }
}

Output:

Java Math.decrementExact() example output_4

Recommended Posts

  • Java Math.incrementExact()
  • Java Math.multiplyExact()
  • Java Math.subtractExact()
  • Java Math.addExact()
❮ 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