{"id":36900,"date":"2022-09-21T15:23:00","date_gmt":"2022-09-21T09:53:00","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/"},"modified":"2024-09-11T14:33:10","modified_gmt":"2024-09-11T09:03:10","slug":"type-casting-in-java","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/","title":{"rendered":"Java Type Casting"},"content":{"rendered":"\n<p>So, what do you do when you have to convert a data type into another data type when you are working on a piece of code? You guessed it right! There is a process of this kind of conversion, and it's called casting. To make it easier for you, we will talk about the type of casting in Java in this blog. Below are the topics that you will go through.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"type-casting-in-java-an-introduction\"><strong>Type Casting in Java - An Introduction<\/strong><\/h2>\n\n\n\n<p>Java programming language consists of diverse features that are efficiently handled by numerous data types. Unfortunately, we are more often required to convert one type of data to another.&nbsp;Here, the concept of Type casting in Java comes into play.<\/p>\n\n\n\n<p>Type Casting is a feature in Java using which the form or type of a variable or object is cast into some other kind of Object, and the process of conversion from one type to another is called Type Casting. Before diving into the typecasting process, let\u2019s understand data types in Java.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-title-link\">Java Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Learn Java the right way! Our course teaches you essential programming skills, from coding basics to complex projects, setting you up for success in the tech industry.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>16.05 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>3 Projects<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-button\">\n                Learn Java Programming\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-types-in-java\"><strong>Data Types in Java<\/strong><\/h2>\n\n\n\n<p>Java is a statically typed language, i.e., variables must be declared before its use. Java has two major categories of data:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"primitive-data-type\"><strong>Primitive Data Type<\/strong>: <\/h3>\n\n\n\n<p>It is the most fundamental data type. Java consists of 8 primitive data types:-<\/p>\n\n\n\n<p><strong>Boolean: <\/strong>It is used to store two types of values, i.e., true or false. This data type is commonly used as a flag in code logic. The default value of the Boolean data type is false.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nboolean flag=true;\n<\/pre><\/div>\n\n\n<p><strong>Byte: <\/strong>It can store 8-bit signed two\u2019s complement integer. The default value of a byte data type is 0. The range lies between -128 to 127<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nbyte num = 127;\n<\/pre><\/div>\n\n\n<p><strong>Char: <\/strong>This data type is used to store a single 16-bit Unicode character. It stores just one character in simple words, and the word Unicode is used because java uses the Unicode system, not the ASCII system. The size of this data type is 16bits (2 bytes). It is declared like below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nchar letter = &#039;a&#039;;\n<\/pre><\/div>\n\n\n<p><strong>Int: <\/strong>It is a data type that stores 32-bit (4 bytes) two\u2019s complement integer, i.e., its range lies within (-2^31 to 2^32 \u2013 1). It is declared using the <strong>int<\/strong> keyword followed by the name of the variable.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint number = 21;\n<\/pre><\/div>\n\n\n<p><strong>Short: <\/strong>Similar to \u2018int,\u2019 short is also used to store integer values but within 16-bit (2 bytes) signed two\u2019s complement. Its range lies within (-2^15 to 2^16-1). It is declared using the <strong>short<\/strong> keyword.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nshort numShort = 9; \n<\/pre><\/div>\n\n\n<p><strong>Long: <\/strong>It is 64 bit two\u2019s complement integer and its range lies within (-2^63 to2^64 - 1) i.e.<strong> ( <\/strong>-9,223,372,036,854,775,808 to 9,223,372,036,854,775,808). It is declared using the <strong>long <\/strong>keyword.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlong numLong = 543;\n<\/pre><\/div>\n\n\n<p><strong>Float: <\/strong>As the name indicates, it is a data type that holds data with more precision, i.e., floating-point numbers. It is a single-precision 32-bit (4 bytes) IEEE754 floating-point. It is declared using the <strong>float <\/strong>keyword.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nfloat decimalNum = 8.213245f\n<\/pre><\/div>\n\n\n<p><strong>Double: <\/strong>It is a double-precision 64-bit (8 bytes) IEEE754 floating point. It is declared using the <strong>double <\/strong>keyword.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndouble d = 73.546;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"non-primitive-data-type\"><strong>Non-primitive Data Type: <\/strong><\/h3>\n\n\n\n<p>Unlike primitive data types, which don\u2019t have any associated methods with them, non-primitive data types have associated methods.<strong> <\/strong>It refers to the Objects. It is also called Object data types or Reference Data Types. <strong>Example:&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>String<\/strong>: It is a sequence of characters.<\/p>\n\n\n\n<p>Example:<strong> String str = \u201cHello World!!\u201d ;<\/strong><\/p>\n\n\n\n<p><strong>Array: <\/strong>Collection of similar types of elements.<\/p>\n\n\n\n<p>Example:  <strong>String[] technology = [\u2018Java\u2019 , \u2018C\u2019 , \u2018Python\u2019]<\/strong><\/p>\n\n\n\n<p>Other examples of non-primitive data types are Class, Objects, and Interface.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"date-types-flow-chart\">Date Types Flow chart:<\/h2>\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM.png\"><img decoding=\"async\" width=\"1024\" height=\"678\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM-1024x678.png\" alt=\"\" class=\"wp-image-37039\" style=\"width:768px;height:509px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM-1024x678.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM-300x199.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM-768x509.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM-696x461.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM-1068x708.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM-634x420.png 634w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM-150x99.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-19-at-12.55.36-PM.png 1286w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"typecasting\"><strong>Typecasting<\/strong><\/h2>\n\n\n\n<p>As explained initially, typecasting is nothing but a way of changing the data type of a variable or an object from one form to another. Every programming language has its own rules and ways of type conversion. For example, an integer value can be converted to a floating-point value or a String, i.e., from numeric to textual representation.<\/p>\n\n\n\n<p>Typecasting in java programming is grouped into several broad categories:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-widening-typecasting-with-primitive-data-types\"><strong>1) Widening Typecasting with Primitive data types<\/strong><\/h3>\n\n\n\n<p>The process of conversion of a lower data type to a higher data type is known as Widening Typecasting. Java automatically performs this type of casting without any explicit code writing, which is why this type of casting is also known as Automatic typecasting.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>Important Note: During this conversion, no information is lost on the overall magnitude of the numeric value.<\/em><\/li>\n<\/ul>\n\n\n\n<p>To perform this conversion, two data types are supposed to be compatible with each other. 19 types of primitive conversion are possible in widening type casting:<\/p>\n\n\n\n<p><strong>a.) byte to short, byte to int, byte to long, byte to float, byte to double<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbyte b = 2;\nshort s = b;\nint i = b;\nlong 1 = b;\nfloat f = b;\ndouble d = b;\n<\/pre><\/div>\n\n\n<p><strong>b.) short to int, short to long, short to float, short to double<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nshort s = 3;\nint i=s;\nlong 1 = s;\nfloat f = s;\ndouble d = s;\n<\/pre><\/div>\n\n\n<p><strong>c.) char to int, char to long, char to float, char to double<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nchar c = \u2018d\u2019 ;\nint i = c ;\nlong l = c;\nfloat f = c;\ndouble d = c; \n<\/pre><\/div>\n\n\n<p><strong>d.) int to long, int to float, int to double<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint i = 32 ;\nlong l = i;\nfloat f = i;\ndouble d = i; \n<\/pre><\/div>\n\n\n<p><strong>e.) long to float, long to double<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nlong l = 78;\nfloat f = l;\ndouble d = l; \n<\/pre><\/div>\n\n\n<p><strong>f.) float to double<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nfloat decNum = 23.45f ;\ndouble d = decNum; \n\n<\/pre><\/div>\n\n\n<p><strong>Widening Type casting example in IDE:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class TypeCasting {\n\n\tpublic static void main(String&#x5B;] args) {\n\t\tbyte b = 5;\n\t\tshort s = b;\n\t\tint i = s ;\n\t\tlong l = s;\n\t\tfloat f = s;\n\t\tdouble d = s; \n\t\tSystem.out.println(&quot;Examples of Widening Type casting...!!&quot;);\n\t\tSystem.out.println(&quot;byte to short : &quot;+s);\n\t\tSystem.out.println(&quot;byte to int : &quot;+i);\n\t\tSystem.out.println(&quot;byte to long : &quot;+l);\n\t\tSystem.out.println(&quot;byte to float : &quot;+f);\n\t\tSystem.out.println(&quot;byte to double : &quot;+d);\n\t}\n}\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Examples of Widening Type casting\u2026!!<br>byte to short: 5<br>byte to int: 5<br>byte to long: 5<br>byte to float: 5.0<br>byte to double: 5.0<\/p>\n\n\n\n<p><strong>Fig: Widening Type Conversion flow<\/strong><\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-2.57.21-PM.png\"><img decoding=\"async\" width=\"918\" height=\"132\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-2.57.21-PM.png\" alt=\"\" class=\"wp-image-37177\" style=\"width:689px;height:99px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-2.57.21-PM.png 918w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-2.57.21-PM-300x43.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-2.57.21-PM-768x110.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-2.57.21-PM-696x100.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-2.57.21-PM-150x22.png 150w\" sizes=\"(max-width: 918px) 100vw, 918px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-widening-typecasting-with-objects-upcasting\"><strong>2) Widening Typecasting with Objects (Upcasting)<\/strong><\/h3>\n\n\n\n<p>Objects of a class can be cast into objects of another class if both classes are related to each other through the property of inheritance, i.e., one class is the parent class, and the other class is the child class.<\/p>\n\n\n\n<p>This type of casting superclass object (parent class) will hold the sub-class object\u2019s properties.<\/p>\n\n\n\n<p>Let\u2019s understand widening casting with objects using an example:&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass Animal{\n\t   protected String name;\n\t   protected int age;\n\t   public Animal(String name, int age){\n\t      this.name = name;\n\t      this.age = age;\n\t   }\n\t   public void animalInfo() {\n\t      System.out.printIn(&quot;Animal class info: &quot;);\n\t      System.out.printIn(&quot;Name: &quot;+this.name);\n\t      System.out.printIn(&quot;Age: &quot;+this.age);\n\t   }\n\t}\n\tpublic class Dog extends Animal {\n\t   public String color;\n\t   public Dog(String name, int age, String color){\n\t      super(name, age);\n\t      this.color = color;\n\t   }\n\t   public void dogInfo() {\n\t      System.out.printIn(&quot;Dog class: &quot;);\n\t      System.out.printIn(&quot;Name: &quot;+this.name);\n\t      System.out.printIn(&quot;Age: &quot;+this.age);\n\t      System.out.printIn(&quot;Color: &quot;+this.color);\n\t   }\n\t   public static void main(String&#x5B;] args) {\n\t\tDog dog = new Dog(&quot;Leo&quot;, 2, &quot;Brown&quot;);\n\t      Animal animal = new Animal(&quot;Casper&quot;, 3);\n\t      animal = dog; \/\/implicit casting Object of dog to Animal\n\t      animal.animalInfo();\n\t   }\n\t}\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Animal class info:<br>Name: Leo<br>Age: 2<\/p>\n\n\n\n<p>In the above code, the Animal class is called the parent class, and the Dog class is called the child class because the Dog class extends the Animal class, and the Dog class has acquired all the properties of the Animal class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the main() method, first, we have created an object of the Dog class using a <strong>new <\/strong>keyword, followed by the creation of the Animal class Object.<\/li>\n\n\n\n<li>In the second step, we have simply assigned the reference object of the Dog class to the animal class, i.e., <strong>animal = dog;<\/strong> this type of casting is known as implicit casting or widening or upcasting of objects.<\/li>\n\n\n\n<li>Widening takes place when a subclass object reference is assigned to a wider superclass object. Like, in the above example dog object was assigned to the Animal object.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-narrowing-typecasting-with-primitive-data-types\"><strong>3) Narrowing Typecasting with primitive data types<\/strong><\/h3>\n\n\n\n<p>The process of conversion of higher data type to lower data type is known as narrowing typecasting. It is not done automatically by Java but needs to be explicitly done by the programmer, which is why it is also called explicit typecasting.&nbsp;<\/p>\n\n\n\n<p>22 types of primitive conversion are possible in narrowing type casting:<\/p>\n\n\n\n<p><strong>a.) short to byte, short to char<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nshort input = 65 ;\nbyte b = (byte) input ;\nchar c = (char) input ; \/\/\n<\/pre><\/div>\n\n\n<p><strong>b.) char to byte, char to short<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nchar input = 65 ;\nbyte b = (byte) input ;\nshort s = (short) input ;\n<\/pre><\/div>\n\n\n<p><strong>c.) int to byte, int to short, int to char<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint input = 12 ;\nbyte b = (byte) input ;\nshort s = (short) input ;\nchar c = (char) input\n<\/pre><\/div>\n\n\n<p><strong>d.) long to byte, long to short, long to char, long to int<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nlong input = 12 ;\nbyte b = (byte) input ;\nshort s = (short) input ;\nchar c = (char) input ;\nint i = (int) input ;\n\n<\/pre><\/div>\n\n\n<p><strong>e.) float&nbsp;to&nbsp;byte, float&nbsp;to&nbsp;short, float&nbsp;to&nbsp;char, float&nbsp;to&nbsp;int, float&nbsp;to long<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nfloat input = 12.0f ;\nbyte b = (byte) input ;\nshort s = (short) input ;\nchar c = (char) input ;\nint i = (int) input ;\n<\/pre><\/div>\n\n\n<p><strong>f.) double&nbsp;to&nbsp;byte,&nbsp;double&nbsp;to&nbsp;short,&nbsp;double&nbsp;to&nbsp;char,&nbsp;double&nbsp;to&nbsp;int,&nbsp;double&nbsp;to&nbsp;long, double&nbsp;to&nbsp;float<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\ndouble input = 65.25 ;\nbyte b = (byte) input ;\nshort s = (short) input ;\nchar c = (char) input ;\nint i = (int) input ;\nlong l = (long) input ;\nfloat f = (float) input ;\n\n<\/pre><\/div>\n\n\n<p><strong>Narrowing Type casting example in IDE:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class TypeCasting {\n\n\tpublic static void main(String&#x5B;] args)\n{\n\t\tfloat input = 65.0f ;\n\t\tbyte b = (byte) input ;\n\t\tshort s = (short) input ;\n\t\tchar c = (char) input ;\n\t\tint i = (int) input ;\n\t\tSystem.out.printIn(&quot;Examples of Narrowing primitive Type casting...!!&quot;);\n\t\tSystem.out.printIn(&quot;float to short : &quot;+b);\n\t\tSystem.out.printIn(&quot;float to byte : &quot;+s);\n\t\tSystem.out.printIn(&quot;float to char : &quot;+c);\n\t\tSystem.out.printIn(&quot;float to int : &quot;+i);\t\n\n}\n}\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Examples of Narrowing primitive Type casting\u2026!!<br>float to short: 65<br>float to byte: 65<br>float to char: A<br>float to int: 65<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-3.18.58-PM.png\"><img decoding=\"async\" width=\"914\" height=\"122\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-3.18.58-PM.png\" alt=\"\" class=\"wp-image-37186\" style=\"width:686px;height:92px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-3.18.58-PM.png 914w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-3.18.58-PM-300x40.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-3.18.58-PM-768x103.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-3.18.58-PM-696x93.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Screenshot-2021-06-21-at-3.18.58-PM-150x20.png 150w\" sizes=\"(max-width: 914px) 100vw, 914px\" \/><\/figure>\n\n\n\n<p><strong>Fig: Narrowing Type casting conversion flow<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-narrowing-typecasting-with-objects-downcasting\"><strong>4) Narrowing Typecasting with Objects (Downcasting)<\/strong><\/h3>\n\n\n\n<p>Similar to widening typecasting, the object of one class can be narrowed cast into the object of another class when two classes hold the relationship of parent class and child class through inheritance. The class that inherits the properties of another class is called a child class or sub-class, while the inherited class is called a Parent class or superclass.<\/p>\n\n\n\n<p>But unlike widening typecasting, the programmer must explicitly use a cast operator to perform narrowcast. If we do not perform narrowcasting, the java compiler will throw a \u201ccompile-time error\u201d.<\/p>\n\n\n\n<p>Let\u2019s understand Narrowing type casting with an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass Animal{\n\t   protected String name;\n\t   protected int age;\n\t   public Animal(String name, int age){\n\t      this.name = name;\n\t      this.age = age;\n\t   }\n\t   public void animalInfo() {\n\t      System.out.printIn(&quot;Animal class info: &quot;);\n\t      System.out.printIn(&quot;Name: &quot;+this.name);\n\t      System.out.printIn(&quot;Age: &quot;+this.age);\n\t   }\n\t}\n\tpublic class Dog extends Animal {\n\t   public String color;\n\t   public Dog(String name, int age, String color){\n\t      super(name, age);\n\t      this.color = color;\n\t   }\n\t   public void dogInfo() {\n\t      System.out.printIn(&quot;Dog class: &quot;);\n\t      System.out.printIn(&quot;Name: &quot;+this.name);\n\t      System.out.printIn(&quot;Age: &quot;+this.age);\n\t      System.out.printIn(&quot;Color: &quot;+this.color);\n\t   }\n\t   public static void main(String&#x5B;] args) {\n\t\tAnimal animal = new Dog(&quot;Leo&quot;, 2, &quot;Black&quot;);\n\t      Dog dog = (Dog) animal; \/\/implicit casting Object of student to person\n\t      dog.animalInfo();\n\t      dog.dogInfo();\n\t   }\n\t}\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Animal class info:<br>Name: Leo<br>Age: 2<br>Dog class:<br>Name: Leo<br>Age: 2<br>Color: Black<\/p>\n\n\n\n<p>In the above code, the Animal class is the parent class, and the Dog class is the child class because the Dog class extends the Animal class, and the Dog class has acquired all the properties of the Animal class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the main() method, first, we have created an object of the Dog class using the reference of the parent class, i.e., Animal animal = new Dog(\u201cLeo\u201d, 2, \u201cBlack\u201d); otherwise, we\u2019ll encounter a runtime exception.<\/li>\n\n\n\n<li>In the second step, we have simply assigned the reference object of the Dog class to the animal class, i.e., <strong>Dog dog = (Dog) animal; <\/strong>this type of casting is known as explicit casting or narrowing or down-casting of objects.<\/li>\n\n\n\n<li>Narrowing typecasting occurs when a superclass object reference is narrow-casted and assigned to a narrower sub-class object. Like, in the above example, an <strong>animal<\/strong> object was assigned to a <strong>Dog<\/strong> object reference.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we studied data types in java and their syntax and characteristics, which helped build the foundational understanding of Type casting in Java. You can also take up a <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/java-programming\" target=\"_blank\" rel=\"noreferrer noopener\">Free java course<\/a> to help you understand more about the concepts. <\/p>\n\n\n\n<p>We also discussed widening or implicit type casting with primitive data types and reference objects and narrowing or explicit typecasting, which needs to be explicitly programmed with primitive data types and reference objects as well.<\/p>\n\n\n\n<p>We also explored type casting with various hands-on examples. Please feel free to explore more examples on your own to get a better understanding of the concept.<\/p>\n\n\n\n<p>Great Learning has collaborated with IIT Roorkee to offer an Advanced Certificate program in Full Stack Software Development. Do check out this program to ace your career and become a certified <a data-type=\"URL\" data-id=\"https:\/\/www.mygreatlearning.com\/advanced-certification-full-stack-software-development-iit-roorkee\" href=\"https:\/\/www.mygreatlearning.com\/advanced-certification-full-stack-software-development-iit-roorkee\" target=\"_blank\" rel=\"noreferrer noopener\">full-stack developer<\/a> today.<\/p>\n\n\n\n<p>Also, if you are preparing for Interviews,&nbsp;check out these&nbsp;<a data-type=\"URL\" data-id=\"https:\/\/www.mygreatlearning.com\/blog\/oops-interview-questions\/\" href=\"https:\/\/www.mygreatlearning.com\/blog\/oops-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">OOPS Interview questions<\/a>&nbsp;to ace it like a pro.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"frequently-asked-questions\"><strong>Frequently Asked Questions<\/strong><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1663754036819\"><strong class=\"schema-faq-question\">What is type casting with examples?<\/strong> <p class=\"schema-faq-answer\">An object can be converted from one data type to another using typecasting, also known as type conversion. It is employed in the programming language to guarantee that a function processes variables appropriately. Converting an integer to a string is an example of typecasting.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663754053799\"><strong class=\"schema-faq-question\">How many types of casting are there?<\/strong> <p class=\"schema-faq-answer\">There are primarily two types of casting, namely, widening type casting and narrowing type casting.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663754066988\"><strong class=\"schema-faq-question\">Why is typecasting needed in Java?<\/strong> <p class=\"schema-faq-answer\">Data can be type cast to be transformed from one data type to another. Type conversion or type coercion are other names for this data conversion procedure. Both reference and primitive data types can be cast in Java. Data cannot be modified with casting; only the data type can be altered.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663754154878\"><strong class=\"schema-faq-question\">What is the difference between type casting and type conversion?<\/strong> <p class=\"schema-faq-answer\">Type casting is a process by which a developer transforms one data type into another data type using the casting () operator. When compiling a program or piece of code, type conversion enables a compiler to transform one data type into another. Both compatible and incompatible data types can be utilized with it.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1663754207469\"><strong class=\"schema-faq-question\">Why is type casting useful?<\/strong> <p class=\"schema-faq-answer\">In a programming language, it is used to guarantee that a function processes the variables properly. The conversion of an integer into a string is an example of typecasting. If one of the numbers is saved as a string and the other as an integer, this might be used to contrast the two values.<\/p> <\/div> <\/div>\n\n\n\n<p>Engaging in the study of Java programming suggests a keen interest in the realm of software development. For those embarking upon this journey with aspirations towards a career in this field, it is recommended to explore the following pages in order to acquire a comprehensive understanding of the development career path:<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table><tbody><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/certificates\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses certificates<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/placements\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses placements<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/syllabus\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses syllabus<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/fees\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses fees<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/eligibility\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses eligibility<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>So, what do you do when you have to convert a data type into another data type when you are working on a piece of code? You guessed it right! There is a process of this kind of conversion, and it's called casting. To make it easier for you, we will talk about the type [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":37208,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36826],"content_type":[],"class_list":["post-36900","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-java"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Java Type Casting - All you need to know about type casting in Java<\/title>\n<meta name=\"description\" content=\"Type Casting in Java is a feature using which the form or type of a variable or object is cast into some other kind or Object.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Type Casting\" \/>\n<meta property=\"og:description\" content=\"Type Casting in Java is a feature using which the form or type of a variable or object is cast into some other kind or Object.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-21T09:53:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-11T09:03:10+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Java Type Casting\",\"datePublished\":\"2022-09-21T09:53:00+00:00\",\"dateModified\":\"2024-09-11T09:03:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/\"},\"wordCount\":1977,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/shutterstock_1933879448-1.jpg\",\"keywords\":[\"java\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/\",\"name\":\"Java Type Casting - All you need to know about type casting in Java\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/shutterstock_1933879448-1.jpg\",\"datePublished\":\"2022-09-21T09:53:00+00:00\",\"dateModified\":\"2024-09-11T09:03:10+00:00\",\"description\":\"Type Casting in Java is a feature using which the form or type of a variable or object is cast into some other kind or Object.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754036819\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754053799\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754066988\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754154878\"},{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754207469\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/shutterstock_1933879448-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/shutterstock_1933879448-1.jpg\",\"width\":1200,\"height\":675,\"caption\":\"typecasting in Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Java Type Casting\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754036819\",\"position\":1,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754036819\",\"name\":\"What is type casting with examples?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"An object can be converted from one data type to another using typecasting, also known as type conversion. It is employed in the programming language to guarantee that a function processes variables appropriately. Converting an integer to a string is an example of typecasting.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754053799\",\"position\":2,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754053799\",\"name\":\"How many types of casting are there?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"There are primarily two types of casting, namely, widening type casting and narrowing type casting.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754066988\",\"position\":3,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754066988\",\"name\":\"Why is typecasting needed in Java?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Data can be type cast to be transformed from one data type to another. Type conversion or type coercion are other names for this data conversion procedure. Both reference and primitive data types can be cast in Java. Data cannot be modified with casting; only the data type can be altered.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754154878\",\"position\":4,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754154878\",\"name\":\"What is the difference between type casting and type conversion?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Type casting is a process by which a developer transforms one data type into another data type using the casting () operator. When compiling a program or piece of code, type conversion enables a compiler to transform one data type into another. Both compatible and incompatible data types can be utilized with it.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754207469\",\"position\":5,\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/type-casting-in-java\\\/#faq-question-1663754207469\",\"name\":\"Why is type casting useful?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"In a programming language, it is used to guarantee that a function processes the variables properly. The conversion of an integer into a string is an example of typecasting. If one of the numbers is saved as a string and the other as an integer, this might be used to contrast the two values.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java Type Casting - All you need to know about type casting in Java","description":"Type Casting in Java is a feature using which the form or type of a variable or object is cast into some other kind or Object.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Java Type Casting","og_description":"Type Casting in Java is a feature using which the form or type of a variable or object is cast into some other kind or Object.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2022-09-21T09:53:00+00:00","article_modified_time":"2024-09-11T09:03:10+00:00","og_image":[{"width":1200,"height":675,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Java Type Casting","datePublished":"2022-09-21T09:53:00+00:00","dateModified":"2024-09-11T09:03:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/"},"wordCount":1977,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1.jpg","keywords":["java"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/","url":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/","name":"Java Type Casting - All you need to know about type casting in Java","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1.jpg","datePublished":"2022-09-21T09:53:00+00:00","dateModified":"2024-09-11T09:03:10+00:00","description":"Type Casting in Java is a feature using which the form or type of a variable or object is cast into some other kind or Object.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754036819"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754053799"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754066988"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754154878"},{"@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754207469"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1.jpg","width":1200,"height":675,"caption":"typecasting in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Java Type Casting"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754036819","position":1,"url":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754036819","name":"What is type casting with examples?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"An object can be converted from one data type to another using typecasting, also known as type conversion. It is employed in the programming language to guarantee that a function processes variables appropriately. Converting an integer to a string is an example of typecasting.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754053799","position":2,"url":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754053799","name":"How many types of casting are there?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"There are primarily two types of casting, namely, widening type casting and narrowing type casting.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754066988","position":3,"url":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754066988","name":"Why is typecasting needed in Java?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Data can be type cast to be transformed from one data type to another. Type conversion or type coercion are other names for this data conversion procedure. Both reference and primitive data types can be cast in Java. Data cannot be modified with casting; only the data type can be altered.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754154878","position":4,"url":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754154878","name":"What is the difference between type casting and type conversion?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Type casting is a process by which a developer transforms one data type into another data type using the casting () operator. When compiling a program or piece of code, type conversion enables a compiler to transform one data type into another. Both compatible and incompatible data types can be utilized with it.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754207469","position":5,"url":"https:\/\/www.mygreatlearning.com\/blog\/type-casting-in-java\/#faq-question-1663754207469","name":"Why is type casting useful?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"In a programming language, it is used to guarantee that a function processes the variables properly. The conversion of an integer into a string is an example of typecasting. If one of the numbers is saved as a string and the other as an integer, this might be used to contrast the two values.","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1.jpg",1200,675,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1-768x432.jpg",768,432,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1.jpg",1200,675,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1.jpg",1200,675,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1-640x675.jpg",640,675,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/shutterstock_1933879448-1-150x84.jpg",150,84,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"So, what do you do when you have to convert a data type into another data type when you are working on a piece of code? You guessed it right! There is a process of this kind of conversion, and it's called casting. To make it easier for you, we will talk about the type&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/36900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=36900"}],"version-history":[{"count":39,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/36900\/revisions"}],"predecessor-version":[{"id":112391,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/36900\/revisions\/112391"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/37208"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=36900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=36900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=36900"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=36900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}