JAVA VARIABLE &
DATA TYPE
By : Kapil Manchandani
TABLE OF CONTENT
1 : What is Variable ?
2: Types of Variable
3: What is Data Type ?
WHAT IS VARIABLE ?
Variables are containers for storing data values.
Variable in Java is a data container that saves the data values during
Java program execution. Every variable is assigned a data type that
designates the type and quantity of value it can hold. A variable is a
memory location name for the data.
A variable's name can be any legal identifier — an unlimited-length
sequence of Unicode letters and digits, beginning with a letter, the dollar
sign " $ ", or the underscore character " _ ". The convention, however, is
to always begin your variable names with a letter, not " $ " or " _ ".
TYPES OF VARIABLE
there are different types of variables,
String - stores text, such as "Hello". String values are surrounded by
double quotes
int - stores integers (whole numbers), without decimals, such as 123 or -
123
float - stores floating point numbers, with decimals, such as 19.99 or -
19.99
char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
boolean - stores values with two states: true or false
To create a variable, you must specify the type and assign it a value:
DECLARING (CREATING) VARIABLES
Syntax
type variableName = value;
Where type is one of Java's types (such as int or String),
and variableName is the name of the variable (such as x or name).
The equal sign is used to assign values to the variable.
To create a variable that should store text, look at the following example:
Example
Create a variable called name of type String and assign it the value
"John":
String name = "John"; System.out.println(name);
EXAMPLES
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
JAVA PRINT VARIABLES
Display Variables : The println() method is often used to display variables.
To combine both text and a variable, use the + character:
String name = "John"; System.out.println("Hello " + name);
You can also use the + character to add a variable to another variable.
Code :
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName; System.out.println(fullName);
or numeric values, the + character works as a mathematical operator
(notice that we use int (integer) variables here):
int x = 5;
int y = 6;
System.out.println(x + y); // Print the value of x + y
DECLARE MANY VARIABLES
To declare more than one variable of the same type, you can use a
comma-separated list:
Example
Instead of writing:
int x = 5;
int y = 6;
int z = 50;
System.out.println(x + y + z);
You can simply write:
int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
JAVA IDENTIFIERS
All Java variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names
(age, sum, totalVolume).
Note: It is recommended to use descriptive names in order to create
understandable and maintainable code:
// Good
int minutesPerHour = 60;
// OK, but not so easy to understand what m actually is
int m = 60;
THE GENERAL RULES FOR NAMING
VARIABLES ARE
Names can contain letters, digits, underscores, and dollar signs
Names must begin with a letter
Names should start with a lowercase letter and it cannot contain
whitespace
Names can also begin with $ and _ (but we will not use it in this tutorial)
Names are case sensitive ("myVar" and "myvar" are different variables)
Reserved words (like Java keywords, such as int or boolean) cannot be
used as names
TYPES OF VARIABLES
There are three types of variables in Java:
local variable
instance variable
static variable
1) Local Variable
A variable declared inside the body of the method is called local variable.
You can use this variable only within that method and the other methods in
the class aren't even aware that the variable exists.
A local variable cannot be defined with "static" keyword.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is
called an instance variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is
not shared among instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be
local. You can create a single copy of the static variable and share it
among all the instances of the class. Memory allocation for static variables
happens only once when the class is loaded in the memory
Example to understand the types of variables in java
public class A
{
static int m=100;//static variable
public static void main(String args[])
{
int data=50;//instance variable
}
void method()
{
int n=90;//local variable
}
}//end of class
Java Variable Example: Add Two Numbers
public class Simple{
public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}
DATA TYPES IN JAVA
Data types specify the different sizes and values that can be stored in the
variable. There are two types of data types in Java:
Primitive data types: The primitive data types include boolean, char,
byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types include Classes,
Interfaces, and Arrays.
Data types are divided into two groups:
Primitive data types -
includes byte, short, int, long, float, double, boolean and char
Non-primitive data types - such as String, Arrays and Classes (you will
learn more about these in a later chapter)
PRIMITIVE DATA TYPES
A primitive data type specifies the size and type of variable values, and it
has no additional methods.
There are eight primitive data types in Java:
EXERCISES
myNum = 9;
myFloatNum = 8.99f;
myLetter = 'A';
myBool = false;
myText = "Hello World";