{"id":2034,"date":"2026-11-20T06:02:17","date_gmt":"2026-11-20T06:02:17","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=2034"},"modified":"2026-11-20T06:02:17","modified_gmt":"2026-11-20T06:02:17","slug":"java-math","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/java-math\/","title":{"rendered":"Java Math Class: Methods with Examples (abs, pow, sqrt, round, random)"},"content":{"rendered":"<p>Java&#8217;s <code>Math<\/code> class (in the <code>java.lang<\/code> package) gives you ready-made methods for common mathematical work: absolute values, powers, square roots, rounding, trigonometry, logarithms and random numbers. You don&#8217;t need to import it or create an object; every method is <code>static<\/code>, so you call it directly as <code>Math.methodName()<\/code>.<\/p>\n<h2>Commonly Used Math Methods<\/h2>\n<table>\n<thead>\n<tr>\n<th>Method<\/th>\n<th>What it returns<\/th>\n<th>Example<\/th>\n<th>Result<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>Math.abs(x)<\/code><\/td>\n<td>Absolute (positive) value<\/td>\n<td><code>Math.abs(-7.5)<\/code><\/td>\n<td><code>7.5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.max(a, b)<\/code><\/td>\n<td>Larger of two values<\/td>\n<td><code>Math.max(10, 20)<\/code><\/td>\n<td><code>20<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.min(a, b)<\/code><\/td>\n<td>Smaller of two values<\/td>\n<td><code>Math.min(10, 20)<\/code><\/td>\n<td><code>10<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.sqrt(x)<\/code><\/td>\n<td>Square root<\/td>\n<td><code>Math.sqrt(64)<\/code><\/td>\n<td><code>8.0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.cbrt(x)<\/code><\/td>\n<td>Cube root<\/td>\n<td><code>Math.cbrt(27)<\/code><\/td>\n<td><code>3.0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.pow(a, b)<\/code><\/td>\n<td>a raised to the power b<\/td>\n<td><code>Math.pow(2, 10)<\/code><\/td>\n<td><code>1024.0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.hypot(x, y)<\/code><\/td>\n<td>\u221a(x\u00b2 + y\u00b2)<\/td>\n<td><code>Math.hypot(3, 4)<\/code><\/td>\n<td><code>5.0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.ceil(x)<\/code><\/td>\n<td>Smallest whole number \u2265 x<\/td>\n<td><code>Math.ceil(4.2)<\/code><\/td>\n<td><code>5.0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.floor(x)<\/code><\/td>\n<td>Largest whole number \u2264 x<\/td>\n<td><code>Math.floor(4.8)<\/code><\/td>\n<td><code>4.0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.round(x)<\/code><\/td>\n<td>Nearest whole number (as long\/int)<\/td>\n<td><code>Math.round(4.5)<\/code><\/td>\n<td><code>5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.random()<\/code><\/td>\n<td>Random double from 0.0 up to (not including) 1.0<\/td>\n<td><code>Math.random()<\/code><\/td>\n<td>e.g. <code>0.7314...<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.log(x)<\/code>, <code>Math.log10(x)<\/code><\/td>\n<td>Natural log, log base 10<\/td>\n<td><code>Math.log10(1000)<\/code><\/td>\n<td><code>3.0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Math.signum(x)<\/code><\/td>\n<td>-1.0, 0.0 or 1.0 depending on the sign<\/td>\n<td><code>Math.signum(-3.2)<\/code><\/td>\n<td><code>-1.0<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Notice that most methods return a <code>double<\/code>, which Java prints with <code>.0<\/code>, even when the answer is a whole number.<\/p>\n<h2>Example Program<\/h2>\n<pre class=\"wp-block-code\"><code>public class MathDemo {\n    public static void main(String[] args) {\n        System.out.println(Math.abs(-7.5));\n        System.out.println(Math.max(10, 20));\n        System.out.println(Math.sqrt(64));\n        System.out.println(Math.pow(2, 10));\n        System.out.println(Math.ceil(4.2));\n        System.out.println(Math.floor(4.8));\n        System.out.println(Math.round(4.5));\n        System.out.println(Math.round(-4.5));\n    }\n}<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>7.5\n20\n8.0\n1024.0\n5.0\n4.0\n5\n-4<\/code><\/pre>\n<p><code>Math.round()<\/code> rounds halves <em>up<\/em> (towards positive infinity), so <code>-4.5<\/code> becomes <code>-4<\/code>, not <code>-5<\/code>.<\/p>\n<h2>Constants: Math.PI and Math.E<\/h2>\n<pre class=\"wp-block-code\"><code>System.out.println(Math.PI);\nSystem.out.println(Math.E);\n\ndouble radius = 7;\ndouble area = Math.PI * radius * radius;\nSystem.out.println(area);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>3.141592653589793\n2.718281828459045\n153.93804002589985<\/code><\/pre>\n<h2>Trigonometry Uses Radians<\/h2>\n<p>Java&#8217;s <code>sin()<\/code>, <code>cos()<\/code> and <code>tan()<\/code> expect <strong>radians<\/strong>, not degrees. Convert with <code>Math.toRadians()<\/code>:<\/p>\n<pre class=\"wp-block-code\"><code>System.out.println(Math.toRadians(180));\nSystem.out.println(Math.sin(Math.toRadians(30)));\nSystem.out.println(Math.cos(Math.toRadians(60)));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>3.141592653589793\n0.49999999999999994\n0.5000000000000001<\/code><\/pre>\n<p>You might expect exactly <code>0.5<\/code>. The tiny difference comes from how computers store decimal numbers (floating point). Round the result if you need to display it.<\/p>\n<h2>Practical Example: Compound Interest<\/h2>\n<pre class=\"wp-block-code\"><code>double principal = 10000;   \/\/ \u20b910,000\ndouble rate = 8;            \/\/ 8% per year\nint years = 3;\n\ndouble amount = principal * Math.pow(1 + rate \/ 100, years);\nSystem.out.println(Math.round(amount * 100) \/ 100.0);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>12597.12<\/code><\/pre>\n<p>Multiplying by 100, rounding and dividing by <code>100.0<\/code> is a common way to keep two decimal places. (For real money calculations, Java programs usually use <code>BigDecimal<\/code>.)<\/p>\n<h2>Random Numbers in a Range<\/h2>\n<pre class=\"wp-block-code\"><code>\/\/ random dice roll from 1 to 6\nint dice = (int) (Math.random() * 6) + 1;\nSystem.out.println(dice);<\/code><\/pre>\n<p><code>Math.random()<\/code> gives a number from 0.0 to just under 1.0. Multiplying by 6 gives 0.0 to just under 6.0; casting to <code>int<\/code> gives 0 to 5; adding 1 gives 1 to 6. For many random numbers, the <code>java.util.Random<\/code> class is more convenient.<\/p>\n<h2>Integer Division Traps<\/h2>\n<pre class=\"wp-block-code\"><code>System.out.println(-7 \/ 2);                 \/\/ integer division truncates\nSystem.out.println(Math.floorDiv(-7, 2));   \/\/ rounds down\nSystem.out.println(Math.floorMod(-7, 2));   \/\/ always non-negative for a positive divisor\nSystem.out.println(Math.floor(10 \/ 3));     \/\/ 10 \/ 3 is already the int 3<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>-3\n-4\n1\n3.0<\/code><\/pre>\n<p>In the last line, <code>10 \/ 3<\/code> is integer division, giving <code>3<\/code> before <code>floor()<\/code> even runs. Write <code>10 \/ 3.0<\/code> if you want decimal division.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>Do I need to import the Math class in Java?<\/h3>\n<p>No. It is in <code>java.lang<\/code>, which Java imports automatically.<\/p>\n<h3>Why does Math.sqrt(64) print 8.0 and not 8?<\/h3>\n<p>Because <code>sqrt()<\/code> returns a <code>double<\/code>. Cast it with <code>(int) Math.sqrt(64)<\/code> if you need an integer.<\/p>\n<h3>What is the difference between Math.round(), Math.ceil() and Math.floor()?<\/h3>\n<p><code>round()<\/code> goes to the nearest whole number, <code>ceil()<\/code> always goes up, and <code>floor()<\/code> always goes down. See <a href=\"https:\/\/gyanipandit.com\/programming\/java-math-floor\/\">Java Math.floor()<\/a>.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/java-methods\/\">Java methods<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/assignment-operators-java\/\">Assignment operators in Java<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/arithmetic-operators-in-java\/\">Arithmetic operators in Java<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java&#8217;s Math class (in the java.lang package) gives you ready-made methods for common mathematical work: absolute values, powers, square roots, rounding, trigonometry, logarithms and random numbers. You don&#8217;t need to import it or create an object; every method is static, so you call it directly as Math.methodName(). Commonly Used Math Methods Method What it returns &#8230; <a title=\"Java Math Class: Methods with Examples (abs, pow, sqrt, round, random)\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/java-math\/\" aria-label=\"Read more about Java Math Class: Methods with Examples (abs, pow, sqrt, round, random)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[12,11],"class_list":["post-2034","post","type-post","status-publish","format-standard","hentry","category-java-tutorial","tag-java","tag-java-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=2034"}],"version-history":[{"count":6,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2034\/revisions"}],"predecessor-version":[{"id":6148,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2034\/revisions\/6148"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=2034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=2034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=2034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}