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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:25:17+00:00 2026-06-09T15:25:17+00:00

I wanted to cast a series of objects in an array to different types

  • 0

I wanted to cast a series of objects in an array to different types based upon user preferences.

Is it possible to reference primitive-types from an array for casting?

public void MakeByteBuffer(float x, float y, float z) {
    xyz[0] = x;
    xyz[1] = y;
    xyz[2] = z;
    byteBufferSend.rewind();
    for (int i = 0; i < whatToSendVarArray.length; i++) {
        switch (whatToSendTypeArray[whatToSendVarArray[i]]) {
        case 0:// byte
            byteBufferSend.put((byte) xyz[whatToSendVarArray[i]]);
            break;
        case 1:// short
            byteBufferSend
                    .putShort((short) xyz[whatToSendVarArray[i]]);
            break;
        case 2:// int
            byteBufferSend
                    .putInt((int) xyz[whatToSendVarArray[i]]);
            break;
        // Plus more types...
        }
        byteArrayDataToSend = byteBufferSend.array();
    }
}

Ideally I would like:

typeArray={byte,short,int,long,float,double};
byteBufferSend.put???((typeArray[i]) xyz[whatToSendVarArray[i]]);

What is the best practice?

  • 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-09T15:25:18+00:00Added an answer on June 9, 2026 at 3:25 pm

    Is it possible to reference primitive-types from an array for casting?

    No.

    In Java, you can’t treat an array of one primitive type as if it was an array of a different primitive type. So whatever the base type of the xyz array is, that is the only way you can view the elements.

    The best you can do is extract an element and then explicitly cast it.

    (Reflection won’t help I think because the reflective APIs don’t do casting of primitive types.)


    What is the best practice?

    This kind of low level stuff is best avoided. But if you have to do it, accept the fact that the code is inevitably going to be a bit clunky.

    Best practice is therefore to hide the clunkiness inside a method so that you don’t need to do this kind of thing throughout your code.

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

Sidebar

Related Questions

Based upon a prior question I had Here . I wanted to join two
Wanted to know if there was a way one could query shelveset details from
just wanted to ask where I define initial class properties? From other languages I
I wanted to make a Java based web crawler for an experiment. I heard
I wanted to use 6 different textures on a cube, one per side, but
I wanted to wrap a small C++ code allocating an array with ctypes and
Is there an equivalent in Java 1.5 to Enumerable.Cast() from C#? I want to
i.e. if we cast a C or C++ unsigned char array named arr as
I'm trying to learn SQLite . I wanted to put the information from the
I have a simple method which converts an array from one type to another.

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.