0

I have a requirement where I have to create my own data type and i should assign some bytes to the data types.
Example:

Datatype A : should have 1 byte of memory
Datatype B : should have 2 bytes of memory
Datatype C : should have 7 bytes of memeory etc..

Is there any way we can define our own data types and allocate some memory to them ?

1
  • 2
    yes they're called objects, welcome to java Commented Aug 2, 2017 at 14:24

4 Answers 4

3

There are a few "options" in Java to express types, namely: interfaces, classes, and enums.

In that sense, the best match would be a class, like:

public class TwoByteHolder {
  public final byte[] data = new byte[2];
}

An object of that class allows you to exactly store 2 bytes; the next "level" could be something like:

public class ByteHolder {
  public final byte[] data;

  public ByteHolder(int numberOfBytes) {
    data = new byte[ numberOfBytes ];
  }
  ...

But of course: the memory overhead would be enormous - Java isn't the the best language to deal with such requirements.

Sign up to request clarification or add additional context in comments.

5 Comments

"An object of that class allows you to exactly store 2 bytes" Yes -- with an overhead of ~32 bytes per instance.
@GhostCat , thanks for the answer, i have tied the same approach by declaring the byte type as you suggested but i am unable to accees the value in my method
public class bytedata { public byte[] dataA = new byte[1]; public void sample() { dataA=125; } }
it was saying change the byte to int
Because that number written this way has type int. And int has a larger range than byte. These are super basics. So the real answer is : pick up a good book and start learning - do not expect us to explain things to you that are documented a zillion times already.
1

The only user-defined types available in Java are classes (including enums), and you cannot directly control how large they are. A class instance has many bytes of overhead you can't avoid having.

Comments

1

You can create a new class which has specific fields. If you need exact size of fields you can use byte arrays.

class Data {

  public byte[] dataA = new byte[1];
  public byte[] dataB = new byte[2];
  public byte[] dataC = new byte[7];
  ...
}

4 Comments

code public byte[] dataA = new byte[1]; public void sample() { //dataA=125; }
thanks for your answer, i have tried declaring the byte as you suggested, i can not able to access the dataA variable in my method#
It's note the best way to do that but you can try with public static byte[] dataA = new byte[1]; and then in your method try Data.dataA
Thanks, i did the same way. i just declared all the byte arrays in one class and tried accessing them from other class with className.varibleName , i a m able to access it but unable to assign some value to it. it was saying change the dataA type to int ByteClass.dataA=123; here am getting error line at 123 to change it to int type
0

You can't create data types in java, but you can create object classes, like this:

Class A {
    public byte[] bytes; 
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.