I would like to go in detail on this.
Firstly Java is a strongly typed language i.e. before using any data we need to tell the complier of what data type this data will be.
In simple words we need to declare the data type of the variable.
Basically there are three type of data types:
- Primitive data types
- Derived data types
- User defined data types
Primitive data types are the general and fundamental data types that we have in Java and those are byte, short, int, long, float, double, char, boolean.
Derived data types are those that are made by using any other data type for example, arrays.
User defined data types are those that user / programmer himself defines. For example, classes, interfaces.
In very-very simple words I can say,
int a
Here a is a variable of int data type.
MyClass obj
Here obj is a variable of data type MyClass and we call them reference variables as they can be used to store the reference to the object of that class.
I would like to go in detail on this.
Firstly Java is a strongly typed language i.e. before using any data we need to tell the complier of what data type this data will be.
In simple words we need to declare the data type of the variable.
Basically there are three type of data types:
- Primitive data types
- Derived data types
- User defined data types
Primitive data types are the general and fundamental data types that we have in Java and those are byte, short, int, long, float, double, char, boolean.
Derived data types are those that are made by using any other data type for example, arrays.
User defined data types are those that user / programmer himself defines. For example, classes, interfaces.
In very-very simple words I can say,
int a
Here a is a variable of int data type.
MyClass obj
Here obj is a variable of data type MyClass and we call them reference variables as they can be used to store the reference to the object of that class.
In JAVA there are two types of data types are available, they are
1. Primitive datatypes
2. User Defined datatype
Basically, the primitive data types are predefined. like int, char, float, etc. if you want to store the data of types integer, character, then you can use these predefined datatypes happily. what if, if you want to store the data of your own type like Student, Employee, which is basically a combination of primitive datatypes, then here we have to go with user-defined data type, for this we can use Classes in java to store data of your own
Of course you have user defined data types in java! Any class you define is a data type for objects that defined by you.
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?
You could use inner/nested classes. You'll have only one .java file, but nested classes will all have their own .class file when compiling.
You have to create a class if you want a data structure. Just to avoid creating multiple .java files, you can put those classes in a single .java file. Assuming, Test.java-
public class Test {
class InnerOne {
}
void m() {
class InnerMost {
}
}
}
class Dummy {
}
class OnMore {
}