Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 61251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:17:23+00:00 2026-05-10T18:17:23+00:00

Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I’d like

  • 0

Looking through some code I came across the following code

trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); 

and I’d like to know if casting this way has any advantages over

trTuDocPackTypdBd.update((TrTuDocPackTypeDto)packDto); 

I’ve asked the developer responsible and he said he used it because it was new (which doesn’t seem like a particularly good reason to me), but I’m intrigued when I would want to use the method.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:17:24+00:00Added an answer on May 10, 2026 at 6:17 pm

    These statements are not identical. The cast method is a normal method invocation (invokevirtual JVM instruction) while the other is a language construct (checkcast instruction). In the case you show above, you should use the second form: (TrTuDocPackTypeDto) packDto

    The cast method is used in reflective programming with generics, when you have a Class instance for some variable type. You could use it like this:

    public <T> Set<T> find(Class<T> clz, Filter criteria) {   List<?> raw = session.find(clz, criteria); /* A legacy, un-generic API. */   Set<T> safe = new HashSet<T>();   for (Object o : raw)      safe.add(clz.cast(o));   return safe; } 

    This gives you a safe way to avoid the incorrect alternative of simply casting a raw type to a generic type:

    /* DO NOT DO THIS! */ List raw = new ArrayList(); ... return (List<Widget>) raw; 

    The compiler will warn you, Unchecked cast from List to List<Widget>, meaning that in the ellipsis, someone could have added a Gadget to the raw list, which will eventually cause a ClassCastException when the caller iterates over the returned list of (supposed) Widget instances.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking through some code and came across a require 'etc' line. Of
I was looking through some code from the SDL library and came across a
I was looking through some Rails source code and came across # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb,
I was looking through Mozilla's JS in JS code and came across the snarf
I came accross the following code today and I didn't like it. It's fairly
I was looking through some code in a string escape library the other day
I was just looking through some information about Google's protocol buffers data interchange format.
In one of my current side projects, I am scanning through some text looking
I have been looking through Scott Guthrie's MVC tutorials and it seems like the
I was looking at some sample code on Jeff LaMarche's excellent blog when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.