You are on page 1of 12

DR.

APJ ABDUL KALAM TECHNICAL UNIVERSITY

Branch- CSE
Web Technology
Lecture-4

Variable, Arrays, Methods


By

Mr. Madhukar
Assistant Professor
Department of Information Technology
Krishna Engineering College, Ghaziabad
Java Variable
• Variable is a name of memory location where the value is stored.
• A variable is a container which holds the value. A variable is assigned with a
data type.
• There are three types of variable in java:
a) Local variable
b) Instance variable
c) Static variable
• A variable is the name of a reserved area allocated in memory. Its value can
be changed.
• Syntax: datatype variablename;
Java Variable contd.
• 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.
Syntax: method() {
datatype variableName; // Local variable
}
• Static variable: A variable that is declared as static is called a static variable. Here
“static” keyword is used to declare 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.
• Syntax: static datatype variableName; / static variable
Java Variable contd.
• 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. Every instance has its own copy of instance
variable.
• Syntax: class {
datatype variablename; //instance variable
}
Java Arrays
• an array is a collection of similar type of elements which has contiguous
memory location.
• Java array is an object which contains elements of a similar data type.
Additionally, The elements of an array are stored in a contiguous memory
location. It is a data structure where we store similar elements. We can store
only a fixed set of elements in a Java array.
• Array in Java is index-based, the first element of the array is stored at the 0th
index, 2nd element is stored on 1st index and so on.
• Types of Arrays in java:
a) Single Dimensional Array.
b) Multidimensional Array
Java Arrays contd.
• Single Dimensional Array in java: This is the array in which rows are fixed
but columns vary.
• Syntax to declare array in java:
i. datatype[] arrayName;
ii. datatype []arrayName;
iii. datatype arrayName[];

Instantiation of array in java:


i. datatype[] ArrayReferenceVariable = new datatype[size];
ii. datatype []ArrayReferenceVariable= new datatype[size];
iii. datatype ArrayReferenceVariable[] = new datatype[size];
We can declare, instantiate and initialize the java array together by:
Datatype arrayReferenceVariable[]={value,value}
Eg. Int a[]={2,3,8,10};
Java Arrays contd.
• data is stored in row and column based index (also known as matrix form).
Syntax to Declare Multidimensional Array in Java
i. datatype[][] arrayName;
ii. datatype [][]arrayName;
iii. datatype arrayName[][];

Instantiation of array in java:


i. datatype[][] ArrayReferenceVariable = new datatype[size][size];
ii. datatype [][]ArrayReferenceVariable= new datatype[size][size];
iii. datatype ArrayReferenceVariable[][] = new datatype[size][size];
We can declare, instantiate and initialize the java array together by:
Datatype arrayReferenceVariable[]={{value,value},{value,value}};
Methods in java
• method in Java is a collection of instructions that performs a specific task. It
provides the reusability of code.
• a method is a way to perform some task.
• A method is a block of code or collection of statements or a set of code
grouped together to perform a certain task or operation.
• Syntax to declare methods:
AccessSpecifier returntype methodname(){ method body}
Eg. Public int IT(){}
Methods in java contd.
• Access Specifier: Access specifier or modifier is the access type of the
method. It specifies the visibility of the method. Java provides four types of
access specifier:
• Public: The method is accessible by all classes when we use public specifier in
our application.
• Private: When we use a private access specifier, the method is accessible only
in the classes in which it is defined.
• Protected: When we use protected access specifier, the method is accessible
within the same package or subclasses in a different package.
• Default: When we do not use any access specifier in the method declaration,
Java uses default access specifier by default. It is visible only from the same
package only.
Methods in java contd.
Return Type: Return type is a data type that the method returns. It may have a
primitive data type, object, collection, void, etc. If the method does not return
anything, we use void keyword.
There are two types of method in java:
1. Predefined method: predefined methods are the method that is already
defined in the Java class libraries is known as predefined methods. It is also
known as the standard library method or built-in method. ome pre-
defined methods are length(), equals(), compareTo(), sqrt(), etc
2. User-defined method: these methods are defined by users.
User defined methods can be of two types:
a. Static method
b. Non-static method
Methods in java contd.
• Static method: static keyword is used to declare method as static.
Syntax: static method(){ body of method}
Eg: public static void meth(){
//body of method
}
• Non-static method in java:
Methods declare without static keyword.
Syntax: method(){}
Eg: public void meth(){
//body of method
}
Thank You

You might also like