java.math.BigInteger negate()
Description
Method Syntax
public BigInteger negate()
Method Argument
| Data Type | Parameter | Description |
|---|---|---|
| N/A | N/A | N/A |
Method Returns
The negate() method returns -this
Compatibility
Requires Java 1.1 and up
Java BigInteger negate() Example
Below is a java code demonstrates the use of negate() method of BigInteger class. The example presented might be simple however it shows the behavior of the negate() method.
package com.javatutorialhq.java.examples;
import java.math.BigInteger;
import java.util.Scanner;
/*
* A java example source code to demonstrate
* the use of negate() method of BigInteger class
*/
public class BigIntegerNegateExample {
public static void main(String[] args) {
// get user input
System.out.print("Enter a value:");
Scanner s = new Scanner(System.in);
String input = s.nextLine();
s.close();
// declare a new BigInteger
BigInteger val1 = new BigInteger("-1245");
// convert string input to BigInteger
BigInteger val2 = new BigInteger(input);
// get the negative value of both the BigInteger and print it
System.out.println("Negation of val1="+val1.negate());
System.out.println("Negation of val2="+val2.negate());
}
}
This example is a lot simpler than it looks. Basically we ask for user input and converted this value into BigInteger. Another BigInteger object were also initialize with constructor having a value of “-1245”. We then use the negete() method to get the negative value of both the input.
