{"id":6460,"date":"2024-04-21T23:19:25","date_gmt":"2024-04-22T06:19:25","guid":{"rendered":"https:\/\/zentut.com\/?page_id=6460"},"modified":"2024-04-21T23:19:27","modified_gmt":"2024-04-22T06:19:27","slug":"java-arithmetic-operators","status":"publish","type":"page","link":"https:\/\/www.zentut.com\/java-tutorial\/java-arithmetic-operators\/","title":{"rendered":"Java Arithmetic Operators"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use Java arithmetic operators to perform various mathematical operations on numeric values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Java arithmetic operators<\/h2>\n\n\n\n<p>Arithmetic operators allow you to perform mathematical operations on numeric values. Java provides all basic arithmetic operators that perform addition (+), subtraction (-), multiplication (+), and division (\/). Also, it offers a remainder operator (%) that returns the remainder of a division. <\/p>\n\n\n\n<p>The following table illustrates the arithmetic operators in Java:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Operator<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>+<\/code><\/td><td>Additive operator<\/td><\/tr><tr><td><code>-<\/code><\/td><td>Subtraction operator<\/td><\/tr><tr><td><code>*<\/code><\/td><td>Multiplication operator<\/td><\/tr><tr><td><code>\/<\/code><\/td><td>Division operator<\/td><\/tr><tr><td><code>%<\/code><\/td><td>Remainder operator<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>These arithmetic operators can be applied to both integers and floats. Depending on the type of numbers, the result will be different. For example:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td colspan=\"2\"><\/td><td class=\"has-text-align-center\" data-align=\"center\" colspan=\"2\"><strong>Integer<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\" colspan=\"2\"><strong>Float<\/strong><\/td><\/tr><tr><td><strong>Operation<\/strong><\/td><td><strong>Operator<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Equation<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Result<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Equation<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Result<\/strong><\/td><\/tr><tr><td>Add<\/td><td>+<\/td><td class=\"has-text-align-center\" data-align=\"center\">15 + 2<\/td><td class=\"has-text-align-center\" data-align=\"center\">17<\/td><td class=\"has-text-align-center\" data-align=\"center\">15.0 + 2.0<\/td><td class=\"has-text-align-center\" data-align=\"center\">17.0<\/td><\/tr><tr><td>Subtract<\/td><td>&#8211;<\/td><td class=\"has-text-align-center\" data-align=\"center\">15 &#8211; 2<\/td><td class=\"has-text-align-center\" data-align=\"center\">13<\/td><td class=\"has-text-align-center\" data-align=\"center\">15.0 &#8211; 2.0<\/td><td class=\"has-text-align-center\" data-align=\"center\">13.0<\/td><\/tr><tr><td>Multiply<\/td><td>*<\/td><td class=\"has-text-align-center\" data-align=\"center\">15 * 2<\/td><td class=\"has-text-align-center\" data-align=\"center\">30<\/td><td class=\"has-text-align-center\" data-align=\"center\">15.0 * 2.0<\/td><td class=\"has-text-align-center\" data-align=\"center\">30.0<\/td><\/tr><tr><td>Divide<\/td><td>\/<\/td><td class=\"has-text-align-center\" data-align=\"center\">15\/2<\/td><td class=\"has-text-align-center\" data-align=\"center\">7<\/td><td class=\"has-text-align-center\" data-align=\"center\">15.0\/2.0<\/td><td class=\"has-text-align-center\" data-align=\"center\">7.5<\/td><\/tr><tr><td>Remainder<\/td><td>%<\/td><td class=\"has-text-align-center\" data-align=\"center\">15%2<\/td><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">15.0%2.0<\/td><td class=\"has-text-align-center\" data-align=\"center\">1.0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"note\">It&#8217;s important to note that when you divide two integers, you&#8217;ll get an integer, not a float. Therefore, there will be no fractional part. e.g., 15\/2 returns 7 instead of 7.5.<\/p>\n\n\n\n<p>The following program illustrates how to use the Java arithmetic operators on integers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n\n        <span class=\"hljs-keyword\">int<\/span> x = <span class=\"hljs-number\">15<\/span>;\n        <span class=\"hljs-keyword\">int<\/span> y = <span class=\"hljs-number\">2<\/span>;\n\n        <span class=\"hljs-keyword\">int<\/span> result = x + y;\n        System.out.println(result); <span class=\"hljs-comment\">\/\/ 17<\/span>\n\n        result = x - y;\n        System.out.println(result); <span class=\"hljs-comment\">\/\/ 13<\/span>\n\n        result =  x * y;\n        System.out.println(result); <span class=\"hljs-comment\">\/\/ 30<\/span>\n\n        result =  x \/ y;\n        System.out.println(result); <span class=\"hljs-comment\">\/\/ 7<\/span>\n\n        result =  x % y;\n        System.out.println(result); <span class=\"hljs-comment\">\/\/ 1<\/span>\n\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-number\">17<\/span>\n<span class=\"hljs-number\">13<\/span>\n<span class=\"hljs-number\">30<\/span>\n<span class=\"hljs-number\">7<\/span>\n<span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Similarly, the following program shows how to use the Java arithmetic operators on floating-point numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n\n        <span class=\"hljs-keyword\">float<\/span> x = <span class=\"hljs-number\">15.0f<\/span>;\n        <span class=\"hljs-keyword\">float<\/span> y = <span class=\"hljs-number\">2.0f<\/span>;\n\n        <span class=\"hljs-keyword\">float<\/span> result = x + y;\n        System.out.println(result); <span class=\"hljs-comment\">\/\/ 17.0<\/span>\n\n        result = x - y;\n        System.out.println(result); <span class=\"hljs-comment\">\/\/ 13.0<\/span>\n\n        result =  x * y;\n        System.out.println(result); <span class=\"hljs-comment\">\/\/ 30.0<\/span>\n\n        result =  x \/ y;\n        System.out.println(result); <span class=\"hljs-comment\">\/\/ 7.5<\/span>\n\n        result =  x % y;\n        System.out.println(result); <span class=\"hljs-comment\">\/\/ 1.0<\/span>\n\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-number\">17.0<\/span>\n<span class=\"hljs-number\">13.0<\/span>\n<span class=\"hljs-number\">30.0<\/span>\n<span class=\"hljs-number\">7.5<\/span>\n<span class=\"hljs-number\">1.0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Prefix and postfix operators<\/h2>\n\n\n\n<p>The prefix and postfix operators are used with <a href=\"https:\/\/zentut.com\/java-tutorial\/java-variables\/\">variables<\/a> to increment and decrement their values by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prefix operators<\/h3>\n\n\n\n<p>A prefix operator is placed before a variable. Java provides two prefix operators <code>++x<\/code> and <code>--x<\/code>, where <code>x<\/code> is a variable.<\/p>\n\n\n\n<p>The <code>++x<\/code> prefix operator increments a variable&#8217;s value by one and the <code>--x<\/code> prefix operator decrements a variable value by one before the value is used in an expression. <\/p>\n\n\n\n<p>The following example illustrates how to use the <code>++count<\/code> prefix operator to increment the <code>count<\/code> variable by one:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">0<\/span>;\n        System.out.println(++count); <span class=\"hljs-comment\">\/\/ 1<\/span>\n        System.out.println(count);   <span class=\"hljs-comment\">\/\/ 1<\/span>\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define a variable called <code>count<\/code> and initialize its value to 0.<\/li>\n\n\n\n<li>Second, increment the <code>count<\/code> variable by one and return it. The <code>System.out.println()<\/code> displays the value of the <code>count<\/code> variable which is 1.<\/li>\n\n\n\n<li>Third, display the value of the <code>count<\/code> variable again. As a result, it shows 1 as expected.<\/li>\n<\/ul>\n\n\n\n<p>The following program shows how to use the prefix operator <code>--count<\/code> to decrease the value of the <code>count<\/code> variable by one:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">1<\/span>;\n        System.out.println(--count); <span class=\"hljs-comment\">\/\/ 0<\/span>\n        System.out.println(count);   <span class=\"hljs-comment\">\/\/ 0<\/span>\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-number\">0<\/span>\n<span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Postfix operators<\/h3>\n\n\n\n<p>The postfix operators are placed after a variable. Java has two postfix operators: <code>x++<\/code> and <code>x--<\/code>, where <code>x<\/code> is a variable.<\/p>\n\n\n\n<p>The postfix operator <code>x++<\/code> increments the variable&#8217;s value and <code>x--<\/code> decrements the variable&#8217;s value <strong>after<\/strong> the value is used in the expression.<\/p>\n\n\n\n<p>The following program uses the postfix operator <code>count++<\/code> to increment a variable&#8217;s value after the value is used in the <code>System.out.println()<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">0<\/span>;\n        System.out.println(count++); <span class=\"hljs-comment\">\/\/ 0<\/span>\n        System.out.println(count);   <span class=\"hljs-comment\">\/\/ 1<\/span>\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-number\">0<\/span>\n<span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>count++<\/code> returns the value of the <code>count<\/code> variable first before incrementing the <code>count<\/code>&#8216;s value by one. Therefore, the first <code>System.out.println()<\/code> displays 0 while the second one shows 1.<\/p>\n\n\n\n<p>The following program illustrates how to use the postfix operator <code>count--<\/code> to decrement a variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">1<\/span>;\n        System.out.println(count--); <span class=\"hljs-comment\">\/\/ 1<\/span>\n        System.out.println(count);   <span class=\"hljs-comment\">\/\/ 0<\/span>\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we define the <code>count<\/code> variable and initialize its value to 1. The <code>count--<\/code> returns the count&#8217;s value first and then decreases the <code>count<\/code>&#8216;s value by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Compound assignment operators<\/h2>\n\n\n\n<p>To add a value to a variable, you use the <code>+<\/code> operator and assign the result back to the variable. For example, the following adds <code>2<\/code> to the <code>x<\/code> variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">int<\/span> x = <span class=\"hljs-number\">15<\/span>;\nx = x + <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the variable <code>x<\/code> appears on both the left and right sides of the expression, it&#8217;s quite redundant. <\/p>\n\n\n\n<p>To remove the redundancy, Java allows you to use something called a <strong>compound assign operator<\/strong> like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">int<\/span> x = <span class=\"hljs-number\">15<\/span>;\nx += <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>x += 2<\/code> is equivalent to the <code>x = x + 2<\/code>.<\/p>\n\n\n\n<p>By definition, a compound assignment operator combines an operator (addition, subtraction, multiplication, division, modular) with the assignment operator (=):<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Arithmetic Operators<\/th><th>Compound Assignment Operators<\/th><\/tr><\/thead><tbody><tr><td>x = x + n;<\/td><td>x += n;<\/td><\/tr><tr><td>x  = x  &#8211; n;<\/td><td>x -= n;<\/td><\/tr><tr><td>x = x * n;<\/td><td>x *= n;<\/td><\/tr><tr><td>x = x \/ n;<\/td><td>x \/= n;<\/td><\/tr><tr><td>x = x % n;<\/td><td>x %= n;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Operator precedence<\/h2>\n\n\n\n<p>If an expression contains multiple operators, Java uses a set of rules that determines the order in which operators are evaluated. <\/p>\n\n\n\n<p>This set of rules is called <strong>operator precedence<\/strong>. In Java, the operator that has higher precedence will be evaluated first.<\/p>\n\n\n\n<p>The following table shows the precedence of postfix operators, prefix operators, and arithmetic operators from high to low:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Operator<\/th><th>Operator Precedence (high to low)<\/th><\/tr><\/thead><tbody><tr><td>Postfix operators<\/td><td>x++, x&#8211;<\/td><\/tr><tr><td>Prefix operators<\/td><td>++x, &#8211;x<\/td><\/tr><tr><td>Multiplicative operators<\/td><td>*, \/ , %<\/td><\/tr><tr><td>Additive operators<\/td><td>+, &#8211;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The operators with the same precedence e.g., (+, -) are evaluated from left to right. To override precedence, you use parenthesis. If nested parenthesis is used, they are evaluated from the inside out.<\/p>\n\n\n\n<p>The following program illustrates how operator precedence works:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n       <span class=\"hljs-keyword\">int<\/span> x = <span class=\"hljs-number\">1<\/span>;\n       <span class=\"hljs-keyword\">int<\/span> y = <span class=\"hljs-number\">2<\/span>;\n       <span class=\"hljs-keyword\">int<\/span> z = <span class=\"hljs-number\">3<\/span>;\n\n       <span class=\"hljs-keyword\">int<\/span> result1 = x + y * z;\n       <span class=\"hljs-keyword\">int<\/span> result2 = (x + y) * z;\n\n       System.out.println(result1); <span class=\"hljs-comment\">\/\/ 7<\/span>\n       System.out.println(result2); <span class=\"hljs-comment\">\/\/ 9<\/span>\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this program, we have three variables <code>x<\/code>, <code>y<\/code>, and <code>z<\/code> and perform two different calculations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>result1<\/code> calculates <code>x + y * z<\/code>. Since multiplication has higher precedence than addition, <code>y * z<\/code> is evaluated first, and then the result is added to <code>x<\/code>. The <code>result1<\/code> is <code>7<\/code> because multiplication is performed before addition: <code>1 + 2 * 3<\/code> equals <code>1 + 6<\/code>, which is <code>7<\/code>.<\/li>\n\n\n\n<li><code>result2<\/code> calculates <code>(x + y) * z<\/code>. Here, we use parentheses to explicitly specify that the addition should be performed first, and then the result is multiplied by <code>z<\/code>.  The <code>result2<\/code> is <code>9<\/code> because we use parentheses to ensure addition is performed first: <code>(1 + 2) * 3<\/code> equals <code>3 * 3<\/code>, which is <code>9<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Java supports basic arithmetic operators including +, -, *, \/, and %.<\/li>\n\n\n\n<li>Use the prefix and postfix operators to increment or decrement a variable by one.<\/li>\n\n\n\n<li>Use compound assignment operators to make the code more concise.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use Java arithmetic operators to perform various mathematical operations on numeric values. Introduction to Java arithmetic operators Arithmetic operators allow you to perform mathematical operations on numeric values. Java provides all basic arithmetic operators that perform addition (+), subtraction (-), multiplication (+), and division (\/). Also, it [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":2190,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6460","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/comments?post=6460"}],"version-history":[{"count":2,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6460\/revisions"}],"predecessor-version":[{"id":6462,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6460\/revisions\/6462"}],"up":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/2190"}],"wp:attachment":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/media?parent=6460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}