Appearance
Java Data types
If you've ever found yourself scratching your head over terms like integers, strings, or booleans, fear not - we're here to demystify it all and make it as clear as a sunny day.
Before we dive into the specifics, let's start with the basics. What exactly are data types? Well, think of them as categories or classifications that tell Java how to interpret and manipulate data. They define the type of values that can be stored in a variable and the operations that can be performed on those values.
NOTE
Java is what we call "statically typed." What does that mean? Well, it means that every variable and expression in Java has a type that's known at compile time. In simpler terms, when you're coding in Java, you need to declare the type of each variable before you can use it. This helps the compiler catch errors early on and ensures that your code is solid from the get-go.
Types of Data Types:
In Java, data types can be broadly categorized into two main groups: primitive data types and reference data types. Let's delve into each category and unravel its mysteries:
Primitive Data Types:
Primitive data types represent the most basic types of data that Java can handle. They are predefined by the language and are not objects. Primitive data types include:
- Integer Types:
Data Type | Size (in bits) | Minimum Value | Maximum Value | Example |
---|---|---|---|---|
byte | 8 | -128 | 127 | byte num = 100; |
short | 16 | -32,768 | 32,767 | short num = 1000; |
int | 32 | -2^31 | 2^31 - 1 | int num = 100000; |
long | 64 | -2^63 | 2^63 - 1 | long num = 1000000000L; |
- Floating-Point Types:
Data Type | Size (in bits) | Minimum Value | Maximum Value | Example |
---|---|---|---|---|
float | 32 | Approximately 1.4E-45 | Approximately 3.4E+38 | float num = 3.14f; |
double | 64 | Approximately 4.9E-324 | Approximately 1.8E+308 | double num = 3.14; |
- Character Type:
Data Type | Size (in bits) | Minimum Value | Maximum Value | Example |
---|---|---|---|---|
char | 16 | '\u0000' | '\uffff' | char letter = 'A'; |
- Boolean Type:
Data Type | Size (in bits) | Minimum Value | Maximum Value | Example |
---|---|---|---|---|
boolean | Not Applicable | false | true | boolean isTrue = true; |
Reference Data Types:
Reference data types, also known as object data types, refer to objects created using classes. They are stored in the heap memory and can hold a reference (address) to the actual data. Common examples of reference data types include:
String:
String name = "John Doe";
Array:
int[] numbers = {1, 2, 3, 4, 5};
TIP
In Java, when we talk about reference data types, it's not just about built-in stuff like strings and arrays. Your very own custom-made classes also fall into this category! So, whether you're using a class you built yourself or one that comes pre-packaged with Java, if it's not a primitive type, it's a reference type.
Example
- Integer Types:
java
byte numByte = 100; // 8-bit signed integer
short numShort = 1000; // 16-bit signed integer
int numInt = 100000; // 32-bit signed integer
long numLong = 1000000000L; // 64-bit signed integer (note the 'L' suffix)
- Floating-Point Types:
java
float numFloat = 3.14f; // 32-bit floating-point number (note the 'f' suffix)
double numDouble = 3.14; // 64-bit floating-point number
- Character Type:
java
char letter = 'A'; // 16-bit Unicode character
- Boolean Type:
java
boolean isTrue = true; // Boolean value: true or false`
- String (Reference Type):
java
String name = "John Doe"; // Reference to a string object`
- Array (Reference Type):
java
int[] numbers = {1, 2, 3, 4, 5}; // Array of integers`
java
public class DataTypesExample {
// Primitive data types
byte numByte = 100; // 8-bit signed integer
short numShort = 1000; // 16-bit signed integer
int numInt = 100000; // 32-bit signed integer
long numLong = 1000000000L; // 64-bit signed integer
float numFloat = 3.14f; // 32-bit floating-point number
double numDouble = 3.14; // 64-bit floating-point number
char letter = 'A'; // 16-bit Unicode character
boolean isTrue = true; // Boolean value: true or false
// Reference data types
String name = "John Doe"; // Reference to a string object
int[] numbers = {1, 2, 3, 4, 5}; // Array of integers
}