Java Variables and Data Types with Program Example

In this tutorial we will study and understand the concept of what are variables and the different Data types of variables in Java. Java is a strictly typed language, thus we need to specify in advance what kind(integer, character, floating point, boolean etc) of data are we going to store in the variables. Thus it is important to understand the concepts of variables and Data types.

  • Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of “vary + able” that means its value can be changed.
  • It is the basic unit of storage in a program.
  • The value stored in a variable can be changed during program execution.
  • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.

Rules for naming variables & other entities
  • Identifiers are the names of variables, methods, classes, packages and interfaces.
  • Following rules while creating these identifiers –
  • Identifiers must be composed of letters, numbers, the underscore _ and the dollar sign $.
  • Identifiers may only begin with a letter, the underscore or a dollar sign.
  • The main restriction on the names you can give your variables is that they cannot contain any white space.
  • You cannot begin a variable name with a number.
  • All variable names are case-sensitive
  • There is no limit to the length of a Java variable name.
  • Also identifiers cannot be java predefined keywords. Following are some predefined keyword in java –
volatilewhile true false


Data Types in Java

java datatypes

Java Data types with Default value and Default Size
Data TypeDefault ValueDefault size
booleanfalse1 bit
char‘\u0000’2 byte
byte01 byte
short02 byte
int04 byte
long0L8 byte
float0.0f4 byte
double0.0d8 byte
Types of Variables in Java

Depending upon where the variables are used, they can be categorized into 3 different types –

  • Local variable – A variable declared inside the method is called local variable.
  • Instance variable – A variable declared inside the class but outside the method, is called instance variable . It is not declared as static.
  • Static variable – A variable which is declared as static is called static variable. It cannot be local. We will study about static variables in detail.

types of java variables

Program example on Variables and Data Types in Java
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *