{"id":1306,"date":"2022-02-27T06:30:30","date_gmt":"2022-02-27T06:30:30","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1306"},"modified":"2022-02-27T06:30:30","modified_gmt":"2022-02-27T06:30:30","slug":"type-casting","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/27\/type-casting\/","title":{"rendered":"Type Casting"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java Type Casting and its types with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">Before you learn about\u00a0<strong>Java Type Casting<\/strong>, make sure you know about\u00a0Java Data Types.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Type Casting<\/h2>\n\n\n\n<p>The process of converting the value of one data type (<code>int<\/code>,&nbsp;<code>float<\/code>,&nbsp;<code>double<\/code>, etc.) to another data type is known as typecasting.<\/p>\n\n\n\n<p>In Java, there are 13 types of type conversion. However, in this tutorial, we will only focus on the major 2 types.<\/p>\n\n\n\n<p>1. Widening Type Casting<\/p>\n\n\n\n<p>2. Narrowing Type Casting<\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"widening\">Widening Type Casting<\/h2>\n\n\n\n<p>In&nbsp;<strong>Widening Type Casting<\/strong>, Java automatically converts one data type to another data type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Converting int to double<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \/\/ create int type variable\n    int num = 10;\n    System.out.println(\"The integer value: \" + num);\n\n    \/\/ convert into double type\n    double data = num;\n    System.out.println(\"The double value: \" + data);\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The integer value: 10\nThe double value: 10.0\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we are assigning the&nbsp;<code>int<\/code>&nbsp;type variable named&nbsp;<var>num<\/var>&nbsp;to a&nbsp;<code>double<\/code>&nbsp;type variable named&nbsp;<var>data<\/var>.<\/p>\n\n\n\n<p>Here, the Java first converts the&nbsp;<code>int<\/code>&nbsp;type data into the&nbsp;<code>double<\/code>&nbsp;type. And then assign it to the&nbsp;<code>double<\/code>&nbsp;variable.<\/p>\n\n\n\n<p>In the case of&nbsp;<strong>Widening Type Casting<\/strong>, the lower data type (having smaller size) is converted into the higher data type (having larger size). Hence there is no loss in data. This is why this type of conversion happens automatically.<\/p>\n\n\n\n<p><strong>Note<\/strong>: This is also known as&nbsp;<strong>Implicit Type Casting<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"narrowing\">Narrowing Type Casting<\/h2>\n\n\n\n<p>In&nbsp;<strong>Narrowing Type Casting<\/strong>, we manually convert one data type into another using the parenthesis.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Converting double into an int<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \/\/ create double type variable\n    double num = 10.99;\n    System.out.println(\"The double value: \" + num);\n\n    \/\/ convert into int type\n    int data = (int)num;\n    System.out.println(\"The integer value: \" + data);\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The double value: 10.99\nThe integer value: 10\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we are assigning the&nbsp;<code>double<\/code>&nbsp;type variable named&nbsp;<var>num<\/var>&nbsp;to an&nbsp;<code>int<\/code>&nbsp;type variable named&nbsp;<var>data<\/var>.<\/p>\n\n\n\n<p>Notice the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int data = (int)num;\n<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<code>int<\/code>&nbsp;keyword inside the parenthesis indicates that that the&nbsp;<var>num<\/var>&nbsp;variable is converted into the&nbsp;<code>int<\/code>&nbsp;type.<\/p>\n\n\n\n<p>In the case of&nbsp;<strong>Narrowing Type Casting<\/strong>, the higher data types (having larger size) are converted into lower data types (having smaller size). Hence there is the loss of data. This is why this type of conversion does not happen automatically.<\/p>\n\n\n\n<p><strong>Note<\/strong>: This is also known as&nbsp;<strong>Explicit Type Casting<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Let&#8217;s see some of the examples of other type conversions in Java.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"int-to-string\">Example 1: Type conversion from int to String<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \/\/ create int type variable\n    int num = 10;\n    System.out.println(\"The integer value is: \" + num);\n\n    \/\/ converts int to string type\n    String data = String.valueOf(num);\n    System.out.println(\"The string value is: \" + data);\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The integer value is: 10\nThe string value is: 10\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, notice the line<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String data = String.valueOf(num);\n<\/code><\/pre>\n\n\n\n<p>Here, we have used the\u00a0<code>valueOf()<\/code>\u00a0method of the\u00a0Java String class\u00a0to convert the int type variable into a string.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"string-to-int\">Example 2: Type conversion from String to int<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \/\/ create string type variable\n    String data = \"10\";\n    System.out.println(\"The string value is: \" + data);\n\n    \/\/ convert string variable to int\n    int num = Integer.parseInt(data);\n    System.out.println(\"The integer value is: \" + num);\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The string value is: 10\nThe integer value is: 10\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, notice the line<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int num = Integer.parseInt(data);\n<\/code><\/pre>\n\n\n\n<p>Here, we have used the&nbsp;<code>parseInt()<\/code>&nbsp;method of the Java&nbsp;<code>Integer<\/code>&nbsp;class to convert a string type variable into an&nbsp;<code>int<\/code>&nbsp;variable.<\/p>\n\n\n\n<p><strong>Note<\/strong>: If the string variable cannot be converted into the integer variable then an exception named&nbsp;<code>NumberFormatException<\/code>&nbsp;occurs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java Type Casting and its types with the help of examples. Before you learn about\u00a0Java Type Casting, make sure you know about\u00a0Java Data Types. Type Casting The process of converting the value of one data type (int,&nbsp;float,&nbsp;double, etc.) to another data type is known as typecasting. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[597],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1306"}],"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=1306"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1306\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}