Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Data Types in Java Programming

Introduction

In Java, data types specify the different sizes and values that can be stored in a variable. Java is a strongly typed language, which means that every variable must have a data type. Data types are divided into two groups:

  • Primitive Data Types
  • Non-Primitive Data Types

Primitive Data Types

Primitive data types are the most basic data types available in Java. There are 8 primitive data types:

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

byte

The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).

Example:
byte a = 100;

short

The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

Example:
short s = 10000;

int

The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2^31 and a maximum value of 2^31 - 1 (inclusive).

Example:
int i = 100000;

long

The long data type is a 64-bit signed two's complement integer. It has a minimum value of -2^63 and a maximum value of 2^63 - 1 (inclusive).

Example:
long l = 100000L;

float

The float data type is a single-precision 32-bit IEEE 754 floating point. It's used to save memory in large arrays of floating point numbers.

Example:
float f = 234.5f;

double

The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice.

Example:
double d = 123.4;

char

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Example:
char letterA = 'A';

boolean

The boolean data type has only two possible values: true and false. This data type is used for simple flags that track true/false conditions.

Example:
boolean isJavaFun = true;

Non-Primitive Data Types

Non-primitive data types, also known as reference types, are more complex than primitive data types. They are used to store a reference to the data. Examples include:

  • Strings
  • Arrays
  • Classes
  • Interfaces

Strings

The String class is used to create and manipulate strings. Strings are a sequence of characters.

Example:
String greeting = "Hello, World!";

Arrays

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created.

Example:
int[] myArray = {1, 2, 3, 4, 5};

Classes

A class is a blueprint for objects. It defines a datatype by bundling data and methods that work on the data into one single unit.

Example:
public class MyClass {
 int x = 5;
}

Interfaces

An interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.

Example:
public interface Animal {
 void eat();
 void travel();
}

Conclusion

Understanding data types is fundamental to programming in Java. Primitive data types provide a way to store simple values, while non-primitive data types allow for the creation of complex structures and objects. By mastering these data types, you will be able to write more efficient and effective Java programs.