Java Tutorial #1 – Variables and Data Types

Table of Contents

Introduction

Variables are names that are used to identify memory locations that store values. Each variable belongs to a particular data type. Data types determine the size of the memory location.In this article, we will be exploring Java variables and data types in detail.

1. Variables

A variable can be used to refer to a value at a memory location. The code can use and modify the value at the memory location using the variable.   A variable needs to be declared and initialized before it can be used.

Declaring a Variable

In order to declare a variable, you need to specify its data type and a name for the variable as shown below:

int i; //declares a variable called i of int data type

Here, int is the data type. It is used to store a non-negative positive or negative whole number. Just like int, there are several other data types supported by Java which are covered in the next section.i is the variable name. It points to the memory location which the operating system has allocated.
You can also declare several variables at once as follows:

int i,j,k; //declares 3 variables called i,j,k of int data type

Note that the variable names are separated using commas.

Initializing a Variable

When a value is assigned to a variable, the variable is said to be initialized.  There are several ways you can initialize a variable:

Initializing at the time of declaration

You can initialize a variable when it is declared as follows:

int i = 100; // creates a variable i and sets it’s value to 100
Initializing a variable later on

You can declare a variable first and initialize it later on as follows:

int i; // declares int variable i
//do something else
i = 100; // sets i to 100
Initializing a variable using an expression

You can also use an expression to initialize as variable as follows:

int sum = 10+6; //sets sum to 16
int sum2 = a+b; // adds the values in a and b and assigns the result to sum2
Using a Variable

Once you declare and initialize a variable, you can use it in your code as follows:

int a = b+27; // uses b in an expression
System.out.println(“a = “+a); // prints value of a
if(a > 20) { // uses a in an if statement
// do something
}

2. Data Types

Data types determine the type of data that a variable can contain and its size. The operating system allocates memory to a variable based its data type. Java supports several data types which can be used to store different type of data. Data types can be categorized as follows:

1) Primitive Data Types

Primitive data types are types used to store a single value. They are basically used to store numbers and characters.  Primitive data types can be further classified into the following groups:

(a) Integer Data Types

Integer data types can be used to store whole numbers, that is non-fractional positive or negative numbers. Java supports byte, short, int and long integer data types as detailed below:

Name Width (Bits) Range Example
byte 8 –128 to 127 byte b = 100; // creates variable b of byte data type with value 100

 

byte b1 = 128; //will cause error as maximum value is 127

short 16 –32,768 to 32,767 short s = 1000; // creates variable s of short data type with value 1000
int 32 –2,147,483,648 to 2,147,483,647 int i = -4507; // creates variable i of int data type with value -4507
long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 long l = 125000000; // create variable l of long data type with value 125000000

As can be seen from the table above, the difference between them is the range of values that they can store. While byte can be used to store very small numbers, long can be used to store bigger numbers.

(b) Decimal Data Types

Decimal data types can be used to store decimal numbers that is numbers having a fractional component. Java supports the float and double data types to store decimal numbers as detailed below:

Name Width (Bits) Range Example
float 32 1.4e–045 to 3.4e+038 float f = 2.5; // compilation error

 

float f1 = 3.5f; //creates a float variable f1 with value as 3.5

double 64 4.9e–324 to 1.8e+308 double d = 100.25;   // creates a double variable d with value as 100.25

The difference between the two is the precision level. Float specifies a single precision value whereas double specifies a double precision value. Single precision takes less space as compared to double precision but are not as accurate as double precision, particularly for very large or very small numbers.By default, when you create a decimal number, Java expects it to be of double data type, so if you want to explicitly create a float value, you need to append an ‘f’ at the end as done above.

(c) Characters

Java supports the char data type to store characters. This can be used to store characters like letters, numbers and symbols as shown below:

Name Width (Bits) Range Example
char 16 0 to 65536 char c = ‘a’; // creates a char variable with value ‘a’

Java uses Unicode to store characters. Unicode is an international character set that can store characters from many languages. So in addition to the English language character like ‘a’, b’, etc, the char data type can be used to store characters from other languages too.

(d) Boolean

Java supports the boolean data type to store ‘true’ or ‘false’ as demonstrated below:

Name Width (Bits) Range Example
boolean 1 true/false boolean b = true; // assigns the value true to b

The boolean data type is basically used for comparisons or to test conditions.

3. Reference Data Types

In addition to primitive data types, Java supports an additional data type called reference data type. These are used to hold more complex data created using the primitive data types in the form of objects. This will be covered later on.

Conclusion

So this article covers declaring, initializing and using Java variables. It also walks you through the various data types supported by Java.

Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

JAVA TUTORIALS

Recent Posts

RELATED POSTS

Java Tutorial #4 – Control Statements

Introduction Control statements are used to change the flow of execution based on changes to certain variables in the code. One of the types of...

Java Tutorial #6 – Jump Statements

Introduction The break statement is used to stop further execution. It can be used either in a loop or within a switch statement. Break Statement The break...

Java Tutorial #5 – Loop Statements

Iteration statements are used to repeat a particular block of code until a certain condition is true. In this article, we will be taking...

Java Tutorial #7 Part 1 – Classes & Objects

Classes are a very important concept in Java. Java code always needs to be written in a class. In this article, we will be...

Â

CHECKOUT 'HOW-TOs'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string...

MORE ON CODEX

MORE IN THIS CATEGORY

Convert String to Date Using SimpleDateFormat Class

Sometimes, you may need to convert a String to a java.util.Date object. For this, you need to use the SimpleDateFormat class. You need to...

How To Create Maven Project in Eclipse Without Archetype

Maven is a very popular build and dependency management tool. Eclipse is an IDE that helps developers write and run Java code easily. In...

Create Basic Hibernate Program

Hibernate is an ORM framework that helps in accessing database tables from Java code. It is an object-oriented way of accessing database tables and...

Java Tutorial #3 – Java Arrays

Table of Contents One Dimensional Array Declaring Array Allocting Memory to Array Accessing Array Elements Initializing Array Length of Array Multi Dimensional Array Creating 3...

OTHER TUTORIALS

How To Sort List in Java

Introduction A List is an interface in the Java collection framework. It can be used to store objects. Programmers often...
- Advertisement -spot_img