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

By Chaitanya Singh | Filed Under: java

Java Math.pow() method returns, first argument raised to the power of second argument. For example, Math.pow(3, 2) returns 9. In this tutorial, we will discuss pow() method with examples.

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = 3, num2 = 2;
    //3 raised to the power 2 == 3*3 == 9
    System.out.println(Math.pow(num, num2));
  }
}

Output:

9.0

Syntax of Math.pow() method

Math.pow(4, 2); //returns 16.0

pow() Description

public static double pow(double a, double b): It returns the value of a raised to the power b.

pow() Parameters

It takes two parameters:

  • a: First argument, the base.
  • b: Second argument, the exponent.

pow() Return Values

  • Returns a to the power b: ab.
  • If the second argument is 1 then it returns the first argument as output.
  • If the second argument is zero of any sign, it returns 1.0 as output.
  • If the first or second argument is NaN then it returns NaN (Not a Number).
  • If first argument is finite & less than zero and second argument is not an integer then NaN is returned.

Example 1: Finding power using Math.pow()

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = 4, num2 = 2;
    //4 raised to the power 2 == 4*4 == 16
    System.out.println("4 to the power 2: "+Math.pow(num, num2));
  }
}

Output:

Java Math.pow() Example Output_1

Example 2: Power of a negative number (Base is negative)

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = -4, num2 = 2;
    //-4 raised to the power 2 == -4 * -4 == 16
    System.out.println("-4 to the power 2: "+Math.pow(num, num2));
  }
}

Output:

Java Math.pow() Example Output_2

Example 3: If exponent is negative

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = 4, num2 = -2;
    //4 raised to the power -2 == 1/4 * 1/4 == 1/16
    System.out.println("4 to the power -2: "+Math.pow(num, num2));
  }
}

Output:

Java Math.pow() Example Output_3

Example 4: Base negative and exponent is not an integer

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = -10, num2 = .2;
    // If first argument is finite and less than zero and
    // second argument is not an integer then result is NaN
    System.out.println(Math.pow(num, num2));
  }
}

Output:

Java Math.pow() Example Output_4

Recommended Posts

  • Java Math.cbrt()
  • Java Math.sqrt()
  • Java Math.round()
  • Java Math.min()
❮ 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