Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Create a New primitive datatype in Java

Tags:

java

types

First thing I want to clarify here, this question is because of curiosity. I am not facing any issue.

There are many primitive types available in Java byte, short, int, etc. Now suppose I want to create a new primitive type (e.g. mediumint or anything else). Can we do that? If yes then how?

like image 370
GD_Java Avatar asked Apr 20 '13 18:04

GD_Java


People also ask

Can we create object for primitive data type?

Since the primitive data types consume less memory and can be accessed faster, they are not objects. The equivalent Wrapper classes are also available in java like "Integer" "Short" etc. They can be used as objects if you want.

Can you create collections of primitive types in Java?

Collections are the framework used to store and manipulate a group of objects. Java Collection means a single unit of objects. Since the above two statements are true, generic Java collections can not store primitive types directly.


5 Answers

Primitive types are the ones defined by the language itself. In Java you can only define new types as Classes all derived from the common base class called Object.

like image 115
Heisenbug Avatar answered Oct 13 '22 18:10

Heisenbug


You all are wrong. Yes. You can. As a rule, you do not need a completely new primitive type. So, you don't need to rebuild JVM. Usually, you only need to "pack" or "cast" your new primitive type to an existing and define some operations like "+" with its wrapper.

You can do it relatively simply with original JVM, but with your own Java compiler. Good luck!

like image 29
Steel Rat Avatar answered Oct 13 '22 18:10

Steel Rat


You could, but then it wouldn't be Java anymore.

like image 20
0x6C38 Avatar answered Oct 13 '22 18:10

0x6C38


You cannot create your own primitive datatype.

As the Java documentation explains: A primitive type is predefined by the language and is named by a reserved keyword.

like image 35
bernie2436 Avatar answered Oct 13 '22 16:10

bernie2436


Simply No, You can not create primitive datatype.

Primitive datatype means which are provided and existed in language feature. Basically Java support this for performance reason and perform arithmetic operation.

You can create a user defined datatype using concept of class and object.

like image 22
Ajay S Avatar answered Oct 13 '22 17:10

Ajay S