{"id":4246,"date":"2014-03-30T09:25:31","date_gmt":"2014-03-30T09:25:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/can-you-downcast-objects-in-java-without-declaring-a-new-variable-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:25:31","modified_gmt":"2014-03-30T09:25:31","slug":"can-you-downcast-objects-in-java-without-declaring-a-new-variable-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/can-you-downcast-objects-in-java-without-declaring-a-new-variable-collection-of-common-programming-errors\/","title":{"rendered":"can you downcast objects in java without declaring a new variable?-Collection of common programming errors"},"content":{"rendered":"<p>You cannot do it like this:<\/p>\n<pre><code>O xyz = new E();\nxyz = (E) xyz; \nxyx.someEMethod(); \/\/ compilation error\n<\/code><\/pre>\n<p>The reason is that typecasts of Java objects don&#8217;t actually change any values. Rather, they perform a type check against the object&#8217;s actual type.<\/p>\n<p>Your code checks that the value of <code>xyz<\/code> is an <code>E<\/code>, but then assigns the result of the typecast back to <code>xyz<\/code> (second statement), thereby upcasting it back to an <code>O<\/code> again.<\/p>\n<p>However, you can do this:<\/p>\n<pre><code>((E) xyx).someEMethod(); \/\/ fine\n<\/code><\/pre>\n<p>The parentheses around the typecast are essential, because the &#8216;.&#8217; operator has higher precedence than a typecast.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You cannot do it like this: O xyz = new E(); xyz = (E) xyz; xyx.someEMethod(); \/\/ compilation error The reason is that typecasts of Java objects don&#8217;t actually change any values. Rather, they perform a type check against the object&#8217;s actual type. Your code checks that the value of xyz is an E, but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4246","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4246"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4246\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}