Module 2
Module 2
Example2
Test test = new Test();
System.out.println(test.instanceVariable);
}
}
Output
10
Method Overloading
If a class has multiple methods having
same name but different in
parameters, it is known as Method
Overloading.
Pass by Value
Any modifications to the formal parameter variable inside the called
function or method affect only the separate storage location and will not
be reflected in the actual parameter in the calling environment.
Pass by reference
Any changes to the formal parameter are reflected in the actual
parameter in the calling environment as formal parameter receives a
reference (or address) to the actual data.
class CallByValue {
Pass by
{
x++;
Value y++;
}
}
public class Main {
public static void main(String[] args)
{
int a = 10;
int b = 20;
CallByValue object = new CallByValue();
object.Example(a, b);
Reference {
obj.a += 10;
obj.b += 20;
}
}
public class Main {
object.ChangeValue(object);
Argument
For example, a program can call Math method sqrt with
promotion an integer argument even though the method expects
to receive a double argument (but, as we will soon see,
not vice versa).
}
}
Output
4.0
5
Scope of Variables In Java
Scope of a variable is the part of the program where the
variable is accessible.
Class Level
Method Level
Block Level
We can declare class variables anywhere in class, but
outside methods.
Example
}
//System.out.println(x);generate error if removes
comment
System.out.println("End");
}
}
Output
Start
10
End
Arrays and Array List in Java
An array is a group of like-typed variables that are
referred to by a common name.
Arrays in Java work differently than they do in C/C++.
Or
Output
3
Ford
Multidimensional A multidimensional array is an array of arrays.
Arrays Each element of a multidimensional array is an array
itself.
For example,
int[][] a = new int[3][4];
int[][] a = new int[3][4];
Memory Layout view
Here is how we can initialize a 2-dimensional array in
Java.
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
Initializing };
Example
// calculate the length of each row
System.out.println("Length of row 1: " + a[0].length);
System.out.println("Length of row 2: " + a[1].length);
System.out.println("Length of row 3: " + a[2].length);
}
}
Output:
Length of row 1: 3
Length of row 2: 4
Length of row 3: 1
public class Main
{
public static void main(String[] args) {
int a[]={33,3,4,5};
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
Array and }
Loop }
}
Output
1
2
3
4
5
6
7
import java.util.Scanner;
public class ArrayInputExample1
{
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
n=sc.nextInt();
int[] array = new int[n];
System.out.println("Enter the elements of the array: ");
Keyboard array[i]=sc.nextInt();
}
System.out.println("Array elements are: ");
for (int i=0; i<n; i++)
{
System.out.println(array[i]);
}
}
}
Output
Enter the number of elements you want to store:
5
Enter the elements of the array:
12345
Array elements are:
1
2
3
4
5
You can pass arrays to a method just like normal
variables.
When we pass an array to a method as an argument,
actually the address of the array in the memory is
Passing passed (reference).
Arrays to Therefore, any changes to this array in the method will
affect the array.
Methods
import java.util.Scanner;
class Example{
void Change(int[] a,int size)
{
for(int i=0;i<size;i++)
Example {
a[i]=a[i]+10;
}
}
}
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array that is to be created::");
int size = sc.nextInt();
int[] myArray = new int[size];
System.out.println("Enter the elements of the array ::");
for(int i=0; i<size; i++)
myArray[i] = sc.nextInt();
System.out.println("Array before function call");
for(int i=0; i<size; i++)
System.out.println(myArray[i]);
Example ob=new Example();
ob.Change(myArray,size);
System.out.println("Array after function call");
for(int i=0; i<size; i++)
System.out.println(myArray[i]);
}
}
Output
5
Enter the elements of the array ::
12345
Array before function call
1
2
3
4
5
Array after function call
11
12
13
14
15
public class Main{
public static void main(String[] args) {
int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
acceptIt(a); // pass it to the method
}
Passing 2D
for (int i = 0; i< a.length; i++) {
Array to for (int j = 0; j < a[i].length; j++) {
Method System.out.print(a[i][j] + "\t");
}
System.out.println("");
}
}
}
Output
Elements are :
1 2 3
4 5 6
7 8 9
ArrayList is a part of collection framework and is present
in java.util package.
Java ArrayList class uses a dynamic array for storing the
elements.
ArrayList
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
Adding cars.add("Mazda");
elements System.out.println(cars);
}
}
Output
[Volvo, BMW, Ford, Mazda]
Access an Item
To access an element in the ArrayList, use
the get() method and refer to the index number:
Example
cars.get(0);
Change an Item
Other To modify an element, use the set() method and refer
Operations to the index number:
Example
cars.set(0, "Opel");
Remove an Item
To remove an element, use the remove() method and
refer to the index number:
Example
cars.remove(0);
Output
[10, 15, 20, 25]
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
for (int i = 0; i < cars.size(); i++) {
System.out.println(cars.get(i));
}
Loop }
Through an }
ArrayList
Output
Volvo
BMW
Ford
Mazda
In Java, the for-each loop is used to iterate through
elements of arrays and collections (like ArrayList).
It is also known as the enhanced for loop.
The advantage of the for-each loop is that it eliminates
the possibility of bugs and makes the code more
readable.
It is known as the for-each loop because it traverses
Enhanced each element one by one.
for The drawback of the enhanced for loop is that it cannot
traverse the elements in reverse order.
Statement Here, you do not have the option to skip any element
because it does not work on an index basis.
The syntax of Java for-each loop consists of data_type
with the variable followed by a colon (:), then array or
collection.
}
}
Output:
vimal
sonoo
ratan
Commandline Arguments in Java
The java command-line argument is an argument i.e.
passed at the time of running the java program.
The arguments passed from the console can be
Java received in the java program and it can be used as an
input.
Command
Line
arguments
class Example{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
Example1 }
}
Compile by > javac Example.java
Run by > java Example hai
Output:
Your first argument is: hai
class A{
public static void main(String args[]){
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
Example2
}
}
compile by > javac A.java
run by > java A sonoo jaiswal 1 3 abc
Output:
sonoo
jaiswal
1
3
abc
class Main {
public static void main(String[] args) {
Output
11
23
Java Packages
A java package is a group of similar types of classes,
interfaces and sub-packages.
Package in java can be categorized in two form, built-in
package and user-defined package.
Java There are many built-in packages such as java, lang,
defined }
Output
Package
Welcome to package
//save by A.java
package pack;
public class A{
Importing public void msg(){System.out.println("Hello");}
Package in }
Java
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output
Hello
Sample Questions