{"id":64,"date":"2019-01-16T15:49:48","date_gmt":"2019-01-16T15:49:48","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=64"},"modified":"2019-03-02T04:36:11","modified_gmt":"2019-03-02T04:36:11","slug":"11-java-basic-operators","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/java\/11-java-basic-operators\/","title":{"rendered":"Java &#8211; Basic Operators"},"content":{"rendered":"<p>Basic Java Operators includes the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Arithmetic Operators<\/a><\/li>\n<li><a href=\"#t2\">Relational Operators<\/a><\/li>\n<li><a href=\"#t3\">Bitwise Operators<\/a><\/li>\n<li><a href=\"#t4\">Logical Operators<\/a><\/li>\n<li><a href=\"#t5\">Assignment Operators<\/a><\/li>\n<li><a href=\"#t6\">Miscellaneous Operators<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Arithmetic Operators<\/strong><\/h4>\n<p>You use arithmetic operators for math statements. Hence, they are used when you need to to some calculation. I provide a list of arithmetic operators below. Also, a brief explanation of each of them.<\/p>\n<p>Let X = 10 and Y = 20<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>Operator<\/th>\n<th>Brief description<\/th>\n<th>An example<\/th>\n<\/tr>\n<tr>\n<td>+ (Addition)<\/td>\n<td>Adds values on both sides of the operator.<\/td>\n<td>X + Y will give 30<\/td>\n<\/tr>\n<tr>\n<td>&#8211; (Subtraction)<\/td>\n<td>Subtracts right-side operand from left-side operand.<\/td>\n<td>X &#8211; Y will give -10<\/td>\n<\/tr>\n<tr>\n<td>* (Multiplication)<\/td>\n<td>Multiplies values on both side of the operator.<\/td>\n<td>X * Y will give 200<\/td>\n<\/tr>\n<tr>\n<td>\/ (Division)<\/td>\n<td>Divides left-side operand by right-side operand.<\/td>\n<td>Y \/ X will give 2<\/td>\n<\/tr>\n<tr>\n<td>% (Modulus)<\/td>\n<td>Divides left-side operand by right-side operand and produces the remainder.<\/td>\n<td>Y % X will give 0<\/td>\n<\/tr>\n<tr>\n<td>++ (Increment)<\/td>\n<td>Reduces the value of operand by 1.<\/td>\n<td>Y++ gives 21<\/td>\n<\/tr>\n<tr>\n<td>&#8212; (Decrement)<\/td>\n<td>Reduces the value of operand by 1.<\/td>\n<td>Y&#8211; gives 19<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.0: List of Arithmetic Operators<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Relational Operators<\/strong><\/h4>\n<p>You use relational operator to test for conditions. Therefore, you see them in conditional statements. Normally with if statements. Table 1.1 gives list of relational operators.<\/p>\n<p>Here X = 10 and Y = 20<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>Operator<\/th>\n<th>Brief description<\/th>\n<th>An example<\/th>\n<\/tr>\n<tr>\n<td>== (is equal to)<\/td>\n<td>Tests if the values of two operands are equal or not, if true then condition returns true.<\/td>\n<td>(X == Y) is not true.<\/td>\n<\/tr>\n<tr>\n<td>!= (is not equal to)<\/td>\n<td>Tests if the values of two operands are equal or not, if values are not equal then condition returns true.<\/td>\n<td>(X != Y) is true.<\/td>\n<\/tr>\n<tr>\n<td>&gt; (is greater than)<\/td>\n<td>Tests if the value of left operand is greater than the value of right operand, if true then condition returns true.<\/td>\n<td>(X &gt; Y) is not true.<\/td>\n<\/tr>\n<tr>\n<td>&lt; (is less than)<\/td>\n<td>Tests if the value of left operand is less than the value of right operand, if true then condition returns true.<\/td>\n<td>(X &lt; Y) is true.<\/td>\n<\/tr>\n<tr>\n<td>&gt;= (is greater than or equal to)<\/td>\n<td>Tests if the value of left operand is greater than or equal to the value of right operand, if true then condition returns true.<\/td>\n<td>(X &gt;= Y) is not true.<\/td>\n<\/tr>\n<tr>\n<td>&lt;= (is less than or equal to)<\/td>\n<td>Tests if the value of left operand is less than or equal to the value of right operand, if true then condition returns true.<\/td>\n<td>(X &lt;= Y) is true.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.1: List of Relational Operators<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Bitwise Operators<\/strong><\/h4>\n<p>We use Bitwise Operators to perform bit-by-bit operations. For that reason, the operands need to be binary numbers.<\/p>\n<p>Let x = 60 and y = 13. Converting them to binary, we then have:<\/p>\n<p>x = 00111100<\/p>\n<p>y = 00001101<\/p>\n<ul>\n<li>x&amp;y = 0000 1100<\/li>\n<li>x|y = 0011 1101<\/li>\n<li>x^y = 0011 0001<\/li>\n<li>~x\u00a0 = 1100 0011<\/li>\n<\/ul>\n<p>Table 1.3 provides a list of bitwise operators<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>Operator<\/th>\n<th>Brief description<\/th>\n<th>An example<\/th>\n<\/tr>\n<tr>\n<td>&amp; (bitwise and)<\/td>\n<td>Binary AND Operator produces a bit in the result if it exists in both operands.<\/td>\n<td>(x &amp; y) will yield 12 which is 0000 1100<\/td>\n<\/tr>\n<tr>\n<td>| (bitwise or)<\/td>\n<td>Binary OR Operator produces a bit if it exists in either operand.<\/td>\n<td>(x | y) will yield 61 which is 0011 1101<\/td>\n<\/tr>\n<tr>\n<td>^ (bitwise XOR)<\/td>\n<td>Binary XOR Operator produces the bit if it is set in one operand but not both.<\/td>\n<td>(x ^ y) will yield 49 which is 0011 0001<\/td>\n<\/tr>\n<tr>\n<td>~ (bitwise compliment)<\/td>\n<td>Binary Ones Complement Operator is a unary operator and has the effect of &#8216;switching&#8217; the\u00a0 bits.<\/td>\n<td>(~x ) will yield -61 which is 1100 0011 in 2&#8217;s complement form due to a signed binary number.<\/td>\n<\/tr>\n<tr>\n<td>&lt;&lt; (left shift)<\/td>\n<td>Binary Shift Left Operator. The left operands value is moved left by the number of bits given in the right operand.<\/td>\n<td>x &lt;&lt; 2 will yield 240 which is 1111 0000<\/td>\n<\/tr>\n<tr>\n<td>&gt;&gt; (right shift)<\/td>\n<td>Binary Shift Right Operator. The left operands value is moved right by the number of bits given int the right operand.<\/td>\n<td>A &gt;&gt; 2 will yield 15 which is 1111<\/td>\n<\/tr>\n<tr>\n<td>&gt;&gt;&gt; (zero right fill shift)<\/td>\n<td>Right Shift zero fill operator. The left operands value is moved right by the number of bits given int the right operand and shifted values are filled up with zeros.<\/td>\n<td>A &gt;&gt;&gt;2 will yield 15 which is 0000 1111<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.2: List of Bitwise Operators<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Logical Operators<\/strong><\/h4>\n<p>Logical operators are used for logical operations.<\/p>\n<p>Let X = true and Y = false, then the Table 1.3 give a list of logical operations on X and Y.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Brief description<\/th>\n<th>An example<\/th>\n<\/tr>\n<tr>\n<td>&amp;&amp; (logical and)<\/td>\n<td>This is the Logical AND operator. If both the operands are non-zero, then the condition results in true.<\/td>\n<td>(X &amp;&amp; Y) is false<\/td>\n<\/tr>\n<tr>\n<td>|| (logical or)<\/td>\n<td>This is the Logical OR Operator. If any of the two operands are non-zero, then the condition results in true.<\/td>\n<td>(X || Y) is true<\/td>\n<\/tr>\n<tr>\n<td>! (logical not)<\/td>\n<td>This is the Logical NOT Operator. You use it to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make false and vice versa<\/td>\n<td>!(X &amp;&amp; Y) is true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.3: List of Logical Operators<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Assignment Operators<\/strong><\/h4>\n<p>You use assignment operators to assign values to variables. Table 1.4 provides a list of assignment operators.<\/p>\n<p>They are like two-in-on operations. With exception of simple assignment.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Brief description<\/th>\n<th>An example<\/th>\n<\/tr>\n<tr>\n<td>=<\/td>\n<td>This is simple assignment operator. Assigns values from right side operands to left side operand.<\/td>\n<td>Z = X + Y will assign value of X + Y into Z<\/td>\n<\/tr>\n<tr>\n<td>+=<\/td>\n<td>Add operator together with assignment operator. It adds right operand to the left operand and assigns the result to left operand.<\/td>\n<td>Z += X is equivalent to Z = Z + X<\/td>\n<\/tr>\n<tr>\n<td>-=<\/td>\n<td>Subtract operator together with assignment operator. It subtracts right operand from the left operand and assigns the result to left operand.<\/td>\n<td>Z -= X is equivalent to Z = Z \u2013 X<\/td>\n<\/tr>\n<tr>\n<td>*=<\/td>\n<td>Multiply operator together with assignment operator. It multiplies right operand with the left operand and assigns the result to left operand.<\/td>\n<td>Z *= X is equivalent to Z = X * X<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">\/=<\/td>\n<td>Divide operator with assignment operator. It divides left operand with the right operand and assigns the result to left operand.<\/td>\n<td>Z \/= X is equivalent to X = Z \/ X<\/td>\n<\/tr>\n<tr>\n<td>%=<\/td>\n<td>Modulus operator together with assignment operator. It takes modulus using two operands and assigns the result to left operand.<\/td>\n<td>Z %= X is equivalent to Z = Z % X<\/td>\n<\/tr>\n<tr>\n<td>&lt;&lt;=<\/td>\n<td>Left shift AND assignment operator.<\/td>\n<td>Z &lt;&lt;= 2 is same as Z = Z &lt;&lt; 2<\/td>\n<\/tr>\n<tr>\n<td>&gt;&gt;=<\/td>\n<td>Right shift AND assignment operator.<\/td>\n<td>Z &gt;&gt;= 2 is same as Z = Z &gt;&gt; 2<\/td>\n<\/tr>\n<tr>\n<td>&amp;=<\/td>\n<td>Bitwise AND assignment operator.<\/td>\n<td>Z &amp;= 2 is same as Z = Z &amp; 2<\/td>\n<\/tr>\n<tr>\n<td>^=<\/td>\n<td>bitwise exclusive OR and assignment operator.<\/td>\n<td>Z ^= 2 is same as Z = Z ^ 2<\/td>\n<\/tr>\n<tr>\n<td>|=<\/td>\n<td>bitwise inclusive OR and assignment operator.<\/td>\n<td>Z |= 2 is same as Z = Z | 2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Listing 1.4: List of Assignment Operators<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Miscellaneous Operators<\/strong><\/h4>\n<p>Finally, we now consider two miscellaneous operators.<\/p>\n<ul>\n<li>Conditional Operator (?:)<\/li>\n<li>InstanceOf Operator (instanceof)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Conditional Operator<\/strong><\/p>\n<p>This is also called ternary operator. This is because it uses three operators. You can probably consider it a shortened if statement.<\/p>\n<p>You used it to conditionally assign a value to a variable. It evaluates a condition. If the condition is true, then assign a value, if false, assign alternate value.<\/p>\n<p>The syntax is:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">\tvariable <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span>condition<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">?<\/span> value_if_true <span style=\"color: #333333;\">:<\/span> value_if_false\r\n<\/pre>\n<\/div>\n<p>Take an example in Listing 1.0 below.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Tester<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String args<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #333399; font-weight: bold;\">int<\/span> x<span style=\"color: #333333;\">;<\/span>\r\n      <span style=\"color: #333399; font-weight: bold;\">int<\/span> y<span style=\"color: #333333;\">;<\/span>\r\n      x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">;<\/span>\r\n      y <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">?<\/span> <span style=\"color: #0000dd; font-weight: bold;\">100<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">200<\/span><span style=\"color: #333333;\">;<\/span>\r\n      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span> <span style=\"background-color: #fff0f0;\">\"x is : \"<\/span> <span style=\"color: #333333;\">+<\/span>  y <span style=\"color: #333333;\">);<\/span> <span style=\"color: #888888;\">\/\/ y is assigned 200<\/span>\r\n\r\n      y <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">(<\/span>x <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">?<\/span> <span style=\"color: #0000dd; font-weight: bold;\">100<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">200<\/span><span style=\"color: #333333;\">;<\/span>\r\n      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span> <span style=\"background-color: #fff0f0;\">\"x is : \"<\/span> <span style=\"color: #333333;\">+<\/span> y <span style=\"color: #333333;\">);<\/span> <span style=\"color: #888888;\">\/\/y is assigned 100<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<\/div>\n<p>Listing 1.0: Example of Conditional Operator<\/p>\n<p>&nbsp;<\/p>\n<p>I would recommend you try this yourself. Copy and paste the code in your IDE. Then run it to see the output.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>instanceof Operator<\/strong><\/p>\n<p>You use this operator only for object reference variables. It tests whether an object is an object of a given class. The syntax is<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">object_variable <span style=\"color: #008800; font-weight: bold;\">instanceof<\/span> class_name\r\n<\/pre>\n<\/div>\n<p>If the object variable is an instance of the class, it returns true. Otherwise, it returns false.<\/p>\n<p>&nbsp;<\/p>\n<p>Example is given in Listing 1.1 below<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Tester<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String args<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t      String website <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Java Tutorials\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n\t      <span style=\"color: #333399; font-weight: bold;\">boolean<\/span> answer <span style=\"color: #333333;\">=<\/span> website <span style=\"color: #008800; font-weight: bold;\">instanceof<\/span> String<span style=\"color: #333333;\">;<\/span>\r\n\t      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span> answer <span style=\"color: #333333;\">);<\/span>    \r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<\/div>\n<p>Listing 1.1: Example of instanceof<\/p>\n<p>&nbsp;<\/p>\n<p>The output of Listing 1.1 would be true. This is because the string website is an instance of the String wrapper class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basic Java Operators includes the following: Arithmetic Operators Relational Operators Bitwise Operators Logical Operators Assignment Operators Miscellaneous Operators &nbsp; 1. Arithmetic Operators You use arithmetic &hellip; <\/p>\n","protected":false},"author":395,"featured_media":65,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-64","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":68,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/64\/revisions\/68"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/65"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}