The Wayback Machine - https://web.archive.org/web/20240922230156/https://www.geeksforgeeks.org/biginteger-max-and-min-methods-in-java/
Open In App

BigInteger max() and min() Methods in Java

Last Updated : 04 Dec, 2018
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisite: BigInteger Basics

  • BigInteger max() method: The max() method of the BigInteger returns the BigInteger whose value is the greater between current BigInteger and BigInteger passed as a parameter to the method. If both the values are equal, either may be returned.

    There is a similar method compareTo() available on BigInteger class. The max() method is different than compareTo() in a way that in the compareTo() method we have to interpret the result while on the max method, the biggest BigInteger is already returned.

    Syntax:

    public BigInteger max(BigInteger val)

    Parameters: The method accepts one parameter val which refers to the value with which the maximum is to be computed.

    Return Value: The method returns the BigInteger whose value is the greater of this and val. If they are equal, either may be returned.

    Below program illustrate max() method of BigInteger class.




    /*
     *Program Demonstrate max() method of BigInteger 
    */
    import java.math.*;
      
    public class GFG {
      
       public static void main(String[] args) {
      
            
           // Create 2 BigInteger objects
          BigInteger biginteger=new BigInteger("8976543245");
          BigInteger val=new BigInteger("9248040402");
            
          // Call max() method to find greater value
          // between two BigIntegers.
          BigInteger biggerInteger = biginteger.max(val);
      
          String result = "Bigger Integer between "+biginteger+" and "
                          +val+ " is " +biggerInteger;
      
          // Prints the result 
          System.out.println(result);
         
       }
    }

    
    

    Output:

    Bigger Integer between 8976543245 and 9248040402 is 9248040402
    
  • BigInteger min() method: The min() method of the BigInteger returns the BigInteger whose value is the lesser between current BigInteger and BigInteger passed as parameter to method. If both the values are equal, either may be returned.

    There is a similar method compareTo() available on BigInteger class . The min() method different than compareTo() in a way that in the compareTo() method we have to interpret the result while on the min() method, the smallest BigInteger will be returned.

    Syntax:

    public BigInteger min(BigInteger val)

    Parameters: The method accepts one parameter val which refers to the value with which the minimum is to be computed.

    Return Value: The method returns the BigInteger whose value is the lesser of this and val. If the values are equal, either may be returned.

    Below program illustrate min() method of BigInteger class.




    /*
     *Program Demonstrate min() method of BigInteger 
    */
    import java.math.*;
      
    public class GFG {
      
       public static void main(String[] args) {
      
        // Create 2 BigInteger objects
          BigInteger biginteger=new BigInteger("5782539631");
          BigInteger val=new BigInteger("3592633823");
            
          // Call min() method to find lesser value
          // between two BigIntegers.
          BigInteger biggerInteger = biginteger.min(val);
      
          String result = "lesser Integer between "+biginteger+" and "
                          +val+ " is " +biggerInteger;
      
          // Prints the result 
          System.out.println(result);
         
       }
    }

    
    

    Output:

    lesser Integer between 5782539631 and 3592633823 is 3592633823
    

Reference:



Similar Reads

Java.math.BigInteger.modInverse() method in Java
Prerequisite : BigInteger Basics The modInverse() method returns modular multiplicative inverse of this, mod m. This method throws an ArithmeticException if m <= 0 or this has no multiplicative inverse mod m (i.e., gcd(this, m) != 1). Syntax: public BigInteger modInverse(BigInteger m) Parameters: m - the modulus. Return Value: This method return
2 min read
Java.math.BigInteger.probablePrime() method in Java
Prerequisite : BigInteger Basics The probablePrime() method will return a Biginteger of bitLength bits which is prime. bitLength is provided as parameter to method probablePrime() and method will return a prime BigInteger of bitLength bits. The probability that a BigInteger returned by this method is composite and does not exceed 2^-100. Syntax: pu
2 min read
Min and Max in a List in Java
Given an unsorted list of integers, find maximum and minimum values in it. Input : list = [10, 4, 3, 2, 1, 20] Output : max = 20, min = 1 Input : list = [10, 400, 3, 2, 1, -1] Output : max = 400, min = -1Sorting This is least efficient approach but will get the work done. The idea is to sort the list in natural order, then the first or last element
5 min read
BigInteger and() Method in Java
The java.math.BigInteger.and(BigInteger val) method returns a BigInteger whose value is bitwise-AND of two BigIntegers. This method returns a negative number if both of the BigIntegers are negative. The and() method applies bitwise-AND operation upon the current bigInteger and bigInteger passed as parameter.Syntax: public BigInteger and(BigInteger
2 min read
Difference Between Long and BigInteger in Java
In Java, long and BigInteger are two different data types used to represent and perform operations on the large integers. In this article we will learn about BigInteger and Long in Java. long in Javalong is a primitive data type in Java used to store 64-bit signed integers. It has a range of values from -9,223,372,036,854,775,808 to 9,223,372,036,8
3 min read
Find max or min value in an array of primitives using Java
Java as a whole is a language that generally requires a lot of coding to execute specific tasks. Hence, having shorthand for several utilities can be beneficial. One such utility, to find maximum and minimum element in array is explained in this article using "aslist()". aslist() type casts a list from the array passed in its argument. This functio
2 min read
BigInteger signum() Method in Java
prerequisite : BigInteger Basics The java.math.BigInteger.signum() method helps us to identify whether a BigInteger is positive or zero or negative. It returns one of the following values depending on the following conditions: returns -1 when number is negativereturns 0 when number is zeroreturns +1 when number is positive Syntax: public int signum
2 min read
BigInteger shiftRight() Method in Java
prerequisite : BigInteger Basics The java.math.BigInteger.shiftRight(int n) method returns a BigInteger whose value is (this >> n). The shift distance, n, may be negative, in which case this method performs a left shift. The shiftRight() method will move each digit in a number's binary representation right by n times and the last bit in the d
2 min read
Java 8 | BigInteger divideAndRemainder() method with Examples
java.math.BigInteger.divideAndRemainder(BigInteger val) was introduced in Java 8. This method returns an array of two BigInteger after applying division operation between the BigInteger calling this method and the BigInteger passed as a parameter to the other method. Here, First BigInteger of the array represents a result of the division (this / va
3 min read
BigInteger abs() Method in Java
prerequisite : BigInteger Basics The java.math.BigInteger.abs() method returns absolute value of a BigInteger. By using this method one can find absolute value of any large size of numerical data stored as BigInteger. Syntax: public BigInteger abs() Parameters: The method does not take any parameters. Return Value: The method returns the absolute v
1 min read
BigInteger modPow() Method in Java
Prerequisite: BigInteger BasicsThe Java.math.BigInteger.modPow() method returns a BigInteger whose value is (thisexponent mod m ). If exponent == 1, the returned value is (this mod m) and if exponent < 0, the returned value is the modular multiplicative inverse of (this-exponent). The method throws an ArithmeticException if m <= 0. Syntax: pu
2 min read
BigInteger or() method in Java
Prerequisite: BigInteger Basics The java.math.BigInteger.or(BigInteger val) method is used to perform bitwise OR of two BigIntegers. One of the BigInteger is passed in parameter and the other on which the function is called. This method returns a negative number if either of the BigIntegers used with the function is negative. OR() method of BigInte
2 min read
BigInteger xor() Method in Java
Prerequisite: BigInteger Basics The xor(BigInteger val) method returns bitwise-XOR of two bigIntegers. This method returns a negative BigInteger if and only if exactly one of the current bigInteger and the bigInteger passed in parameter id negative. The xor() method of BigInteger class apply bitwise-XOR operation upon the current BigInteger and Big
2 min read
BigInteger not() Method in Java
The java.math.BigInteger.not() method is used to find the bitwise-NOT of a BigInteger. This method returns a negative value if and only if this BigInteger is non-negative. The BigInteger.not() method apply bitwise Not operation upon the current bigInteger. Syntax: public BigInteger not() Parameters: The method does not take any parameters. Return V
1 min read
BigInteger andNot() Method in Java
The java.math.BigInteger.andNot(BigInteger val) method returns a BigInteger whose value is (this & ~val) where this to the current BigInteger with which the function is being used and val is the bigInteger passed to the function as a parameter. This method, which is equivalent to and(val.not()), is provided as a convenience for masking operatio
2 min read
BigInteger bitCount() Method in Java
Prerequisite: BigInteger Basics The java.math.BigInteger.bitCount() method returns number of bits in the two's complement representation of this BigInteger that differ from its sign bit. This method is useful when implementing bit-vector style sets atop BigIntegers.Syntax: public int bitCount() Parameters: The method does not take any parameters.Re
1 min read
BigInteger bitLength() Method in Java
The java.math.BigInteger.bitLength() method returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit. For positive BigIntegers, this is equivalent to the number of bits in the ordinary binary representation. The bitLength method Computes (ceil(log2(this < 0 ? -this : this+1))).Syntax: pub
2 min read
BigInteger clearBit() Method in Java
Prerequisite: BigInteger BasicsThe clearBit() method returns a BigInteger which is used to clear a particular bit position in a BigInteger. The bit at index n of binary representation of BigInteger will be cleared means converted to zero. Mathematically we can say that it is used to calculate this & ~(1<<n).Syntax: public BigInteger clear
2 min read
BigInteger flipBit() Method in Java
Prerequisite: BigInteger Basics The java.math.BigInteger.flipBit(index) method returns a BigInteger which is used to flip a particular bit position in a BigInteger. This method Computes (bigInteger ^ (1<<n)). The bit at index n of binary representation of the bigInteger will be flipped. That is, if the bit position is 0 it will be converted t
2 min read
BigInteger negate() Method in Java
Prerequisite: BigInteger Basics The java.math.BigInteger.negate() method returns a BigInteger whose value is (- this). negate() method will change the signed bit of BigInteger. Syntax: public BigInteger negate() Parameters: The method does not accept any parameter. Return Value: The method returns the operation of (- this). Examples: Input: value =
1 min read
BigInteger setBit() Method in Java
The java.math.BigInteger.setbit(index) method returns a Big-integer whose value is equivalent to this Big-integer with the designated bit set. The method computes (this | (1<<n)). The bit at index n of binary representation of Big-integer will be set means converted to 1. Syntax: public BigInteger setbit(int n) Parameters: The method takes on
2 min read
BigInteger shiftLeft() Method in Java
The java.math.BigInteger.shiftLeft(int n) method returns a BigInteger whose value is (this << n). The shift distance, n, may be negative, in which case this method performs a right shift.shiftLeft() method will moves each digit in a number's binary representation left by n times and the last bit in the direction of the shift is replaced by 0.
2 min read
BigInteger testBit() Method in Java
prerequisite : BigInteger Basics The java.math.BigInteger.testBit(index) method returns true if and only if the designated bit is set. This method Computes (this & (1<<n)) != 0). Syntax: public boolean testBit(int n) Parameter: The method takes one parameter n of integer type which refers to the index of the bit that needs to be tested. R
2 min read
BigInteger getLowestSetBit() Method in Java
prerequisite : BigInteger Basics The java.math.BigInteger.getLowestSetBit() method returns the index of the rightmost (lowest-order) set bit of this BigInteger. It means this method returns the number of zero or unset bits to the right of the rightmost set bit. If the BigInteger contains no set bit then this method will return -1. The method comput
2 min read
Java 8 | BigInteger longValueExact() Method with Examples
java.math.BigInteger.longValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a long and checks for lost information. If the value of BigInteger is greater than 9,223,372,036,854,775,807 or less than -9,223,372,036,854,775,808; the method will throw ArithmeticException as BigInteger doesn’t fit
3 min read
Java 8 | BigInteger shortValueExact() Method with Examples
java.math.BigInteger.shortValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a short and checks for lost information. If the value of BigInteger is greater than 32,767 or less than -32,768; the method will throw ArithmeticException as BigInteger doesn’t fit in short range. Syntax: public short
3 min read
BigInteger intValueExact() Method in Java with Examples
java.math.BigInteger.intValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a int and checks for lost information. If the value of BigInteger is greater than 2,147,483,647 or less than -2,147,483,648; the method will throw ArithmeticException as BigInteger doesn’t fit in int range. Syntax: publ
3 min read
BigInteger equals() Method in Java
The java.math.BigInteger.equals(Object x) method compares this BigInteger with the object passed as the parameter and returns true in both are equal in value else it returns false. Syntax: public boolean equals(Object x) Parameter: This method accepts a single mandatory parameter x which is the Object to which BigInteger Object is to be compared. R
3 min read
BigInteger compareTo() Method in Java
The java.math.BigInteger.compareTo(BigInteger value) method Compares this BigInteger with the BigInteger passed as the parameter. Syntax: public int compareTo(BigInteger val)Parameter: This method accepts a single mandatory parameter val which is the BigInteger to compare with BigInteger object. Return Type: This method returns the following: 0: if
2 min read
BigInteger hashCode() Method in Java
The java.math.BigInteger.hashCode() method returns the hash code for this BigInteger. The hashcode is always the same if the object doesn't change. Hashcode is a unique code generated by the JVM at the time of object creation. We can use hashcode to perform some operation on hashing related algorithms like hashtable, hashmap etc. We can search an o
2 min read