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

  • SEARCH
  • Home
  • 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 7766315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:23:02+00:00 2026-06-01T15:23:02+00:00

I have a question to which I have not found an answer. Let’s say

  • 0

I have a question to which I have not found an answer.
Let’s say we have in either java or c# the following code:

class Car {
   /* car stuff */
}

And then in Java

class Truck extends Car {
   /* truck stuff */
}

and C#

class Truck : Car {
   /* truck stuff again */
}

In C# the following works just fine:

List<Car> carList = new List<Car>();
//add some objects to the collection
foreach(Truck t in carList)
     //do stuff with only the Truck objects in the carList collection

This works because Truck is a subclass of Car which in simple terms means that each Truck is also a Car. The thing is though, that type checking is done and only Trucks are selected from carList.

If we try the same thing in Java:

List<Car> carList = new ArrayList<Car>();
//add some objects to the collection
for(Truck t : carList)
     //**PROBLEM**

Because of the code inside the enhanced loop, the code will not even compile. Instead we have to do something like this to get the same effect:

for(Car t : carList)
   if(t instanceof Car)
      //cast t to Truck and do truck stuff with it

It is the same idea which in C# works without any problem, but in Java you need extra code. Even the syntax is almost the same!
Is there a reason why it does not work in Java?

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-06-01T15:23:03+00:00Added an answer on June 1, 2026 at 3:23 pm

    The thing is though, that type checking is done and only Trucks are selected from carList.

    Nope, it’s not. If your list contains anything but Trucks, a runtime exception will occur in C#. Basically, in C#, the following

    foreach(Truck t in carList) {
        ...
    }
    

    behaves like

    foreach(object _t in carList) {
        Truck t = (Truck)_t;        // throws an InvalidCastException if _t is not a Truck
        ...
    }
    

    The Java variant, on the other hand, is type-safe: You have to make the cast and the type-checking yourself.


    So, why do Java and C# behave differently? This is my guess:

    C# had the foreach keyword before it had generics. Thus, there was no possibility to have a List<Car>. If C# had opted for the Java way of foreach, you’d have to write

    foreach(object _c in myArraylistContainingOnlyCars) {
        Car c = (Car)_c;
        // do something with c
    }
    

    which is annoying. On the other hand, the extended for loop and generics were introduced in Java in the same version (Java 5), so there was no need for automatic casts.

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

Sidebar

Related Questions

I guess I have an easy question, I have not found the right answer
Just wondering I have searched but I have not found an answer. My question
I have a very quick question to which I havn't found an answer for.
I have few Question for which I am not able to get a proper
I was reading this question (which you do not have to read because I
Hey guys i have a question which i have been looking for the answer
This question nags me for a while but I did not found complete answer
I've not found a answer to this question anywhere, but this seems like a
I have a question which is described below: What problems would arise for testing
I have a question which I think involves conditional entropy in the field of

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.