{"id":1046,"date":"2015-05-27T07:53:17","date_gmt":"2015-05-27T07:53:17","guid":{"rendered":"https:\/\/theoryofprogramming.wordpress.com\/?p=1046"},"modified":"2023-12-12T03:26:29","modified_gmt":"2023-12-12T03:26:29","slug":"java-tutorials-data-types-input-operators-in-java","status":"publish","type":"post","link":"https:\/\/theoryofcoding.com\/index.php\/2015\/05\/27\/java-tutorials-data-types-input-operators-in-java\/","title":{"rendered":"Data Types, Input and Operators in Java"},"content":{"rendered":"<p>Hello people&#8230;! This is a new post in Java Tutorials \u2013 Data Types, Input and Operators in Java. In my previous post, <strong><a title=\"Introduction\" href=\"http:\/\/theoryofprogramming.azurewebsites.net\/2015\/05\/26\/java-an-introduction\/\">Java Tutorials &#8211; An Introduction<\/a><\/strong>, I introduced you to the Java programming language. We didn&#8217;t do anything more than printing text on the terminal. Now, we will learn to write short programs which will involve a little computation.<\/p>\n<h3>\u00a0Data Types in Java<\/h3>\n<figure id=\"attachment_1098\" aria-describedby=\"caption-attachment-1098\" style=\"width: 773px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2015\/05\/datatype-e1437329695297.png?ssl=1\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1098 size-full\" style=\"margin-top: 10px; margin-bottom: 0;\" title=\"Data Types in Java\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2015\/05\/datatype-e1437329695297.png?resize=773%2C564&#038;ssl=1\" alt=\"Data Types in Java\" width=\"773\" height=\"564\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2015\/05\/datatype-e1437329695297.png?w=670&amp;ssl=1 670w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2015\/05\/datatype-e1437329695297.png?resize=300%2C219&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2015\/05\/datatype-e1437329695297.png?resize=550%2C400&amp;ssl=1 550w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2015\/05\/datatype-e1437329695297.png?resize=230%2C168&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2015\/05\/datatype-e1437329695297.png?resize=350%2C255&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2015\/05\/datatype-e1437329695297.png?resize=480%2C350&amp;ssl=1 480w\" sizes=\"auto, (max-width: 773px) 100vw, 773px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-1098\" class=\"wp-caption-text\">Data Types in Java<\/figcaption><\/figure>\n<p>These are the data types in Java. The primitive data types are much like the C++ data types. But, unlike C++, <strong>String is not a primitive data type. Strings in Java are objects<\/strong>. The Java library has the String class and we create their objects when we deal with strings. When it comes to numeric data types, Java does not have any notion of unsigned integers. So, the numeric data types in Java can always store positive and negative values. We will talk about the non-primitive data types later. For now, focus on getting comfy with the primitive data types.<\/p>\n<p>The sizes of the primitive data types are &#8211;<\/p>\n<ul>\n<li><b><span style=\"font-family: Consolas; color: blue;\">byte<\/span><\/b> &#8211; 1 Byte<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">short<\/span><\/b> &#8211; 2 Bytes<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">int<\/span><\/b> &#8211; 4 Bytes<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">long<\/span><\/b> &#8211; 8 Bytes<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">float<\/span><\/b> &#8211; 4 Bytes &#8211; up to 7 digits of precision<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">double<\/span><\/b> &#8211; 8 Bytes &#8211; up to 15 digits of precision<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">char<\/span><\/b> &#8211; 2 Bytes &#8211; But holds only up to 1 character<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">boolean<\/span><\/b> &#8211; JVM Dependant<\/li>\n<\/ul>\n<h3>Taking Input on Terminal<\/h3>\n<p>Before we start writing programs, we need to learn how to take input from the user, so that, we can keep testing our code for various inputs. There are several methods to take input from the user. We will look at the simplest one, by using the Scanner Class. Scanner is a class in the <strong>java.util<\/strong> package, or, library, a more familiar term. It has all the functions related to taking input from the user. So, to use those methods, we must first create an object of class Scanner.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nScanner scan = new Scanner(System.in);\n<\/pre>\n<p>This is how we create a an object of the class Scanner. An object is an instance (an occurrence) of a class. We use the <b><span style=\"font-family: Consolas; color: blue;\">new<\/span><\/b> keyword to create an object. We will have a detailed discussion about classes and objects later. The memory for the object is dynamically allocated by the JVM. And it looks like we are calling, some sort of a function, it is a <strong>Constructor<\/strong>&#8230;! It is used to instantiate an object. We will have a detailed discussion about Constructors later, but for now, I must ask you not to panic looking at that statement&#8230; \ud83d\ude1b &#8230;. We will learn everything..!<\/p>\n<p>On the right hand side, we have &#8220;Scanner scan&#8221;. The &#8220;Scanner&#8221; is to specify the data type of the variable\u00a0&#8220;scan&#8221;. This variable, &#8220;scan&#8221;, is called an <strong>Object Reference<\/strong>. It stores the address of the newly allocated memory (by using <b><span style=\"font-family: Consolas; color: blue;\">new<\/span><\/b>) for the object. We access this object by the Object Reference. But remember, this is not a pointer..! With pointers, we can add or subtract values, called Pointer Arithmetic, but we can do no such thing with Object References.<\/p>\n<p>Having created an object, we can access the methods that are provided by the Scanner Class, by using the &#8220;.&#8221; operator. Let&#8217;s look at an example program to make things clear. This program is an interest calculator, which takes the necessary information from the user &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport java.util.Scanner;\n\npublic class InterestCalculator {\n\n    public static void main(String&#x5B;] args) {\n        Scanner scan = new Scanner(System.in);\n\n        System.out.println(&amp;quot;Hello..! Enter your name -&amp;quot;);\n\n        String name = scan.nextLine();\n\n        System.out.println(&amp;quot;Hello &amp;quot; + name + &amp;quot;..!&amp;quot;\n                + &amp;quot; Please enter the principal amount -&amp;quot;);\n\n        int amount = scan.nextInt();\n\n        System.out.println(&amp;quot;Enter the rate percentage -&amp;quot;);\n\n        float rate = scan.nextFloat();\n\n        System.out.println(&amp;quot;Enter the time period -&amp;quot;);\n\n        int time = scan.nextInt();\n        float interest = amount * time * rate;\n\n        System.out.println(&amp;quot;Your interest amount is - &amp;quot;\n                + interest);\n    }\n\n}\n<\/pre>\n<p>This is the program. It demonstrates how we scan for the most basic data types, integer, string and float. The methods that scan the specific data types return that value, which was used to initialise the variables in the above program. The one new thing there is while printing, we can use the &#8220;+&#8221; operator to concatenate the strings. The other is the <b><span style=\"font-family: Consolas; color: blue;\">import<\/span><\/b> statement. It is used to include library files. You can think of it as <b><span style=\"font-family: Consolas; color: blue;\">#include<\/span><\/b> of C for now. It was used to include the Scanner class which is in <strong>java.util<\/strong>.<\/p>\n<p>Now, let&#8217;s look at the various operators in Java and later, we will have some really cool discussion about the data types. The operators are very much like the operators in C. So, if you know C well, you enjoy the dividends of your efforts here, otherwise, you pay for your sins&#8230;! \ud83d\ude1b<\/p>\n<h3>Arithmetic Operators in Java<\/h3>\n<table>\n<colgroup>\n<col style=\"width: 125px;\" \/>\n<col style=\"width: 150px;\" \/>\n<col style=\"width: 200px;\" \/> <\/colgroup>\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Meaning<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>Addition<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 10;\nint b = a + 10; \/\/ 20\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>&#8211;<\/td>\n<td>Subtraction<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 10;\nint b = a - 5; \/\/ 5\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>*<\/td>\n<td>Multiplication<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 10;\nint b = a * 7; \/\/ 70\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\/<\/td>\n<td>Division<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 50;\nint b = a \/ 5; \/\/ 10\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>%<\/td>\n<td>Remainder \/ Modulo<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 11;\nint b = a % 3; \/\/ 2\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>++<\/td>\n<td>Increment<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 10;\n++a; \/\/ 11\na++; \/\/ 12\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>&#8212;<\/td>\n<td>Decrement<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 10;\na--; \/\/ 9\n--a; \/\/ 8\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Relational Operators in Java<\/h3>\n<table>\n<colgroup>\n<col style=\"width: 125px;\" \/>\n<col style=\"width: 150px;\" \/>\n<col style=\"width: 200px;\" \/> <\/colgroup>\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Meaning<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td>&lt;<\/td>\n<td>less than<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 10, b = 20;\n\nif (a &amp;lt; b) {\n    \/\/ true\n}\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>&gt;<\/td>\n<td>greater than<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 10, b = 20;\n\nif (a &amp;gt; b) {\n    \/\/ false\n}\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>&lt;=<\/td>\n<td>less than or equal to<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 10;\n\nif (a &amp;lt;= 10) {\n    \/\/ true\n}\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>&gt;=<\/td>\n<td>greater than or equal to<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint b = 20;\n\nif (b &amp;gt;= 20) {\n    \/\/ true\n}\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>==<\/td>\n<td>is it equal to<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 10, b = 20;\n\nif (a == b) {\n    \/\/ false\n}\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>!=<\/td>\n<td>is not equal to<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 10, b = 20;\n\nif (a != b) {\n    \/\/ true\n}\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Logical Operators in Java<\/h3>\n<table>\n<colgroup>\n<col style=\"width: 125px;\" \/>\n<col style=\"width: 150px;\" \/>\n<col style=\"width: 200px;\" \/> <\/colgroup>\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Meaning<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td>&amp;&amp;<\/td>\n<td>AND<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nboolean a = true, b = false;\n\nif (a &amp;amp;&amp;amp; b) {\n    \/\/ false\n}\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>||<\/td>\n<td>OR<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nboolean a = true, b = false;\n\nif (a || b) {\n    \/\/ true\n}\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>!<\/td>\n<td>NOT<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nboolean a = true;\n\nif (!a) {\n    \/\/ false\n}\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Bitwise Operators in Java<\/h3>\n<table>\n<colgroup>\n<col style=\"width: 125px;\" \/>\n<col style=\"width: 150px;\" \/>\n<col style=\"width: 200px;\" \/> <\/colgroup>\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Meaning<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td>&amp;<\/td>\n<td>bitwise AND<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 2;\n\na = a &amp;amp; 1; \/\/ 0\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>|<\/td>\n<td>bitwise OR<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 2;\n\na = a | 1; \/\/ 3\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>^<\/td>\n<td>bitwise XOR<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 3;\n\na = a ^ 1; \/\/ 2\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>~<\/td>\n<td>bitwise NOT \/ complement<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint  a = 8;\n\na = ~a; \/\/ -9\n\/\/ 2&#039;s complement\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>&lt;&lt;<\/td>\n<td>left shift<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 8;\n\na = a &amp;lt;&amp;lt; 1; \/\/ 16\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>&gt;&gt;<\/td>\n<td>right shift<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 8;\n\na = a &amp;gt;&amp;gt; 1; \/\/ 4\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>&gt;&gt;&gt;<\/td>\n<td>right shift with zero fill<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 8;\n\na = a &amp;gt;&amp;gt;&amp;gt; 1; \/\/ 4\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Shorthand Operators<\/h3>\n<table>\n<colgroup>\n<col style=\"width: 125px;\" \/>\n<col style=\"width: 150px;\" \/>\n<col style=\"width: 200px;\" \/> <\/colgroup>\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Meaning<\/th>\n<th>Example<\/th>\n<\/tr>\n<tr>\n<td>x += 1;<\/td>\n<td>x = x + 1;<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 2;\n\na += 1; \/\/ 3\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>x -= 1;<\/td>\n<td>x = x &#8211; 1;<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 8;\n\na -= 1; \/\/ 7\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>x *= 2;<\/td>\n<td>x = x * 2;<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 8;\n\na *= 2; \/\/ 16\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>x \/= 2;<\/td>\n<td>x = x \/ 2;<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 8;\n\na \/= 2; \/\/ 4\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>x %= 2;<\/td>\n<td>x = x % 2;<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint a = 8;\n\na %= 2; \/\/ 0\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Operator Precedence in Java<\/h3>\n<table width=\"404\">\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Meaning<\/th>\n<\/tr>\n<tr>\n<td>.<\/td>\n<td>Member Selection<\/td>\n<\/tr>\n<tr>\n<td>function()<\/td>\n<td>Function Call<\/td>\n<\/tr>\n<tr>\n<td>arr[]<\/td>\n<td>Array&#8217;s\u00a0Random\u00a0Access<\/td>\n<\/tr>\n<tr>\n<td>-var<\/td>\n<td>Unary Minus<\/td>\n<\/tr>\n<tr>\n<td>var++<\/td>\n<td>Postfix Increment<\/td>\n<\/tr>\n<tr>\n<td>var&#8211;<\/td>\n<td>Postfix Decrement<\/td>\n<\/tr>\n<tr>\n<td>++var<\/td>\n<td>Prefix Increment<\/td>\n<\/tr>\n<tr>\n<td>&#8211;var<\/td>\n<td>Prefix Decrement<\/td>\n<\/tr>\n<tr>\n<td>!<\/td>\n<td>Logical Negation<\/td>\n<\/tr>\n<tr>\n<td>~<\/td>\n<td>Complement<\/td>\n<\/tr>\n<tr>\n<td>(data_type)<\/td>\n<td>Type Casting<\/td>\n<\/tr>\n<tr>\n<td>*<\/td>\n<td>Multiplication<\/td>\n<\/tr>\n<tr>\n<td>\/<\/td>\n<td>Division<\/td>\n<\/tr>\n<tr>\n<td>%<\/td>\n<td>Modulo Division<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>Addition<\/td>\n<\/tr>\n<tr>\n<td>&#8211;<\/td>\n<td>Subtraction<\/td>\n<\/tr>\n<tr>\n<td>&lt;&lt;<\/td>\n<td>Left Shift<\/td>\n<\/tr>\n<tr>\n<td>&gt;&gt;<\/td>\n<td>Right Shift<\/td>\n<\/tr>\n<tr>\n<td>&gt;&gt;&gt;<\/td>\n<td>Right Shift with Zero\u00a0Fill<\/td>\n<\/tr>\n<tr>\n<td>&lt;<\/td>\n<td>Less than<\/td>\n<\/tr>\n<tr>\n<td>&lt;=<\/td>\n<td>Less than or equal to<\/td>\n<\/tr>\n<tr>\n<td>&gt;<\/td>\n<td>Greater than<\/td>\n<\/tr>\n<tr>\n<td>&gt;=<\/td>\n<td>Greater than or equal to<\/td>\n<\/tr>\n<tr>\n<td>instanceof<\/td>\n<td>Object-Class comparison<\/td>\n<\/tr>\n<tr>\n<td>==<\/td>\n<td>Is it equal to<\/td>\n<\/tr>\n<tr>\n<td>!=<\/td>\n<td>Is not equal to<\/td>\n<\/tr>\n<tr>\n<td>&amp;<\/td>\n<td>Bitwise AND<\/td>\n<\/tr>\n<tr>\n<td>^<\/td>\n<td>Bitwise XOR<\/td>\n<\/tr>\n<tr>\n<td>|<\/td>\n<td>Bitwise OR<\/td>\n<\/tr>\n<tr>\n<td>&amp;&amp;<\/td>\n<td>Logical AND<\/td>\n<\/tr>\n<tr>\n<td>||<\/td>\n<td>Logical OR<\/td>\n<\/tr>\n<tr>\n<td>(condition) ? true : false<\/td>\n<td>Ternary Operator<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>These are the precedence levels. We still haven&#8217;t discussed a few operators, such as <b><span style=\"font-family: Consolas; color: blue;\">instanceof<\/span><\/b>. We will discuss them soon when we talk more about classes. You need not mug up the whole hierarchy, but if you have an idea of the hierarchy between the arithmetic operators, relational operators and logical operators, it&#8217;s enough, because those are the most frequently used ones. But if you ever need, you can always come here to lookup the precedence levels of various operators.<\/p>\n<h3>Automatic Type Conversions<\/h3>\n<p>Java employs &#8220;<strong>widening type conversion<\/strong>&#8221; between the primitive data types. Which means that if a binary operator has operands of two different data types, the data type of the result will be the data type of the bigger operator (which occupies more memory). And an operation between a floating point operand \u00a0and an integer type operand always yield\u00a0a floating point result. This is because Java considers integer-to-floating-point to be a widening conversion. The widening conversion is safe and integer-to-floating-point conversion preserves accuracy. All through, we talked about the data type of the resulting expression. The value which is stored may ultimately depend on the LHS variable.<\/p>\n<p>This is the automatic type conversion in Java. The widening type conversion is implicit, that is, it is done by Java automatically. But, sometimes we may want to do a narrowing type conversion. We can do this, but we must ask Java explicitly for it, by using the type casting syntax.<\/p>\n<p style=\"text-align: center;\"><strong>LHS \u00a0= (data_type) (expression)<\/strong><\/p>\n<p>It is a good practice to enclose the expression in parenthesis too, so that we can avoid logical errors, because type casting has a very\u00a0high precedence.<\/p>\n<p>Here are some examples &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class CircleArea {\n\n    public static void main(String&#x5B;] args) {\n        float radius = 49f \/ 13;   \/\/ 3.7692308\n\n        System.out.println(radius * Math.PI);\n        \/\/ 11.841387924765852\n\n        int area = (int) (2 * radius * Math.PI);\n\n        System.out.println(area);   \/\/ 23\n    }\n\n}\n<\/pre>\n<p>The points to be noted in the program are &#8211;<\/p>\n<ul>\n<li>Math.PI is of type <b><span style=\"font-family: Consolas; color: blue;\">double<\/span><\/b> which has the value of\u00a0\u03a0. It is multiplied with a <b><span style=\"font-family: Consolas; color: blue;\">float<\/span><\/b> number, giving a <b><span style=\"font-family: Consolas; color: blue;\">double<\/span><\/b>, demonstrating the widening type conversion.<\/li>\n<li>If you don&#8217;t enclose the expression within parenthesis, it&#8217;s a compilation error, saying, &#8220;incompatible types: possible lossy conversion from double to int&#8221;. This is because of the precedence of the type casting.<\/li>\n<\/ul>\n<h3>Default Numeric Data Types<\/h3>\n<p>We must take a small note on how Java treats numeric data types. There are only two things to keep in mind &#8211;<\/p>\n<p>Firstly, <strong>whenever Java sees a constant floating point number in an expression, it treats its data type to be <span style=\"font-family: Consolas; color: blue;\">double<\/span>, not <span style=\"font-family: Consolas; color: blue;\">float<\/span><\/strong>. So, the declaration &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nfloat f = 3.14;\n<\/pre>\n<p>Gives a compilation error, saying, &#8220;incompatible types: possible lossy conversion from double to float&#8221;. So, what Java is doing here is &#8211;<\/p>\n<ul>\n<li>It treats the floating point constant, 3.14, as a <b><span style=\"font-family: Consolas; color: blue;\">double<\/span><\/b> value.<\/li>\n<li>It tries to convert the value to what is expected, which is <b><span style=\"font-family: Consolas; color: blue;\">float<\/span><\/b>.<\/li>\n<li>If it feels that the conversion may cause a loss of information, it gives a compilation error, saying &#8220;incompatible types: possible lossy conversion from ____ to _____&#8221;.<\/li>\n<li>If it is so, we must explicitly type cast the value to the required data-type.<\/li>\n<\/ul>\n<p>Remember, there can possibly be a loss of conversion only, if we are assigning a bigger data type (here <b><span style=\"font-family: Consolas; color: blue;\">double<\/span><\/b>), to a smaller data type (<b><span style=\"font-family: Consolas; color: blue;\">float<\/span><\/b>). So, the remedy for initialising <b><span style=\"font-family: Consolas; color: blue;\">float<\/span><\/b> variables is &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nfloat num = (float) 3.14;\nfloat var = 3.14f;\n<\/pre>\n<p>The first is type casting, and the second is giving a suffix &#8216;<strong>f<\/strong>&#8216;, to tell Java to treat it as a <b><span style=\"font-family: Consolas; color: blue;\">float<\/span><\/b> number. Be careful and keep these concepts in mind because we will use them again very shortly.<br \/>\nSecondly, <strong>whenever Java sees an integer constant, it treats its data type to be <span style=\"font-family: Consolas; color: blue;\">int<\/span><\/strong>. So, this declaration will cause an error &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nbyte b = 12345;\n<\/pre>\n<p>Therefore, we must use type casting &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nbyte b = (byte) 12345;\n<\/pre>\n<p>In that case, what value is stored in the variable <em>b<\/em>..? We know that 12345\u00a0won&#8217;t fit in a <b><span style=\"font-family: Consolas; color: blue;\">byte<\/span><\/b>. In such a situation, <strong>the excess bits\u00a0are truncated<\/strong>..! So, the variable <em>b<\/em> would take only the first 8 bytes of 12345 (0011000000111001), that is 00111001&#8230; From the right..! \ud83d\ude09 &#8230; So, the variable <em>b<\/em> has the value 57. And don&#8217;t forget that the last bit is used for the sign..!<\/p>\n<p>The same goes when we want to initialise <b><span style=\"font-family: Consolas; color: blue;\">long<\/span><\/b> variables. The following initialisation won&#8217;t work &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nlong l = -123456789123456;\n<\/pre>\n<p>But we all know that the given constant is well within the range of <b><span style=\"font-family: Consolas; color: blue;\">long<\/span><\/b>. The compiler gives an error saying, &#8220;integer number too large: -123456789123456&#8221;. And we know why..! Java is treating the integer constant&#8217;s data type to be <b><span style=\"font-family: Consolas; color: blue;\">int<\/span><\/b>..! And what&#8217;s worse, type casting won&#8217;t work here..! Why would it work..? We can&#8217;t even store the value in the first place&#8230;! So, how on earth could we initialize <b><span style=\"font-family: Consolas; color: blue;\">double<\/span><\/b> variables&#8230;? We can do, by giving a suffix, &#8216;l&#8217; or &#8216;L&#8217; to tell Java to treat it as a <b><span style=\"font-family: Consolas; color: blue;\">long<\/span><\/b>\u00a0value &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nlong l = -123456789123456l;\n<\/pre>\n<p>This is a little advanced discussion about the data types in Java. I can perfectly understand if you don&#8217;t follow me on this topic&#8230; \ud83d\ude42 &#8230; But I wan&#8217;t you to remember how to initialize <b><span style=\"font-family: Consolas; color: blue;\">float<\/span><\/b>, <b><span style=\"font-family: Consolas; color: blue;\">byte<\/span><\/b> and <b><span style=\"font-family: Consolas; color: blue;\">long<\/span><\/b> variables.<\/p>\n<h3>What Compliers\u00a0know and don&#8217;t&#8230;!<\/h3>\n<p>This too, is a little advanced discussion, feel free to skip this&#8230;! \ud83d\ude09 &#8230;. I hope you got an idea about the problem we faced with &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nbyte b = 12345;\n<\/pre>\n<p>Now, what if we do something like &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nbyte b = 57;\n<\/pre>\n<p>Does this too cause a compilation error..? No..! This is because, Java knows that 57 is well within the range of <b><span style=\"font-family: Consolas; color: blue;\">byte<\/span><\/b>. So there is no &#8220;lossy conversion&#8221; here. So, Java does not complaint&#8230;! Feeling good..? Check this out..!<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint num = 57;\nbyte b = num;\n<\/pre>\n<p>This gives a compilation error..! Regarding assigning the value of <em>num<\/em> to <em>b<\/em>, the compiler repeats its dull old story, &#8220;incompatible types: possible lossy conversion from int to byte&#8221;. Now what is this&#8230;? We know that logically, we are trying to assign 57 to <em>b<\/em>, which seems to be perfectly all right..! So, what&#8217;s the compiler whining about&#8230;?<br \/>\nThe thing is that &#8211; &#8220;<strong>A compiler knows about the data types only. It knows nothing about the values<\/strong>&#8220;. This is because values are a runtime stuff. So, when the compiler comes to Line 2, it sees that we are assigning a variable of type <b><span style=\"font-family: Consolas; color: blue;\">int<\/span><\/b> to <b><span style=\"font-family: Consolas; color: blue;\">byte<\/span><\/b>. It knows that the range of <b><span style=\"font-family: Consolas; color: blue;\">int<\/span><\/b> is more than that of <b><span style=\"font-family: Consolas; color: blue;\">byte<\/span><\/b>, so there is a possibility of information loss during the conversion. This is why the compiler gives an error.<br \/>\nBut then, the 57 being assigned to <em>num<\/em> on Line 1.. isn&#8217;t that a value..? Well, yes, we are assigning a value, but what we are assigning is a constant. A constant is not a runtime stuff, it does not change, but a variable could change. I could to hell a lot of computation between Line 1 and 2. So, Java doesn&#8217;t want to take a chance, so it gives a compilation error, which can be resolved by explicit type casting.<\/p>\n<p>So, golden rule here is that the compiler doesn&#8217;t know about the values. This is a very very important one..! This can be used in many places and is really helpful in solving problems, which would otherwise baffle other programmers..! So, trust me, you&#8217;ve just added something really powerful in your arsenal..! \ud83d\ude09 &#8230; And, you are a real genius if you already know this..! \ud83d\ude1b<\/p>\n<h3>Summary<\/h3>\n<ul>\n<li>The primitive data types in Java are divided to numeric and non-numeric. The numeric data types are &#8211;\n<ul>\n<li><b><span style=\"font-family: Consolas; color: blue;\">byte<\/span><\/b> &#8211; 1 Byte<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">short<\/span><\/b> &#8211; 2 Bytes<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">int<\/span><\/b> &#8211; 4 Bytes<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">long<\/span><\/b> &#8211; 8 Bytes<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">float<\/span><\/b> &#8211; 4 Bytes &#8211; up to 7 digits of precision<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">double<\/span><\/b> &#8211; 8 Bytes &#8211; up to 15 digits of precision<\/li>\n<\/ul>\n<p>The non-numeric data types are &#8211;<\/p>\n<ul>\n<li><b><span style=\"font-family: Consolas; color: blue;\">char<\/span><\/b> &#8211; 2 Bytes &#8211; But holds only up to 1 character<\/li>\n<li><b><span style=\"font-family: Consolas; color: blue;\">boolean<\/span><\/b> &#8211; 1 Bit<\/li>\n<\/ul>\n<\/li>\n<li>We take input from the user using the functions in Scanner Class. Some of the useful functions are (if &#8220;scan&#8221; is the Object Reference) &#8211;\n<ul>\n<li>scan.nextLine() &#8211; to scan a String<\/li>\n<li>scan.nextInt() &#8211; to scan a <b><span style=\"font-family: Consolas; color: blue;\">int<\/span><\/b>.<\/li>\n<li>scan.nextLong() &#8211; to scan a <b><span style=\"font-family: Consolas; color: blue;\">long<\/span><\/b>.<\/li>\n<li>scan.nextFloat() &#8211; to scan a <b><span style=\"font-family: Consolas; color: blue;\">float<\/span><\/b>.<\/li>\n<li>scan.nextDouble() &#8211; to scan a <b><span style=\"font-family: Consolas; color: blue;\">double<\/span><\/b>.<\/li>\n<\/ul>\n<\/li>\n<li>The operators in Java are much like the operators in C.<\/li>\n<li>Java employs widening automatic type conversion.<\/li>\n<li>We initialize the respective data types as follows &#8211;\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nfloat var1 = 5.1236f;\nfloat var2 = (float) 5.1236;\nlong var3 = 12346876314l;\nbyte var4 = (byte) 12345;\n<\/pre>\n<\/li>\n<li>A compiler knows about the data types only. It knows nothing about the values.<\/li>\n<\/ul>\n<h3>Practice<\/h3>\n<ul>\n<li>There is really nothing challenging except for the discussions we had on the data types. I don&#8217;t want to give problem which asks you to lookup into the table of precedence levels. It is monotonous. So, it only makes sense to work a little on the data types. So, try to make a table between data types and fill out the table with either &#8220;implicit&#8221; or &#8220;explicit&#8221; or &#8220;not possible&#8221; according to the type of conversion required between those two data types. Try to include all types of primitive data types.<\/li>\n<\/ul>\n<p>This was to get you started with the data types in Java. It is very much like C. So, I hope you were comfortable with this post. Feel free to comment your doubts..! We will cover more topics in another post&#8230; Till then&#8230; Keep practising&#8230;! Happy Coding&#8230;! \ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello people&#8230;! This is a new post in Java Tutorials \u2013 Data Types, Input and Operators in Java. In my previous post, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[24,62,63,64,67,98],"class_list":["post-1046","post","type-post","status-publish","format-standard","hentry","category-java","tag-automatic-type-conversion","tag-java","tag-java-compiler","tag-java-data-types","tag-java-tutorials","tag-scanner-class"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/comments?post=1046"}],"version-history":[{"count":4,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1046\/revisions"}],"predecessor-version":[{"id":3952,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1046\/revisions\/3952"}],"wp:attachment":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/media?parent=1046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/categories?post=1046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/tags?post=1046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}