Skip to content

Data Types

This content is for Java. Switch to the latest version for up-to-date documentation.

A data type defines the kind of data a variable can hold and how much memory it uses and what operations can be performed on it.

Java has two categories of data types:

  1. Primitive data types
  2. Reference data types

Primitive types are the basic building blocks and are not objects.

Data TypeSizeDescriptionRangeExample
byte8 bitsInteger-128 to 127byte b = 100;
short16 bitsInteger-32,768 to 32,767short s = 10000;
int32 bitsInteger-2³¹ to 2³¹-1int i = 100000;
long64 bitsInteger-2⁶³ to 2⁶³-1long l = 100000L;
float32 bitsFloating-point~±3.402…float f = 3.14f;
double64 bitsFloating-point~±1.797…double d = 3.14159;
char16 bitsUnicode character0 to 65,535char c = 'A';
boolean1 bitTrue/false valuetrue or falseboolean flag = true;
byte smallNumber = 127;
short mediumNumber = 32767;
int regularNumber = 2147483647;
long largeNumber = 9223372036854775807L; // Note the 'L' suffix
float simpleDecimal = 3.14f; // Note the 'f' suffix
double preciseDecimal = 3.141592653589793;
char letter = 'A';
char unicodeChar = '\u00A9'; // Copyright symbol ©
boolean isActive = true;
boolean hasPermission = false;

Reference types are derived from classes and interfaces.

String greeting = "Hello, Java!";
String empty = ""; // Empty string
String nullString = null; // Null reference
int[] numbers = {1, 2, 3, 4, 5};
String[] names = new String[3];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
// Define a class
class Person {
String name;
int age;
}
// Create an object
Person person = new Person();
person.name = "John";
person.age = 30;

Every primitive type has a corresponding wrapper class:

Primitive TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean
Integer wrappedInt = 42;
FeaturePrimitive Data TypesReference Data Types
StorageStore actual values directly in memoryStore references (addresses) to objects in memory
DefinitionPredefined by the Java languageCreated from classes or interfaces (can be user-defined)
SizeFixed sizeSize can vary (depends on the object)
Are they objects?NoYes
Examplesint, double, char, booleanString, arrays, all class objects
  1. Use the appropriate type for the data you’re storing
  2. Use int for most integer values unless you need a larger range
  3. Use double for most floating-point calculations
  4. Use wrapper classes when you need to treat primitives as objects
  5. Be careful with type conversions to avoid data loss
Built with passion by Ngineer Lab