Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Power function in Java? – Know its uses

Last updated on Jun 17,2021 46K Views

A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
52 / 72 Blog from Java Core Concepts

Power function in Java is used to calculate a number raised to the power of some other number. This function accepts two parameters and returns the value of the first parameter raised to the second parameter. In this article, I will tell you the use of a power function.

Below topics are covered in this article:

Let’s get started!

Introduction to Power function in Java

Java Pow() - Power Function in Java - EdurekaPower function in Java is of type java.lang.Math.pow() library. It is mainly used to return the value of the first argument raised to the power of the second argument. It works similar to the exponents that we use in our mathematics.

Syntax:

double pow(double base, double exponent)
  • base − Any primitive data type.
  • exponent − Any primitive data type

Return: This method returns baseexponent.

  • If the second argument is positive or negative zero, this method will return 1.0.
  • If the second argument is not a number (NaN), this method will return NaN.
  • If the second argument is 1, this method will return the result same as the first argument.

With this, now let’s move further and look the various ways of using pow() with the help of below examples.

Examples of Power Function

Example 1: Demonstrates the working of java.lang.Math.pow() method.

import java.lang.Math;
public class Example1 {
public static void main(String args[]) {
double x = 60;
double y = 3;
System.out.println(Math.pow(x, y));
x = 3;
y = 4;
System.out.println(Math.pow(x, y));
x = 2;
y = 5;
System.out.println(Math.pow(x, y));
}
}

Output:

216000
81
32

Example 2:

public class Example2 {
public static void main(String[] args) {
double a = 18.0;
double b = -3;
//return (18) power of -3
System.out.println(Math.pow(a, b));
}
}

Output:

1.7146776406035665294924554183813e-4

Example 3:

public class Example3 {
public static void main(String[] args) {
double a = -107;
double b = 0.6;
//returns NaN
System.out.println(Math.pow(a, b));
}
}

Output: 

NaN

With this, we come to an end of this article on Power Function in Java. I hope you found it informative and understood the various uses of pow() method. If you wish to learn fundamentals of Java in depth, kindly check other blogs on Java.

Check out the Java Online Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. 

Got a question for us? Please mention it in the comments section of this “Power Function in Java” article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Power function in Java? – Know its uses

edureka.co