Java is a strongly typed language which means that each variable declared within an application must have its type specified. This provides the compiler with all the necessary information to properly manage and allocate space in memory for each item, as well as identify any potential errors when code is being written.
The basic datatypes available in Java are primitives such as int, long, double, and char which represent whole numbers, real numbers, or characters, respectively. Additionally, there are non-primitive types like Strings and arrays, which allow developers to store more complex data structures within their programs. These types can be used alone or combined together into custom classes giving applications even more flexibility when dealing with user input or creating other kinds of objects during runtime!
Primitive Data Types
Primitive data types are the basic data types that are built-in into the Java language. They include
● int: used to store whole numbers (integers).
● double: used to store decimal numbers (floating-point numbers). ● boolean: used to store true or false values.
● char: used to store a single character.
● byte: used to store small integers.
● short: used to store short integers.
● long: used to store large integers.
● float: used to store decimal numbers with single precision.
Example:
int number = 42;
double pi = 3.14;
boolean isTrue = true;
One of the main advantages of using these pre-defined datatypes is that they provide a way to write more efficient code by ensuring each variable has been assigned only one type—preventing unnecessary conversions between different primitive values at compile time, thus improving performance significantly over time. Furthermore, it also helps reduce complexity by providing strict guidelines on how data should be handled - making it easier for developers to debug their applications quickly if needed while still
retaining full control over what kind of data they need inside their projects! It’s clear then why Java’s basic datatypes remain so popular today - offering both safety and speed for those who choose to work with this powerful language!
Reference Data Types
Reference data types are used to store references to objects in Java. They include
● String: A sequence of characters will be stored in a string.
● Array: Stores a collection of elements.
● Class: Blueprint for creating objects.
Example:
String name = "Jane Doe";
Car myCar = new Car();
In the first line, a string is declared with the name “Jane Doe”
In the second line, a variable "myCar" of type Car (which is a custom class) is declared and initialized with a new instance of the class.
It's important to keep in mind that Reference data types are not primitive, and they are objects, so they have their own methods and properties, and they are created by using the new keyword, unlike primitive types.