## Primitive Data types - Introduction

__Background __

During this tutorial, we will investigate the different data-types used in Java.

Before we start, we first have to consider how do computers store information internally? Well, in 0s and 1s.

For example, the number 5 in

as

use all types of information, ranging from simple text files to movies and song clips.

In the previous paragraph, we converted two decimal numbers to binary. It can be noted that the larger the number, the more

digits we need to represent it in binary (duh!), so the number 5 in decimal would need 3 digits in binary, and the number 10 would

need a minimum of 4 digits. This amount of digits is called

can represent. For example. a bit dept of 2, can be used to represent 4 different things, namely 00, 01, 10, 11. Therefore it can

be used to represent 4 different decimal numbers, i.e. 0, 1, 2, 3. A bit length of 8 can represent numbers from 0 to 255

(total of 256 different numbers) from 00000000, 00000001, .., .., .... 11111111.

When storing information in a java program, we use different

we use a data-type to store whole numbers ( such as 1, 99, -12) and a different data-type to store floating point

numbers (numbers with fractions like 0.44, -31.9238, 66.0). Each data-type stores a certain type of information, and have different

sizes/bit-lengths, which will be discussed next.

Before we start, we first have to consider how do computers store information internally? Well, in 0s and 1s.

For example, the number 5 in

**(the numbers format that people usually use) would be represented in**__decimal__*format*__binary__as

*101,*and the number 10 in decimal would be represented as 1010 in binary. Computers use the binary format to store anduse all types of information, ranging from simple text files to movies and song clips.

In the previous paragraph, we converted two decimal numbers to binary. It can be noted that the larger the number, the more

digits we need to represent it in binary (duh!), so the number 5 in decimal would need 3 digits in binary, and the number 10 would

need a minimum of 4 digits. This amount of digits is called

__bit-length__or__bit-depth__. The larger the bit depth, the more numbers wecan represent. For example. a bit dept of 2, can be used to represent 4 different things, namely 00, 01, 10, 11. Therefore it can

be used to represent 4 different decimal numbers, i.e. 0, 1, 2, 3. A bit length of 8 can represent numbers from 0 to 255

(total of 256 different numbers) from 00000000, 00000001, .., .., .... 11111111.

When storing information in a java program, we use different

__data-types__to store different types of data. For example,we use a data-type to store whole numbers ( such as 1, 99, -12) and a different data-type to store floating point

numbers (numbers with fractions like 0.44, -31.9238, 66.0). Each data-type stores a certain type of information, and have different

sizes/bit-lengths, which will be discussed next.

__Primitive Data Types in Java__

Examine the following piece of code:

public static void main(String[]args){ int x = 3; float y = 4.2f; String z = "When Chuck Norris saves a file, the file thanks him"; boolean a = true; boolean b = false; }

The above code is a main method of a program that contains three statements. The first statement defines a

Just like in mathematics, java also contains variables, which can be initialized with certain values. The variable x here, is assigned the value 3.

The

Similarly, we define a variable y, with a type of

The variable y, is assigned a value of 4.2. The

it could be added to a floating point variable of size 32-bit. If the

fraction/floating point number occupies a 64-bit size variable.

The variable z is of type

predefined size, and there is no limit to the size or number of characters a string can have (sort of). We will explore strings in more depth in a later tutorial.

Variables

They are used in conditional statements, which will be discussed in a later tutorial.

There are 9 primitive data types in java. The following table summarizes the information related to every data-type, including their respective

sizes and default values.

*variable*called x.Just like in mathematics, java also contains variables, which can be initialized with certain values. The variable x here, is assigned the value 3.

The

*int*before the x, says that x is of type*int*, which means that x can store integers of sizes up to 32-bits.Similarly, we define a variable y, with a type of

*float.**A*float can store floating point numbers of sizes up to 32-bits.The variable y, is assigned a value of 4.2. The

*f*that is added at the end of the number, indicates that it is a "float", i.e.,it could be added to a floating point variable of size 32-bit. If the

*f*is omitted, you could get a syntax error, since by default, anyfraction/floating point number occupies a 64-bit size variable.

The variable z is of type

*String*. Strings are used to store pieces of text, such as words and sentences. Strings do not have apredefined size, and there is no limit to the size or number of characters a string can have (sort of). We will explore strings in more depth in a later tutorial.

Variables

**a**and**b**are called*booleans*. A boolean variable has a size of only 1-bit, and can store just two values. Either*true*or*false.*They are used in conditional statements, which will be discussed in a later tutorial.

There are 9 primitive data types in java. The following table summarizes the information related to every data-type, including their respective

sizes and default values.

Data-type | Bit-length | Storage type | Default value |
---|---|---|---|

boolean | 1-bit | true/false | false |

byte | 8-bit | Signed integers | 0 |

short | 16-bit | Signed integers | 0 |

int | 32-bit | Signed integers | 0 |

long | 64-bit | Signed integers | 0 |

float | 32-bit | Signed floating point | 0.0f |

double | 64-bit | Signed floating point | 0.0d |

boolean | 1-bit | true/false | false |

char | 16-bit | Unicode characters, unsigned integers | '\u0000' |

String | Undefined | String values/Text | null |

## What next?

Each of the data-types mentioned in the table have their own unique characteristics, operations and properties.

We are going to explore each of them in depth in the tutorials ahead, while discussing what arithmetic operations

Java can offer. What are conditional statements, and how are they used? uzw..

We are going to explore each of them in depth in the tutorials ahead, while discussing what arithmetic operations

Java can offer. What are conditional statements, and how are they used? uzw..