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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:39:51+00:00 2026-06-14T02:39:51+00:00

This is the snippet of code in Java: Object ob = new int[2]; Now

  • 0

This is the snippet of code in Java:

Object ob = new int[2];

Now let’s say I want to initialize the array.
This
ob[0] = 5; will not work because ob is of the type of Object.
Casting is not working either:

(int[])ob[0] = 5;

By the way, (int[]ob)[0]= 5;
will cause syntax error.
So, how to assign values at run-time with no hard-coding (e.g. Object ob = new int[]{1,2}?
This is not a home-work, I am trying to understand Java. That is needed in order to prepare for Java certification.
Cheers

  • 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-14T02:39:52+00:00Added an answer on June 14, 2026 at 2:39 am

    You are trying to cast the value of ob[0] and not ob itself. You need to cast your ob first to int[] and then use it on index [0].

    ((int[])ob)[0] = 5;
    

    Note: – Parenthesis matters. But why would you like to do something like this?


    If you don’t want to hardcode values, and want to take it at runtime, then you should follow something mentioned by @HotLicks in comments.

    Object ob = new int[5];
    
    int[] tempArr = (int [])ob;
    
    for (int i = 0; i < tempArr.length; i++) {
        tempArr[i] = i;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just found in my java project this code snippet: List<IssueType> selectedIssueTypes = new
Is it possible to convert this java code snippet to php? public void testBalanceReturnsToZeroOnVending()
I have this JSP code snippet: <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=c%> <c:choose> <c:when test=${var1.properties[\Item Type\]
This snippet of code always parses the date into the current timezone, and not
if you run this snippet of code(put it in form1) in a fresh new
I have the following snippet of java code: final Class<?> junitCoreClass = AccessController.doPrivileged( new
Here is a small Java Code Snippet. ConfigData[] cd = new ConfigData[1]; cd[0] =
I have this snippet of java code. I am a noob in java.. Error
Considering the following code snippet: Object bar1 = new ... ; Object bar2 =
This is the snippet of Java code. class A{ public A() { } public

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.