Java – Basic Data Types

Basic Data Types in Java
  1. Introduction to Data Types
  2. Primitive Data Types
  3. Reference Data Types
  4. Literals in Java
  5. Escape Sequences

1. Introduction to Data Types

First of all, you should remember that Java is a statically typed languages. For this reason, variables must be declared before use. Variable declaration allocated memory for the variable.

Amount of space allocated depends of the data type. Therefore, we would examine Java basic data types. We have two types of data in Java:

  • Primitive Data Type
  • Reference Data Type

2. Primitive Data Type

We have eight of them. They are byte, short, long, float, double, char and boolean.

byte

  • 8 bit signed integer
  • Ranges from -128 to 127
  • Default value of 0
  • For example: byte x = 20, byte y = -5

short

  • 16 bit signed integer
  • Ranges from -32,768 to 32,767
  • Default value of 0
  • For example: short a = 20,000, short b = -12,434

int

  • 32 singed integer
  • Ranges from -2,147,483,648 to 2,147,483,648
  • Default value of 0
  • For example: int i = 300000, int j = -574800
  • Generally used for integers

long

  • 64 bit signed integer values
  • Ranges from -263 to 263 – 1
  • Used when very large value is needed
  • Default value of 0L
  • For example: long x = 200000L; long y = -100000L

float

  • 32 bit floating point numbers
  • Default value of 0.0f
  • Not generally used
  • Lacks precision relative to double
  • For example: float fnum = 0.01f; float fnum2 = 21.840f

double

  • 64 bit floating point numbers
  • Has better precision than float
  • A preferred type for decimals
  • Default value of 0.0d
  • For example: double mydouble = 47.02984; double d = 0.034d

char

  • A single 16 bit unicode character
  • Ranges from ‘\u0000’ (0) to ‘\uffff’ (65,535)
  • Stores a single character
  • For exampe: char a = ‘a’; char grade = ‘B’

boolean

  • A single bit data type
  • Has only two values: true or false (1 and 0)
  • Default value of false
  • For example: boolean tall = true

3. Reference Data Types

Reference types are used to access objects. Therefore, they are created using constructors. Note the following about reference types:

  • Class objects are declared as reference types
  • Arrays are also reference types
  • Default value of null
  • Reference variable can be used to refer to any object of the declared type
  • For example: Vehicle vehicle = new Vehicle (“Camry”);

4. Literals in Java

		char grade = 'A';
		byte b = 20;
		boolean rich = true;

Here, ‘A’, 20 and true are all literals.

Furthermore, byte, int, long and short can be express in decimal, octal or hexadecimal

  • decimal is base 10
  • octal is base 8
  • hexadecimal is base 16

The following prefix rules applies:

  • hexadecimal literals are prefixed with 0x
  • octal literals are prefixed with 0

Example is given below:

 int decimalval = 200; 

int octalval = 0200; 
int hexval = 0x128F; 

String Literals

String literals are enclosed in double quotes. For example:

  • “The Tech Pro
  • “Java”

Also note that string literals can be assigned to string variables. We would learn this in subsequent tutorials.

5. Escape Sequences  in Java

Used to control display of strings. Note list of escape sequences below

SequenceWhat it Represents
\nNew line (0x0a)
\rCarriage return (0x0d)
\fFormfeed (0x0c)
\bBackspace (0x08)
\sSpace (0x20)
\ttab
\”Double quotes
\’Single quote
\\backslash
\dddOctal character (ddd)
\uxxxxHex UNICODE character (xxxx)