Home Variables and Basic DataTypes
Post
Cancel

Variables and Basic DataTypes

Although Java is object oriented, not all types are object. There are primitive types as well. Here is a list of all primitive types in Java:

Primitive Datatypes:

Data TypeDescriptionSizeExample LiteralsRange
bytetwos complement integer8 bits(none)-2^7…2^7-1
shorttwos complement integer16 bits(none)-2^15…2^15-1
inttwos complement integer32 bits-2, -1, 0, 1, 2-2^31…2^31-1
longtwos complement integer64 bits-2L, -1L, 0L, 1L, 2L-2^63…2^63-1
floatIEEE 754 floating point32 bits1.23e100f, -1.23e-100f, .3f, 3.14F 
doubleIEEE 754 floating point64 bits1.23456e300d, -1.23456e-300d, 1e1d 
booleantrue or false1 bittrue, false0…1
charUnicode character16 bits‘a’, ‘\u0041’, ‘\101’, ‘\’, ‘'’, ‘\n’, ‘ß’‘\u0000’…‘\uffff’ or 0…65,535 inclusive


Reference Datatypes:

Data TypeDescriptionSizeExample LiteralsRange
Class objects and various type of array variablesThese variables are declared to be of a specific type that cannot be changed-Animal animal = new Animal(giraffe);-

Numbers

1
2
3
4
5
byte a = 68;
int myNumber = 5;
double d = 4.5;
float f = (float) 4.5;
float f = 4.5f; // (f is a shorter way of casting float)

Characters and Strings

1
char c = 'g';

Double quotes automatically creates a new String object. String objects are immutable, which means that once created, their values cannot be changed.
For example: String s = "this is a string";

1
2
String s1 = new String("Who let the dogs out?"); // String object stored in heap memory
String s2 = "Who who who who!";	// String literal stored in String pool

Boolean

1
boolean b = false;

Hope to see you in the next article…

This post is licensed under CC BY 4.0 by the author.