Home » Java programming language

# Java Math Class static double copySign(double d1 , double d2) with example

**Java Math Class copySign(double d1 , double d2) method**: Here, we are going to learn about the **copySign(double d1 , double d2) method** of Math Class with its syntax and example.

Submitted by Preeti Jain, on September 01, 2019

## Math Class copySign(double d1 , double d2)

- This method is available in java.lang package.
- This method is used to return the first floating-point argument along with returning the sign of the second floating-point argument.
- This is a static method so it is accessible with the class name too.
- The return type of this method is double that means it returns the first argument with the sign of second argument.
- In this method we pass two parameters as arguments of double type: First Parameter – it is the double floating-point argument number which is to be returned, and the Second Parameter – it is also a double floating-point number which is not to be returned but its sign will be returned with the first argument.
- This method does not throw any exception.
- This is an overloaded method so two versions of this method are available like one is double type argument and another is of the float type argument.

**Syntax:**

public static double copySign(double d1 , double d2){ }

**Parameter(s):**

- d1 – value to be returned.
- d2 – value whose sign to be returned with the value.

**Return value:**

The return type of this method is double, The return type of this method is double that means it returns the first argument with the sign of second argument.

### Java program to demonstrate example of copySign(double d1 , double d2) method

// Java program to demonstrate the example of // copySign(double d1 , double d2) method of Math Class class CopySignMethod { public static void main(String[] args) { // Here we are declaring few variables double d1 = 100.6; double d2 = -200.6; // Display previous value of d1 and d2 System.out.println("Old value of d1 before implementation is: " + d1); System.out.println("Old value of d2 before implementation is :" + d2); // Here , we will get (-100.6) because we are passing parameter // (100.6,-200.6) so it will return first argument value and // return the sign of the second argument [d1=100.6 , d2= -200.6] System.out.println("New value after implementation is :" + Math.copySign(d1, d2)); // Here , we will get (200.6) because we are passing parameter // (-200.6,100.6) so it will return first argument value and // return the sign of the second argument System.out.println("New value after implementation is :" + Math.copySign(d2, d1)); } }

**Output**

E:\Programs>javac CopySignMethod.java E:\Programs>java CopySignMethod Old value of d1 before implementation is :100.6 Old value of d2 before implementation is :-200.6 New value after implementation is : -100.6 New value after implementation is : 200.6

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL