Skip to main content

Java Fundamentals

DataTypes

In Java, each type of data (such as integer, character, decimal, boolean, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types. It determines the type of data associated with variables.

In Java, data types are divided into two main categories.

Primitive Data Types

Primitive data types in Java are simple data types that hold single values and do not possess any special capabilities. There are a total of 8 primitive data types. 

Let’s discuss each of these data types and use them in our programs to understand how they function.

Integer - The integer data type in Java is a way to store whole numbers, both positive and negative. It is represented by 32 bits.

In Java SE 8 and subsequent versions, the int data type can be utilized to represent an unsigned 32-bit integer within the range [0, 2^32-1].

Syntax

int num = 10;

Long - The long data type in Java is used to store very large whole numbers, both positive and negative. It is represented by 64 bits.

In Java SE 8 and subsequent versions, the long data type can be utilized to represent an unsigned 64-bit integer within the range [0, 2^64-1].

Syntax

long largeNum = 10000000006;

This data type is helpful when you need to work with numbers that might be too large for the standard integer data type to handle.

Float - The float data type in Java is used to store decimal numbers, but it takes up less memory compared to the double data type. It uses 32 bits to store the value. 

If you have a variable declared as a float, it means you can store decimal values with less precision compared to a double. 

Remember to use 'f' as a suffix for the value, for example, 12.3f.

For example:

float fraction =  12.3f;

Double - The double data type in Java is used for storing decimal values, and it offers more precision compared to the float data type.

When you declare a variable as double, it means you can store decimal values with higher precision compared to a float.

For example:

double d = 4.355453532; 

By default, fraction value is double in Java.

Boolean:  The boolean data type in Java is used to represent only two values, which are true and false. It's like a switch that can be either on or off.  

The size of the boolean data type can vary depending on the virtual machine being used. 

For example:

boolean isTrue = true;

Char - The char data type in Java is used for storing a single character. It uses 16 bits to represent a character, adhering to the Unicode standard, which allows it to accommodate a wide range of characters from different languages and symbols.

When you declare a variable as char, it means you can store a single character like 'A', 'B', '1', or '$'.

For example:

 char letter = ‘a’;

Byte - The byte data type in Java is used to store small numbers. It is represented by 8 bits using the two's complement method for negative numbers.

When you declare a variable as a byte, it means you can store values from -128 to 127.

This data type is beneficial for conserving memory, especially when dealing with large arrays of numbers.

For example:

 byte b = 10; 

Short - The short data type in Java is used to store small integers. It is represented by 16 bits and follows the two's complement method for handling negative numbers.

This data type is beneficial when you need to save memory, especially in scenarios where you are dealing with large arrays of numbers.

For example:

short s = 40;

Non-primitive data types

It included data types such as strings, objects, and arrays that store memory addresses rather than the variable value directly. These are also known as reference types. These types include more complex structures like classes and interfaces, enabling you to manage and manipulate complex data and functionalities in your programs. 

Arrays - An array is a collection of similar variables that share a common name. An array can hold primitives like integers, characters, and so on, as well as references to objects of a class, based on how the array is defined.

For primitive data types, the real values are stored in neighboring memory locations, making it easier to access them efficiently.

Syntax

datatype array-name[];
OR
datatype[] var-array;

Strings - Strings are objects that can hold sequences of characters. Each character is stored in 16 bits using the UTF-16 encoding format. Essentially, a string in Java works like an array of characters. 

Syntax

String variableName = “sequence of characters”;

Class - A class in Java is like a template that describes what something is made of and what it can do. It is a set of objects that share common characteristics and common properties/ attributes.

For example, think of a class as a blueprint for creating objects. For instance, consider a class named Fruit. This class could have properties such as name, color, and taste. It could also have methods like getTaste() and getColor(), which return the taste and color of the fruit, respectively.

In this example, the Fruit class is a template that defines what a fruit is and what it can do. It doesn't represent any specific fruit itself, but it outlines the common features and behaviors that any fruit object could have. This way, you can create multiple fruit objects based on this Fruit class blueprint, such as an apple, banana, or orange, each with its own unique characteristics and behaviors.

We will learn more about the class later.

Object - An object in Java is like a thing you can interact with in a program. It's a part of the program that can do stuff and has information about itself. Think of it like a pet, like a dog, which can do things like bark and wag its tail and has characteristics like its breed and color.

Let's consider the example of a Fruit class and a few objects created from it.

Imagine a Fruit class that has attributes like name, color, and taste and methods like getTaste() and getColor(). Using this class, you can create specific fruit objects such as an apple, banana, and orange, each with its own unique properties and behaviors.

For instance:

  • apple: name = "Apple", color = "Red", taste = "Sweet"
  • banana: name = "Banana", color = "Yellow", taste = "Sweet"
  • orange: name = "Orange", color = "Orange", taste = "Tangy"

Each of these objects would have the common properties and behaviors defined in the Fruit class but with specific values unique to each fruit. This way, you can create and manipulate different fruit objects in your Java program, each representing a specific type of fruit.

We will learn more about the objects later.

Interface - In Java, an interface is a programming structure that allows you to define a contract for classes to follow. It specifies a set of methods that a class must implement. Interfaces provide a way to achieve abstraction in Java, enabling you to define the functionality a class should provide without specifying how that functionality will be implemented.

We will learn more about the interface later.