Sarath
Sarath

Reputation: 23

User-defined datatype missing in java

Why java does not contain "struct" . Is there a specific reason to avoid struct and also user-defined data type in java?

Upvotes: 1

Views: 138

Answers (6)

Sarath
Sarath

Reputation: 23

Thank you all for your answers. Actually i came out with a wrong title for my question. (I meant to ask the reason for omitting structures in java and ended up with the current title). Thanks again to everyone for your answers.

Upvotes: 0

Rahul Rabhadiya
Rahul Rabhadiya

Reputation: 430

Java is based on OOPs , So real life entity is represented as Class object. so basically class represent custom data type, other then that you have enum also in java. There lots of tutorial you can find on Java basics. one of is javatpoint . Read some basics about Java class.

Upvotes: 2

Luke Garrigan
Luke Garrigan

Reputation: 5011

This is from Oracle on why they made the decision not to have structs:

2.2.2 No More Structures or Unions Java has no structures or unions as complex data types. You don't need structures and unions when you have classes; you can achieve the same effect simply by declaring a class with the appropriate instance variables. The code fragment below declares a class called Point.

They also explain the features that have been removed from C and C++:

2.2 Features Removed from C and C++ The earlier part of this chapter concentrated on the principal features of Java. This section discusses features removed from C and C++ in the evolution of Java. The first step was to eliminate redundancy from C and C++. In many ways, the C language evolved into a collection of overlapping features, providing too many ways to say the same thing, while in many cases not providing needed features. C++, in an attempt to add "classes in C", merely added more redundancy while retaining many of the inherent problems of C.

Upvotes: 1

Gabe
Gabe

Reputation: 441

User-defined data type in Java = we use a class :)

Upvotes: 2

Shira Elitzur
Shira Elitzur

Reputation: 441

Of course you have user defined data types in java! Any class you define is a data type for objects that defined by you.

Upvotes: 3

Pooja Arora
Pooja Arora

Reputation: 574

Because Java has classes. To start with classes implement Inheritance and many other features of OOPS.

Read this post:

What is the difference between C structures and Java classes?

Upvotes: 2

Related Questions