Loading lessons...
Java Data Types
Java Data Types
A data type defines what kind of value a variable can hold. Java has two groups: primitive types and reference types.
Primitive data types
Primitive types are built into Java and store single values:
| Type | Size | Stores |
|---|---|---|
byte | 1 byte | Whole numbers from -128 to 127 |
short | 2 bytes | Whole numbers from -32768 to 32767 |
int | 4 bytes | Whole numbers from -2147483648 to 2147483647 |
long | 8 bytes | Very large whole numbers |
float | 4 bytes | Decimal numbers |
double | 8 bytes | Decimal numbers with more precision |
boolean | 1 bit | true or false |
char | 2 bytes | A single character, written in single quotes |
Examples
int myNum = 5;
float myFloat = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
int vs double
- Use
intfor whole numbers like42. - Use
doublefor numbers with decimals like3.14.
float vs double
Both store decimals. double is the default and is more precise. Use double unless you have a reason not to.
TL;DR
- Primitive types store a single value.
- Common types:
int,double,boolean,char, and theStringreference type. charuses single quotes;Stringuses double quotes.