Data Types
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:
- Primitive data types
- Reference data types
Primitive Data Types
Section titled “Primitive Data Types”Primitive types are the basic building blocks and are not objects.
| Data Type | Size | Description | Range | Example |
|---|---|---|---|---|
byte | 8 bits | Integer | -128 to 127 | byte b = 100; |
short | 16 bits | Integer | -32,768 to 32,767 | short s = 10000; |
int | 32 bits | Integer | -2³¹ to 2³¹-1 | int i = 100000; |
long | 64 bits | Integer | -2⁶³ to 2⁶³-1 | long l = 100000L; |
float | 32 bits | Floating-point | ~±3.402… | float f = 3.14f; |
double | 64 bits | Floating-point | ~±1.797… | double d = 3.14159; |
char | 16 bits | Unicode character | 0 to 65,535 | char c = 'A'; |
boolean | 1 bit | True/false value | true or false | boolean flag = true; |
Integer Types
Section titled “Integer Types”byte smallNumber = 127;short mediumNumber = 32767;int regularNumber = 2147483647;long largeNumber = 9223372036854775807L; // Note the 'L' suffixFloating-Point Types
Section titled “Floating-Point Types”float simpleDecimal = 3.14f; // Note the 'f' suffixdouble preciseDecimal = 3.141592653589793;Character Type
Section titled “Character Type”char letter = 'A';char unicodeChar = '\u00A9'; // Copyright symbol ©Boolean Type
Section titled “Boolean Type”boolean isActive = true;boolean hasPermission = false;Reference Data Types
Section titled “Reference Data Types”Reference types are derived from classes and interfaces.
String
Section titled “String”String greeting = "Hello, Java!";String empty = ""; // Empty stringString nullString = null; // Null referenceArrays
Section titled “Arrays”int[] numbers = {1, 2, 3, 4, 5};String[] names = new String[3];names[0] = "Alice";names[1] = "Bob";names[2] = "Charlie";Classes and Objects
Section titled “Classes and Objects”// Define a classclass Person { String name; int age;}
// Create an objectPerson person = new Person();person.name = "John";person.age = 30;Wrapper Classes
Section titled “Wrapper Classes”Every primitive type has a corresponding wrapper class:
| Primitive Type | Wrapper Class |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Integer wrappedInt = 42;Differences
Section titled “Differences”| Feature | Primitive Data Types | Reference Data Types |
|---|---|---|
| Storage | Store actual values directly in memory | Store references (addresses) to objects in memory |
| Definition | Predefined by the Java language | Created from classes or interfaces (can be user-defined) |
| Size | Fixed size | Size can vary (depends on the object) |
| Are they objects? | No | Yes |
| Examples | int, double, char, boolean | String, arrays, all class objects |
Best Practices
Section titled “Best Practices”- Use the appropriate type for the data you’re storing
- Use
intfor most integer values unless you need a larger range - Use
doublefor most floating-point calculations - Use wrapper classes when you need to treat primitives as objects
- Be careful with type conversions to avoid data loss