Java Data Types

Domains: Java Basics

1. Introduction to Data Types in Java

In Java, data types specify the different sizes and values that can be stored in a variable. Java is a statically-typed language, meaning that variables must be declared with a specific data type. Let's explore five fundamental data types: boolean, int, float, double, and String.

2. Boolean

Definition: The boolean data type is used to store two possible values: true or false. It is commonly used for conditional logic, decision-making, and flagging certain states.

Size: It occupies one bit of memory (though it is generally optimized by the JVM to use a byte).

boolean isJavaFun = true;
boolean isRaining = false;

3. Integer (int)

Definition: The int data type is used to store integer values (whole numbers without a decimal point) in the range of -2^31 to 2^31 - 1.

Size: It occupies 4 bytes (32 bits) in memory.

int age = 25;
int year = 2024;

4. Float

Definition: The float data type is used for storing single-precision decimal numbers (floating-point numbers). It’s less precise than double, but occupies less memory.

Size: It occupies 4 bytes (32 bits).

Usage: It is typically used when you need to save memory in large arrays of floating-point numbers.

float price = 19.99f;
float temperature = 36.6f;

Note: The f suffix is necessary to tell the compiler the number is a float and not a double.

5. Double

Definition: The double data type is used for storing double-precision decimal numbers. It provides more precision and range compared to float, making it suitable for mathematical calculations requiring high accuracy.

Size: It occupies 8 bytes (64 bits).

double pi = 3.14159265359;
double salary = 55000.75;

6. String

Definition: The String data type represents a sequence of characters, and it is widely used to store and manipulate text in Java.

Size: It is not a primitive data type but a class in Java. However, it behaves similarly to primitive types due to Java's special handling of String.

String name = "John Doe";
String greeting = "Hello, World!";

Strings are immutable in Java, meaning that once created, their values cannot be changed. However, you can perform various operations on strings using methods provided by the String class.

String Methods

Here are some commonly used String methods:

a. substring()

Definition: Extracts a portion of the string based on the specified starting index and (optionally) ending index.

Syntax:

String substring(int startIndex)
String substring(int startIndex, int endIndex)

Example:

String text = "Hello, World!";
String part = text.substring(7); // Outputs "World!"
String specificPart = text.substring(0, 5); // Outputs "Hello"

b. charAt()

Definition: Returns the character at a specific index in the string.

Syntax:

char charAt(int index)

Example:

String message = "Java";
char letter = message.charAt(1); // Outputs 'a' (indexing starts from 0)

c. length()

Definition: Returns the length (number of characters) in a string.

Syntax:

int length(string)

Example:

String phrase = "Data types in Java";
int length = phrase.length(); // Outputs 18

d. split()

Definition: Divides the string into an array of substrings based on a specified delimiter.

Syntax:

String[] split(String regex)

Example:

String sentence = "Java is powerful";
String[] words = sentence.split(" "); 
// Outputs: ["Java", "is", "powerful"]

Conclusion

Understanding these basic data types and string methods is crucial for any Java developer. By mastering how to use data types like boolean, int, float, double, and String, and effectively utilizing string manipulation methods, you can perform a wide range of programming tasks, from basic operations to more advanced logic.

Similar pages

Page structure
Terms

String

Integer

Java

Float

Double

Data Types

Boolean

Method

length()

Arrays

Class

substring()

charAt()

split()