{"id":1104,"date":"2022-02-07T19:05:41","date_gmt":"2022-02-07T19:05:41","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1104"},"modified":"2022-02-07T19:05:41","modified_gmt":"2022-02-07T19:05:41","slug":"numbers","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/07\/numbers\/","title":{"rendered":"Numbers"},"content":{"rendered":"\n<p>Normally, when we work with Numbers, we use primitive data types such as byte, int, long, double, etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int i = 5000;\nfloat gpa = 13.65f;\ndouble mask = 125;<\/code><\/pre>\n\n\n\n<p>However, in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides&nbsp;<strong>wrapper classes<\/strong>.<\/p>\n\n\n\n<p>All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/java\/images\/number_classes.jpg\" alt=\"Number Classes\"\/><\/figure>\n\n\n\n<p>The object of the wrapper class contains or wraps its respective primitive data type. Converting primitive data types into object is called&nbsp;<strong>boxing<\/strong>, and this is taken care by the compiler. Therefore, while using a wrapper class you just need to pass the value of the primitive data type to the constructor of the Wrapper class.<\/p>\n\n\n\n<p>And the Wrapper object will be converted back to a primitive data type, and this process is called unboxing. The&nbsp;<strong>Number<\/strong>&nbsp;class is part of the java.lang package.<\/p>\n\n\n\n<p>Following is an example of boxing and unboxing \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/rNELk1\" target=\"_blank\" rel=\"noreferrer noopener\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Test {\n\n   public static void main(String args&#91;]) {\n      Integer x = 5; \/\/ boxes int to an Integer object\n      x =  x + 10;   \/\/ unboxes the Integer to a int\n      System.out.println(x); \n   }\n}<\/code><\/pre>\n\n\n\n<p>This will produce the following result \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">15\n<\/pre>\n\n\n\n<p>When x is assigned an integer value, the compiler boxes the integer because x is integer object. Later, x is unboxed so that they can be added as an integer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"number-methods\">Number Methods<\/h2>\n\n\n\n<p>Following is the list of the instance methods that all the subclasses of the Number class implements \u2212<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Sr.No.<\/th><th>Method &amp; Description<\/th><\/tr><tr><td>1<\/td><td>xxxValue()Converts the value of\u00a0<em>this<\/em>\u00a0Number object to the xxx data type and returns it.<\/td><\/tr><tr><td>2<\/td><td>compareTo()Compares\u00a0<em>this<\/em>\u00a0Number object to the argument.<\/td><\/tr><tr><td>3<\/td><td>equals()Determines whether\u00a0<em>this<\/em>\u00a0number object is equal to the argument.<\/td><\/tr><tr><td>4<\/td><td>valueOf()Returns an Integer object holding the value of the specified primitive.<\/td><\/tr><tr><td>5<\/td><td>toString()Returns a String object representing the value of a specified int or Integer.<\/td><\/tr><tr><td>6<\/td><td>parseInt()This method is used to get the primitive data type of a certain String.<\/td><\/tr><tr><td>7<\/td><td>abs()Returns the absolute value of the argument.<\/td><\/tr><tr><td>8<\/td><td>ceil()Returns the smallest integer that is greater than or equal to the argument. Returned as a double.<\/td><\/tr><tr><td>9<\/td><td>floor()Returns the largest integer that is less than or equal to the argument. Returned as a double.<\/td><\/tr><tr><td>10<\/td><td>rint()Returns the integer that is closest in value to the argument. Returned as a double.<\/td><\/tr><tr><td>11<\/td><td>round()Returns the closest long or int, as indicated by the method&#8217;s return type to the argument.<\/td><\/tr><tr><td>12<\/td><td>min()Returns the smaller of the two arguments.<\/td><\/tr><tr><td>13<\/td><td>max()Returns the larger of the two arguments.<\/td><\/tr><tr><td>14<\/td><td>exp()Returns the base of the natural logarithms, e, to the power of the argument.<\/td><\/tr><tr><td>15<\/td><td>log()Returns the natural logarithm of the argument.<\/td><\/tr><tr><td>16<\/td><td>pow()Returns the value of the first argument raised to the power of the second argument.<\/td><\/tr><tr><td>17<\/td><td>sqrt()Returns the square root of the argument.<\/td><\/tr><tr><td>18<\/td><td>sin()Returns the sine of the specified double value.<\/td><\/tr><tr><td>19<\/td><td>cos()Returns the cosine of the specified double value.<\/td><\/tr><tr><td>20<\/td><td>tan()Returns the tangent of the specified double value.<\/td><\/tr><tr><td>21<\/td><td>asin()Returns the arcsine of the specified double value.<\/td><\/tr><tr><td>22<\/td><td>acos()Returns the arccosine of the specified double value.<\/td><\/tr><tr><td>23<\/td><td>atan()Returns the arctangent of the specified double value.<\/td><\/tr><tr><td>24<\/td><td>atan2()Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.<\/td><\/tr><tr><td>25<\/td><td>toDegrees()Converts the argument to degrees.<\/td><\/tr><tr><td>26<\/td><td>toRadians()Converts the argument to radians.<\/td><\/tr><tr><td>27<\/td><td>random()Returns a random number.<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Normally, when we work with Numbers, we use primitive data types such as byte, int, long, double, etc. Example However, in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides&nbsp;wrapper classes. All the wrapper classes (Integer, Long, Byte, Double, Float, Short) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[238],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1104"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1104"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1104\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}