Variable
● 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
● It is the basic unit of storage in a program
● The value stored in a variable can be changed during program
execution.
● In Java, all the variables must be declared before use
● Also called as identifiers
● It can be used as class name, method name, interface name
and package name
Declaring a variable
Syntax
default
private
public Name of the
protected variable
Access-modifier data-type variable_name;
Access-modifier data-type variable_name=value;
Variable/Identifier naming rules
● Begin with a letter (A to Z or a to z), currency character ($) or an
underscore (_).
● After the first character, identifiers can have any combination of
characters
● A keyword cannot be used as an identifier
● Most importantly, identifiers are case sensitive
● Examples of legal identifiers: age, $salary, _value, _ _1_value
● Examples of illegal identifiers: 123abc, -salary
Example of variable declaration and initialization
int ivar; int ivar=10 ;
private double dvar; private double dvar=12.34;
public char cvar; public char cvar=’c’;
protected String svar; protected String svar=”java”;
Types of variable (based on scope)
Three types
● Local variable
● Instance variable
● Class variable
Local variable
● variable defined within a block or method or constructor is called local variable.
● Created when the block in entered or the function is called
● Destroyed after exiting from the block or when the call returns from the
function.
● Initialization of the variable is mandatory
● The scope of these variables exists only within the block in which the variable is
declared. i.e. we can access these variable only within that block .
Instance variable
● A variable declared inside the class but outside the body of the method,
is called instance variable
● Instance variables are non-static variables
● Created when an object of the class is created
● Destroyed when the object is destroyed.
● Initialization is not mandatory. Its default value is 0
● Instance Variable can be accessed only by objects
Class or Static variables
● Variable which is declared as static is called static variable.
● It cannot be local.
● Only one copy of a static variable per class
● Variables are created at the start of program execution
● Destroyed automatically when execution ends
● Initialization of static Variable is not Mandatory. Its default value is 0
● Static variable can be accessed through either with class or an object
Example public class A
{
static int m=100;//static variable
class Variable {
void method()
int amount = 100; // instance
{
variable
int n=90;//local variable
static int pin = 2315; // static variable }
public static void main(String[ ] args) { public static void main(String args[])
int age = 35; // local variable {
int data=50;//instance variable
}
}
}
}//end of class
Methods
Method statement can be
● Method declaration
● Method definition
● Method invocation/Call
Method Declaration
Syntax
default
private
public Type of input
protected Identifier
variable
access-specifier return-type methodName(data type of arguments);
Data type
Example- 1
void show();
● access-specifier – default
● return-type – void
● methodName – show
● data type of arguments – nil
Example- 2
void show(String);
● access-specifier – default
● return-type – void
● methodName – show
● data type of arguments – String
Example- 3
public int addValues(int, int);
● access-specifier – public
● return-type – int
● methodName – addValues
● data type of arguments – int, int
Method Definition
Syntax
default
private
public Declaration of
protected Data type Identifier
input variable
access-specifier return-type methodName(arguments){
method body;
}
Method example
Method example - 1
void show(){
System.out.println(“Object oriented programming”);
}
● access-specifier – default
● return-type – void
● methodName – show
● arguments – nil
Method example - 2
void show(String str){
System.out.println(str);
}
● access-specifier – default
● return-type – void
● methodName – show
● arguments – String str
Method example - 3
int addValues(int v1, int v2){
int v3 = v1 + v2
return v3;
}
● access-specifier – default
● return-type – int
● methodName – addValues
● arguments – int v1, int v2
Method definition
Method Invocation/Call
Syntax
Identifier input values for the
method
methodName(argument value(s));
Example
show();
show(“Java Programming”);
int sum=addValues(3, 4);
int large=max(7, 3);
Object
● Object is an instance of a class
● It is an real world entity
● Each object have state, behaviour and identity
Creating objects
Object in Java
Syntax
Name of Construct
the class or
ClassName object_name=new ClassName();
keyword
Example of an Object
Class with Object
Constructors
● Similar to method
● Method name is same as Class name
● No return type
● Can have arguments/parameters
● Cannot be invoked explicitly
● It is invoked when an instance of a class is created
● Can be overloaded
● Used to initialize the member of a class
● Two types
○ Default constructor
○ Parameterized constructor
Syntax for constructor
ClassName(argument(s) declaration) {
body of constructor;
}
Example
Class Example{
int ivar;
Example(){ //constructor
ivar=10;
}
public static void main(String[ ] args){
Example e1=new Example();
System.out.println(e1.ivar);
}
}
Types of constructors
● Default constructor
○ Constructor without parameter
● Parameterized constructor
○ Constructor with parameter
Example for Constructor
class Constructor{
Constructor(){
System.out.println(“Default constructor”);
}
Constructor(String str){
System.out.println(“Parameterized constructor”);
}
}
main() method
Syntax
public static void main(String[ ] args){
body of main method;
}
Input statement
Syntax final member
import java.util.*; //package
from System
class of type
Class from InputStream
class lang package
Scanner scan=new Scanner(System.in);
construct
object
or
Output statement
Method from
Syntax PrintStream
Class from class
lang package
System.out.print(“string value”);
final member of
System class of
type PrintStream
static keyword
● Variable
● Method
● Block
● Nested class
static variable
● Variable with static keyword
● Also known as class variable Syntax
● Gets memory only once static data-type
● One memory space for a class variable_name;
● Initialize with 0 Example
● Can be accessed by class name static int var;
static method
Syntax
● Method with static keyword
static return-type
method_name(arguments){
● Can be accessed by class name
method body;
}
● Access only static members
Example
● It will preserve the last value static void show(String str){
System.out.println(str);
● Belongs to class rather than the object of a class
}
static block
Syntax
● Block with static keyword
static{
statement(s);
● Executed prior to main() method
}
● Used to initialize static members
Example
static{
● Executed only once
System.out.println(“static
block ”);
}
Data types
Data types
Data Type Description Default Default size
Value
boolean Stores true or false values false 1 bit
char Stores a single character/letter or ASCII values '\u0000' 2 byte
byte Stores whole numbers from -128 to 127 0 1 byte
short Stores whole numbers from -32,768 to 32,767 0 2 byte
int Stores whole numbers from -2,147,483,648 to 2,147,483,647 0 4 byte
long Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L 8 byte
float Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits 0.0f 4 byte
3.4e−038 to 3.4e+038.
double Stores fractional numbers. Sufficient for storing 15 decimal digits 0.0d 8 byte
1.7e−308 to 1.7e+308
byte data type
Syntax
byte varname;
public class MyClass {
public static void main(String[] args) {
byte myNum = 100;
System.out.println(myNum);
}
}
Output
100
short data type
Syntax
short varname;
public class MyClass {
public static void main(String[] args) {
short myNum = 1000;
System.out.println(myNum);
}
}
Output
1000
int data type
Syntax
int varname;
public class MyClass {
public static void main(String[] args) {
int myNum = 10000;
System.out.println(myNum);
}
}
Output
10000
long data type
Syntax
long varname;
public class MyClass {
public static void main(String[] args) {
long myNum = 100000L;
System.out.println(myNum);
}
}
Output
100000
float data type
Syntax
float varname;
public class MyClass {
public static void main(String[] args) {
float myNum = 2.56f;
System.out.println(myNum);
}
}
Output
2.56
double data type
Syntax
double varname;
public class MyClass {
public static void main(String[] args) {
double myNum = 12.56d;
System.out.println(myNum);
}
}
Output
12.56
Scientific number
Syntax
float/double varname;
public class MyClass {
public static void main(String[] args) {
float myNum1 = 12e3f;
double myNum2 = 5e4d;
System.out.println(myNum1);
System.out.println(myNum2);
}
}
Output
12000.0
boolean datatype
Syntax
boolean varname;
public class MyClass {
public static void main(String[] args) {
boolean isBlue = true;
boolean isRed = false;
System.out.println(isBlue);
System.out.println(isRed);
}
}
Output
true