10

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?

0

5 Answers 5

13

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.

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

8 Comments

whats stopping him from playing around with the original code?
You can mess with the source of a JVM, but it wouldn't be Java as Java is defined by the Java language specification (docs.oracle.com/javase/specs)
In addition to the answer: in some similar languages (e.g. C#) you can define your own objects which are passed by value (msdn.microsoft.com/en-us/library/s1ax56ch(v=vs.110).aspx), which resemble primitives in some ways. But in Java you can't.
@dtech: from my point of view C#'s structs are more similar to classes than primitive data. They are simple collections of primitive data and methods. The only difference is that are passed by value like you pointed out.
@dtech: maybe you don't consider C++ a "reasonable OOP language" :). But in C++ you can pass objects either by value or reference.
|
5

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

Comments

2

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.

Comments

2

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!

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.