My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Should you ever do explicit casts in modern Java?

Mark's photo
Mark
·Jul 27, 2018

The more the merrier

11%

Only exceptional cases

56%

Never!

22%

Idk

11%

9 votes · Closed

I feel like it's a big red flag, but I'm not sure it'd be safe to reject any code that has explicit type casting (throught e.g. linting + commit checks).

if (generalObject instanceof SpecificObject) {
    SpecificObject myObj = (SpecificObject) generalObject
}

No need to consider legacy code or badly written dependencies. Let's say we control all of the code.