How Can We Help?
Introduction
Each variable in Java must have a data type. There are several Java data types available. The type determines what that variable can contain and what operations it can perform. For example, the int type declares a variable of type integer (int). These variables can only receive integer values (positive and negative).
The Java programming language has two categories of data types: primitive and reference. A primitive type variable contains a value of the appropriate size and format for its type: number, character, or boolean value. For example, an integer value is 32-bit data in a format known as a dual compliment, a char value is 16-bit data formatted as a Unicode character, and so on.
Basic Syntax
All java data types in Java are given in the following table:
Data type | Description | Format |
---|---|---|
byte | Integer with 1 byte-length | 8-bit integer, double complement |
short | Short integer | 16-bit integer, double complement |
int | Integer | 32-bit integer, double complement |
long | Long integer | 64-bit integer, double complement |
float | Decimal number / with floating point | 32-bit IEEE 754 number with floating point |
double | Double decimal number / with floating point | 64-bit IEEE 754 number with floating point |
char | Single Unicode character | 16-bit Unicode character |
boolean | Logic value (true/false) | 8 bits space /1-bit data |
byte Java Data Type
The byte data type is a primitive data type in Java that can store a single 8-bit signed two’s complement integer value. It conserves memory space and efficiently performs operations on binary data. However, it should only be used for values within its range of -128 to 127 to avoid errors and data loss.
Example:
public class MaxVariablesDemo {
public static void main(String args[]) {
// integers
byte largestByte = Byte.MAX_VALUE;
// display byte data type in java
System.out.println("The largest byte value is " + largestByte);
}
}
Output:
The largest byte value is 127
short Java Data Type
The short data type in Java is a primitive data type that can store a 16-bit signed two’s complement integer value. It occupies more memory compared to the byte data type but can store larger values. The short data type is useful in scenarios where memory conservation is not a primary concern and larger values must be stored. However, like the byte data type, values outside its range may result in data loss or truncation.
Example:
public class MaxVariablesDemo {
public static void main(String args[]) {
// integers
short largestShort = Short.MAX_VALUE;
// display short data type in java
System.out.println("The largest short value is " + largestShort);
}
}
Output:
The largest short value is 32767
int Java Data Type
The int data type is a primitive data type in Java that can store a 32-bit signed two’s complement integer value. It can store a broader range of values than the short and byte data types. It is the default choice for representing whole numbers in Java, and arithmetic operations involving integers are usually performed using the int data type.
Example:
public class MaxVariablesDemo {
public static void main(String args[]) {
// integers
int largestInteger = Integer.MAX_VALUE;
// display int data type in java
System.out.println("The largest integer value is " + largestInteger);
}
}
Output:
The largest integer value is 2147483647
long Java Data Type
The long data type in Java is a primitive data type that can store a 64-bit signed two’s complement integer value. It is used when a more comprehensive range of integer values needs to be stored, exceeding the range of the int data type. The long data type occupies more memory compared to the int data type but provides greater precision for large values.
Example:
public class MaxVariablesDemo {
public static void main(String args[]) {
// integers
long largestLong = Long.MAX_VALUE;
// display long data type in java
System.out.println("The largest long value is " + largestLong);
}
}
Output:
The largest long value is 9223372036854775807
float Java Data Type
The float data type is a primitive data type in Java that can store single-precision 32-bit IEEE 754 floating-point numbers. It is commonly used to represent decimal values and occupies less memory than the double data type. However, it provides less precision and may result in rounding errors when calculating large or small values.
Example:
public class MaxVariablesDemo {
public static void main(String args[]) {
// real numbers
float largestFloat = Float.MAX_VALUE;
// display float data type in java
System.out.println("The largest float value is " + largestFloat);
}
}
Output:
The largest float value is 3.40282e+38
double Java Data Type
The double data type in Java is a primitive data type that can store double-precision 64-bit IEEE 754 floating-point numbers. It provides greater precision compared to the float data type but occupies more memory. It is commonly used for representing decimal values and is the default choice for performing arithmetic operations involving decimal values in Java.
Example:
public class MaxVariablesDemo {
public static void main(String args[]) {
// real numbers
double largestDouble = Double.MAX_VALUE;
// display double data type in java
System.out.println("The largest double value is " + largestDouble);
}
}
Output:
The largest double value is 1.79769e+308
char Java Data Type
The char data type in Java is a primitive data type that can store a single 16-bit Unicode character. It represents individual characters, such as letters, digits, and symbols. The char data type is often used in text processing and is essential for handling internationalization and localization in Java applications.
Example:
public class MaxVariablesDemo {
public static void main(String args[]) {
// other primitive types
char aChar = 'S';
// display char data type in java
if (Character.isUpperCase(aChar)) {
System.out.println("The character " + aChar + " is uppercase.");
}
else {
System.out.println("The character " + aChar + " is lowercase.");
}
}
}
Output:
The character S is upper case.
boolean Java Data Type
The boolean data type in Java is a primitive data type that can store a value of either true or false. It is used for logical operations and conditional statements in Java programming. The boolean data type is essential for writing programs that decisions based on certain conditions or criteria.
Example
With the following program code – we can test the scope (size) of each data type:
public class MaxVariablesDemo {
public static void main(String args[]) {
// other primitive types
boolean aBoolean = true;
// display boolean data type in java
System.out.println("The value of aBoolean is " + aBoolean);
}
}
Output:
The value of aBoolean is true
Literals in Java
Literals are used to express specific values of any type within a program. A literal can be a constant value of a primitive type or a string object. We had already used them in previous lessons when we wanted to assign a value to a variable.
Examples:
char c = ‘R’; - character literal
byte b = 46 ; - byte literal
int hexa_number = 0x52; - hexadecimal integer
float d = -2.35; - floating point literal
string s = “Hello”; - string literal
Escape Characters in Java
If you want to include a control character in a literal string, you need to use a \ character followed by a letter. ‘\’ Is called an escape character. It is also used to introduce certain characters into a string, such as ” -, which would otherwise mean the end of the string. The following is a list of the most important escape characters in Java:
Symbol | Action |
---|---|
\n | newline |
\t | offset (tab) |
\b | backspace |
\r | return – move to the beginning of the next line |
\f | form-feed – move to the next line without going to the beginning |
\s | space |
\ | \ character |
\’ | ‘ character |
\” | “ character |
Final Thoughts
In conclusion, understanding Java Data Types is crucial for writing efficient and high-performing code. By knowing the different data types, characteristics, and limitations, developers can optimize their code and avoid common mistakes. Moreover, mastering Java Data Types can improve code quality, reduce memory usage, and boost performance. Therefore, investing time in learning and practicing Java Data Types is essential to become a better programmer. Whether you’re a beginner or an experienced developer, a solid understanding of Java Data Types is a valuable asset that can help you succeed in your career.