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 4268148
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:59:45+00:00 2026-05-21T06:59:45+00:00

class Dims { public static void main(String[] args) { int[][] a = {{1,2,}, {3,4}};

  • 0
class Dims {
    public static void main(String[] args) {
        int[][] a = {{1,2,}, {3,4}};
        int[] b = (int[]) a[1];
        Object o1 = a;
        int[][] a2 = (int[][]) o1;
        int[] b2 = (int[]) o1;   // Line 7
        System.out.println(b[1]);
    } 
}  

I have a doubt in the above piece of code in Java.
Why does it give a Runtime Exception and not a compile time error at line number 7?

  • 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-05-21T06:59:46+00:00Added an answer on May 21, 2026 at 6:59 am

    Because o1 is an int[][], not an int[]. The RuntimeException you get is a ClassCastException, because the first is an array of int arrays, and the latter just an array of ints.

    You don’t get a compile time error, because o1 is defined as an Object. So at compile time, it could hold anything that is derived from object, which is in fact every Type in Java except the primitive types long, int, short, byte, char, double, float and boolean. So, at compile time it seems possible that the object might in fact be an int[].

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

Sidebar

Related Questions

class A { public static void main(String[] args) { A a = new A();
class ZiggyTest{ public static void main(String[] args) { String str = aaaaaaaaabb; String []
I have BaseClass List Public Class Package <XmlElement(OBJECT)> Public List As List(Of baseobj) Public
class foo { public: void set(const int a) {b=a;} private: int b; }; Here
class Base { public: virtual void foo() {} }; class Derived: public Base {
class TreeNode { public string Value { get; set;} public Collection<TreeNode> Nodes { get;
class Catalog { // string StationTitle; string StationLocation; public: string StationTitle; Catalog() { StationTitle
class B{ private: int a; } class D: public B{ private: int b; }
// class class MyClass { public: void doIt() const { cout << It works!
class Connection { public: typedef boost::shared_ptr<Connection> pointer; static pointer create(boost::asio::io_service& io_service){return pointer(new Connection(io_service));} explicit

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.