{"id":52,"date":"2025-02-07T06:04:45","date_gmt":"2025-02-07T06:04:45","guid":{"rendered":"https:\/\/techkubo.com\/java\/?p=52"},"modified":"2025-06-07T20:52:32","modified_gmt":"2025-06-07T20:52:32","slug":"java-type-casting","status":"publish","type":"post","link":"https:\/\/techkubo.com\/java\/java-type-casting\/","title":{"rendered":"Java Type Casting"},"content":{"rendered":"\n<p>Type casting in Java is a process of assigning a value of one primitive data type to another. It is an essential concept in Java programming, enabling flexibility and precision in handling different data types. There are two main types of casting in Java: Widening Casting (automatic) and Narrowing Casting (manual).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Widening Casting<\/h2>\n\n\n\n<p>Widening casting happens automatically when converting a smaller data type to a larger one. This process is seamless and does not require explicit conversion by the programmer. The order of data types from smaller to larger is as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;JAVA&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">byte -&gt; short -&gt; char -&gt; int -&gt; long -&gt; float -&gt; double<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Example of Widening Casting<\/h5>\n\n\n\n<p>In this example, the integer <code>smallNum<\/code> is automatically cast to a double <code>largeNum<\/code> without any explicit conversion.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;JAVA&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">public class Main {\n    public static void main(String[] args) {\n        int smallNum = 9;\n        double largeNum = smallNum; \/\/ Automatic casting: int to double\n        System.out.println(smallNum); \/\/ Outputs 9\n        System.out.println(largeNum); \/\/ Outputs 9.0\n    }\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Narrowing Casting<\/h2>\n\n\n\n<p>Narrowing casting must be performed manually, as it involves converting a larger data type to a smaller one. This process requires the programmer to use explicit conversion by placing the target type in parentheses before the value.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;JAVA&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">double -&gt; float -&gt; long -&gt; int -&gt; char -&gt; short -&gt; byte<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Example of Narrowing Casting<\/h5>\n\n\n\n<p>In this example, the double <code>bigNum<\/code> is manually cast to an integer <code>smallNum<\/code> using explicit conversion.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;JAVA&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">public class Main {\n    public static void main(String[] args) {\n        double bigNum = 9.78;\n        int smallNum = (int) bigNum; \/\/ Manual casting: double to int\n        System.out.println(bigNum); \/\/ Outputs 9.78\n        System.out.println(smallNum); \/\/ Outputs 9\n    }\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Application of Type Casting<\/h2>\n\n\n\n<p>Type casting can be useful in various real-world scenarios. Consider a program that calculates the percentage of a user&#8217;s score in relation to the maximum score in a game. Type casting ensures the result is a floating-point number rather than an integer.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Example<\/h5>\n\n\n\n<p>In this example, the integer <code>userPoints<\/code> is cast to a float before performing the division, ensuring an accurate floating-point result.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;JAVA&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">public class GameScore {\n    public static void main(String[] args) {\n        int maxPoints = 500; \/\/ Maximum possible score in the game\n        int userPoints = 423; \/\/ User's actual score\n\n        \/\/ Calculate the user's percentage score\n        float percentageScore = (float) userPoints \/ maxPoints * 100;\n        System.out.println(&quot;User's percentage score is &quot; + percentageScore);\n    }\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Type casting in Java is a process of assigning a value of one primitive data type to another. It is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","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":"","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":"default","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":[1],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Type Casting - Java Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techkubo.com\/java\/java-type-casting\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Type Casting - Java Tutorial\" \/>\n<meta property=\"og:description\" content=\"Type casting in Java is a process of assigning a value of one primitive data type to another. It is [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techkubo.com\/java\/java-type-casting\/\" \/>\n<meta property=\"og:site_name\" content=\"Java Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-07T06:04:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-07T20:52:32+00:00\" \/>\n<meta name=\"author\" content=\"Manong\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Manong\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/techkubo.com\/java\/java-type-casting\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/techkubo.com\/java\/java-type-casting\/\"},\"author\":{\"name\":\"Manong\",\"@id\":\"https:\/\/techkubo.com\/java\/#\/schema\/person\/b4fa2f01fa4ff2a4e98276ce47115965\"},\"headline\":\"Java Type Casting\",\"datePublished\":\"2025-02-07T06:04:45+00:00\",\"dateModified\":\"2025-06-07T20:52:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/techkubo.com\/java\/java-type-casting\/\"},\"wordCount\":239,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/techkubo.com\/java\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/techkubo.com\/java\/java-type-casting\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/techkubo.com\/java\/java-type-casting\/\",\"url\":\"https:\/\/techkubo.com\/java\/java-type-casting\/\",\"name\":\"Java Type Casting - Java Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/techkubo.com\/java\/#website\"},\"datePublished\":\"2025-02-07T06:04:45+00:00\",\"dateModified\":\"2025-06-07T20:52:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/techkubo.com\/java\/java-type-casting\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/techkubo.com\/java\/java-type-casting\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/techkubo.com\/java\/java-type-casting\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/techkubo.com\/java\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Type Casting\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/techkubo.com\/java\/#website\",\"url\":\"https:\/\/techkubo.com\/java\/\",\"name\":\"Java Tutorial\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/techkubo.com\/java\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/techkubo.com\/java\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/techkubo.com\/java\/#organization\",\"name\":\"Java Tutorial\",\"url\":\"https:\/\/techkubo.com\/java\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/techkubo.com\/java\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/techkubo.com\/java\/wp-content\/uploads\/sites\/11\/2025\/04\/cropped-TechKubo-Logo-PNG.png\",\"contentUrl\":\"https:\/\/techkubo.com\/java\/wp-content\/uploads\/sites\/11\/2025\/04\/cropped-TechKubo-Logo-PNG.png\",\"width\":300,\"height\":220,\"caption\":\"Java Tutorial\"},\"image\":{\"@id\":\"https:\/\/techkubo.com\/java\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/techkubo.com\/java\/#\/schema\/person\/b4fa2f01fa4ff2a4e98276ce47115965\",\"name\":\"Manong\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/techkubo.com\/java\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21a7455736c21887b8fefe0935012d65?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21a7455736c21887b8fefe0935012d65?s=96&d=mm&r=g\",\"caption\":\"Manong\"},\"sameAs\":[\"https:\/\/techkubo.com\"],\"url\":\"https:\/\/techkubo.com\/java\/author\/manong\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Type Casting - Java Tutorial","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:\/\/techkubo.com\/java\/java-type-casting\/","og_locale":"en_US","og_type":"article","og_title":"Java Type Casting - Java Tutorial","og_description":"Type casting in Java is a process of assigning a value of one primitive data type to another. It is [&hellip;]","og_url":"https:\/\/techkubo.com\/java\/java-type-casting\/","og_site_name":"Java Tutorial","article_published_time":"2025-02-07T06:04:45+00:00","article_modified_time":"2025-06-07T20:52:32+00:00","author":"Manong","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Manong","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techkubo.com\/java\/java-type-casting\/#article","isPartOf":{"@id":"https:\/\/techkubo.com\/java\/java-type-casting\/"},"author":{"name":"Manong","@id":"https:\/\/techkubo.com\/java\/#\/schema\/person\/b4fa2f01fa4ff2a4e98276ce47115965"},"headline":"Java Type Casting","datePublished":"2025-02-07T06:04:45+00:00","dateModified":"2025-06-07T20:52:32+00:00","mainEntityOfPage":{"@id":"https:\/\/techkubo.com\/java\/java-type-casting\/"},"wordCount":239,"commentCount":0,"publisher":{"@id":"https:\/\/techkubo.com\/java\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techkubo.com\/java\/java-type-casting\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techkubo.com\/java\/java-type-casting\/","url":"https:\/\/techkubo.com\/java\/java-type-casting\/","name":"Java Type Casting - Java Tutorial","isPartOf":{"@id":"https:\/\/techkubo.com\/java\/#website"},"datePublished":"2025-02-07T06:04:45+00:00","dateModified":"2025-06-07T20:52:32+00:00","breadcrumb":{"@id":"https:\/\/techkubo.com\/java\/java-type-casting\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techkubo.com\/java\/java-type-casting\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/techkubo.com\/java\/java-type-casting\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techkubo.com\/java\/"},{"@type":"ListItem","position":2,"name":"Java Type Casting"}]},{"@type":"WebSite","@id":"https:\/\/techkubo.com\/java\/#website","url":"https:\/\/techkubo.com\/java\/","name":"Java Tutorial","description":"","publisher":{"@id":"https:\/\/techkubo.com\/java\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techkubo.com\/java\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techkubo.com\/java\/#organization","name":"Java Tutorial","url":"https:\/\/techkubo.com\/java\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techkubo.com\/java\/#\/schema\/logo\/image\/","url":"https:\/\/techkubo.com\/java\/wp-content\/uploads\/sites\/11\/2025\/04\/cropped-TechKubo-Logo-PNG.png","contentUrl":"https:\/\/techkubo.com\/java\/wp-content\/uploads\/sites\/11\/2025\/04\/cropped-TechKubo-Logo-PNG.png","width":300,"height":220,"caption":"Java Tutorial"},"image":{"@id":"https:\/\/techkubo.com\/java\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/techkubo.com\/java\/#\/schema\/person\/b4fa2f01fa4ff2a4e98276ce47115965","name":"Manong","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techkubo.com\/java\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21a7455736c21887b8fefe0935012d65?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21a7455736c21887b8fefe0935012d65?s=96&d=mm&r=g","caption":"Manong"},"sameAs":["https:\/\/techkubo.com"],"url":"https:\/\/techkubo.com\/java\/author\/manong\/"}]}},"_links":{"self":[{"href":"https:\/\/techkubo.com\/java\/wp-json\/wp\/v2\/posts\/52"}],"collection":[{"href":"https:\/\/techkubo.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techkubo.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techkubo.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techkubo.com\/java\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":2,"href":"https:\/\/techkubo.com\/java\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":172,"href":"https:\/\/techkubo.com\/java\/wp-json\/wp\/v2\/posts\/52\/revisions\/172"}],"wp:attachment":[{"href":"https:\/\/techkubo.com\/java\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techkubo.com\/java\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techkubo.com\/java\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}