Understanding Datatypes In Java
Java is a statically typed programming language, which means that every variable and expression must have a declared data type at compile time. Here are some of the common data types available in Java:
1. **Primitive Data Types:**
These are the most basic data types in Java, representing simple values.
- `byte`: 8-bit signed integer.
- `short`: 16-bit signed integer.
- `int`: 32-bit signed integer.
- `long`: 64-bit signed integer.
- `float`: 32-bit floating-point number.
- `double`: 64-bit floating-point number.
- `char`: 16-bit Unicode character.
- `boolean`: Represents true or false.
2. **Reference Data Types:**
These data types are used to refer to objects created using classes.
- `class`: Represents a user-defined blueprint or
template for creating objects.
- `interface`: Specifies a contract that a class must adhere to.
- `enum`: Represents a set of predefined constants.
- `array`: Represents a collection of elements of the same type.
3. **Derived Data Types:**
These are constructed from the primitive and reference data types.
- `interface`: Specifies a contract that a class must adhere to.
- `enum`: Represents a set of predefined constants.
- `array`: Represents a collection of elements of the same type.
3. **Derived Data Types:**
These are constructed from the primitive and reference data types.
- `String`: Represents a sequence of characters. Despite its appearance, it's not a primitive data type but a class.
- `Array`: Represents a collection of elements, where each element can be of any data type.
- `Class`: Represents a class type.
These data types are used to define the type of values that variables can hold. For example:
```java
int age = 25; // Primitive data type
double pi = 3.14159; // Primitive data type
char initial = 'A'; // Primitive data type
boolean isActive = true; // Primitive data type
String name = "John"; // Reference data type
int[] numbers = {1, 2, 3, 4, 5}; // Reference data type (array)
```
It's worth noting that Java is a strongly typed language, which means that you can't perform certain operations between incompatible data types without explicit conversion. For example, you can't directly add a `String` to an `int` without converting the `int` to a `String` first.
```java
String str = "Hello, ";
int num = 42;
String result = str + num; // This works due to automatic conversion of int to String
```
- `Class`: Represents a class type.
These data types are used to define the type of values that variables can hold. For example:
```java
int age = 25; // Primitive data type
double pi = 3.14159; // Primitive data type
char initial = 'A'; // Primitive data type
boolean isActive = true; // Primitive data type
String name = "John"; // Reference data type
int[] numbers = {1, 2, 3, 4, 5}; // Reference data type (array)
```
It's worth noting that Java is a strongly typed language, which means that you can't perform certain operations between incompatible data types without explicit conversion. For example, you can't directly add a `String` to an `int` without converting the `int` to a `String` first.
```java
String str = "Hello, ";
int num = 42;
String result = str + num; // This works due to automatic conversion of int to String
```