UNIT-I
(INTRODUCTION)
BY: DR. AMAN TYAGI
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
A variable's data type determines
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
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:
The following chart summarizes the default values for the above data types.
Data Type Default Value (for fields)
Byte 0
Short 0
Int 0
Long 0L
Float 0.0f
Double 0.0d
Char '\u0000'
String (or any object) Null
Boolean False
Non-Primitive Data Type or
Reference Data Types
The Reference Data Types will contain a memory address of variable values because the reference
types won’t store the variable value directly in memory. They are strings, objects, arrays, etc.
1. Strings
Strings are defined as an array of characters. The difference between a character array and a string in
Java is, that the string is designed to hold a sequence of characters in a single variable whereas, a
character is a collection of separate char-type entities.
2. Class
A class is a user-defined blueprint or prototype from which objects are created. It represents the set of
properties or methods that are common to all objects of one type.
3. Object
An Object is a basic unit of Object-Oriented Programming and represents real-life entities. A typical Java
program creates many objects, which as you know, interact by invoking methods.
4. Interface
Like a class, an interface can have methods and variables, but the methods declared in an interface are by
default abstract (only method signature, no body).
5. Array
An Array is a group of like-typed variables that are referred to by a common name.
Strings are discussed in c programing. Class, Object, array and Interface will be discussed in the class.
Variables
A variable is a container which holds the value while the Java
program is executed. A variable is assigned with a data type.
Variable is a name of memory location. There are three types of
variables in java: local, instance and static.
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.
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a
value:
type variable = value;
Where type is one of Java's types (such as int or String),
and variable is the name of the variable (such as x or name).
The equal sign is used to assign values to the variable.
Example
Create a variable called name of type String and
assign it the value "John":
String name = "John";
System.out.println(name);
Dynamic Initialization In Java
When variable initialized at the time when it is declared this type
of initialization is called dynamic initialization .
Example Of Dynamic initialization
int count = 10;
In above example 10 is assign to a variable count at the time it is
declared.
Scope and lifetime of variables in
Java
Scope of a variable refers to in which areas or sections of a
program can the variable be accessed and lifetime of a variable
refers to how long the variable stays alive in memory.
General convention for a variable’s scope is, it is accessible only
within the block in which it is declared. A block begins with a left
curly brace { and ends with a right curly brace }.
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
An instance variable is a variable which is declared in a class but outside of
constructors, methods, or blocks. It is not declared as static.
It is called instance variable because its value is instance specific and is not
shared among instances.
3) Static variable
A variable which is declared as static is called static variable. It cannot be
local. You can create a single copy of static variable and share among all
the instances of the class. Memory allocation for static variable happens
only once when the class is loaded in the memory.
Arrays
An array is a container object that holds a fixed number of values of
a single type.
The length of an array is established when the array is created.
After creation, its length is fixed.
An array of 10 elements
Each item in an array is called an element, and each element is
accessed by its numerical index.
As shown in the preceding illustration, numbering begins with 0.
The 9th element, for example, would therefore be accessed at
index 8.
END