bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Java/Java Tutorial
Java•Java Tutorial

Java Math

The Java Math class has many methods that allows you to perform mathematical tasks on numbers.

Math.max( x,y )

The Math.max( x , y ) method can be used to find the highest value of x and y :

Math.max(5, 10);

Math.min( x,y )

The Math.min( x , y ) method can be used to find the lowest value of x and y :

Math.min(5, 10);

Math.sqrt( x )

The Math.sqrt( x ) method returns the square root of x :

Math.sqrt(64);

Math.abs( x )

The Math.abs( x ) method returns the absolute (positive) value of x :

Math.abs(-4.7);

Math.pow( x, y )

The Math.pow( x, y ) method returns the value of x raised to the power of y :

Math.pow(2, 8); // 256.0

Note

Math.pow(2, 8) means 2 multiplied by itself 8 times: 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 256

Note

The Math.pow() method always returns a double , even if the result is a whole number. For example, Math.pow(2, 8) returns 256.0 (not 256 ).

Rounding Methods

Java has several methods for rounding numbers:

  • Math.round(x) - rounds to the nearest integer
  • Math.ceil(x) - rounds up (returns the smallest integer greater than or equal to x)
  • Math.floor(x) - rounds down (returns the largest integer less than or equal to x)
Math.round(4.6); // 5
Math.ceil(4.1); // 5.0
Math.floor(4.9); // 4.0

Random Numbers

Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):

Math.random();

To get more control over the random number, for example, if you only want a random number between 0 and 100, you can use the following formula:

int randomNum = (int)(Math.random() * 101); // 0 to 100

Note

Math.random() returns a double . To get an integer, you need to cast it with (int) .

Previous

Java Special Characters

Next

Java Booleans

This chapter

Overview
49

Lessons

110m

Read time

1. Java Tutorial2. Java Introduction3. Java Getting Started4. Java Syntax5. Java Statements6. Java Output / Print7. Java Output Numbers8. Java Comments9. Java Variables10. Java Print Variables11. Java Declare Multiple Variables12. Java Identifiers13. Java Constants (final)14. Java Data Types15. Java Numbers16. Java Boolean Data Types17. Java Characters18. Java Data Types Example19. Java Non-Primitive Data Types20. Java var Keyword21. Java Type Casting22. Java Operators23. Java Arithmetic Operators24. Java Assignment Operators25. Java Comparison Operators26. Java Logical Operators27. Java Operator Precedence28. Java Strings29. Java String Concatenation30. Java Numbers and Strings31. Java Special Characters32. Java Math33. Java Booleans34. Java If ... Else35. Java Else36. Java Else If37. Java Short Hand If...Else (Ternary Operator)38. Java Nested If39. Java Logical Operators in Conditions40. Java Switch41. Java While Loop42. Java Do/While Loop43. Java For Loop44. Java Nested Loops45. Java For Each Loop46. Java Break and Continue47. Java Arrays48. Java Arrays Loop49. Java Multi-Dimensional Arrays

On this page

Math.max( x,y )Math.min( x,y )Math.sqrt( x )Math.abs( x )Math.pow( x, y )Rounding MethodsRandom Numbers