Core Java (J2SE) Madhusudhan
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.
✓ Each item in an array is called an element, and each element is accessed by its numerical
index.
The Index begins with 0. The 9th element, for example, would therefore be accessed at index 8.
Declaration:
Type 1:
[Link]: int[] a;
[Link]: a = new int[5];
[Link]: a[0] = 10; a[1] = 20;…
Type 2:
int[] a = {10,20,30,40,50};
Possibilities of array declaration:
Single dimensional array:
int[] a;//recommended
int []a;
int a[];
Multi-dimensional array:
int[][] a;//recommended
int []a[];
int a[][];
Example:
public class Test {
public static void main(String[] args) {
int[] x;// declaration
x = new int[5]; // instantiation
// Initialization
x[0] = 10;
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
x[1] = 20;
x[2] = 30;
x[3] = 40;
x[4] = 50;
for (int i = 0; i < 5; i++) {
[Link](x[i]);
}
}
}
Output:
10
20
30
40
50
Example:
public class Test {
public static void main(String[] args) {
int[] x;// declaration
x = new int[5]; // instantiation
// Initialization
x[0] = 10;
x[1] = 20;
x[3] = 40;
for (int i = 0; i < 5; i++) {
[Link](x[i]);
}
}
}
Output:
10
20
0
40
0
Example:
public class Test {
public static void main(String[] args) {
int[] x;// declaration
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
x = new int[5]; // instantiation
// Initialization
x[0] = 10;
x[1] = 20;
x[2] = 30;
x[3] = 40;
x[4] = 50;
// Individual values
[Link](x[0]);
[Link](x[1]);
[Link](x[2]);
[Link](x[3]);
[Link](x[4]);
// By using for loop
for (int i = 0; i < 5; i++) {
[Link](x[i]);
}
// For-Each loop from JDK 1.5v
for (int i : x) {
[Link](i);
}
}
}
Note:
int[] x = new int[5];
When we create an array with size, the array is created with default values internally.
When we insert a[0]=10; default value is replaced with 10.
Example for the above Note:
class A {
}
public class Test {
public static void main(String[] args) {
int[] a = new int[5];
for (int aa : a) {
[Link](aa);
}
boolean[] b = new boolean[5];
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
for (boolean bb : b) {
[Link](bb);
}
A[] obj = new A[3];// A is user defined type
for (A obj1 : obj) {
[Link](obj1);
}
}
}
Output:
0
0
0
0
0
false
false
false
false
false
null
null
null
Example:
class A {
}
public class Test {
public static void main(String[] args) {
int[] a = new int[5];
a[0] = 100;
a[3] = 200;
a[4] = 300;
for (int aa : a) {
[Link](aa);
}
boolean[] b = new boolean[5];
b[0] = false;
b[2] = true;
b[3] = false;
for (boolean bb : b) {
[Link](bb);
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
A[] obj = new A[3];// A is user defined type
for (A obj1 : obj) {
[Link](obj1);
}
}
}
Example for User defined Classes:
class Employee {
int empId;
String empName;
double empSalary;
public Employee(int empId, String empName, double empSalary) {
[Link] = empId;
[Link] = empName;
[Link] = empSalary;
}
}
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee(101, "John", 35500.00);
Employee[] emp = new Employee[5];
emp[0] = e1;
emp[2] = new Employee(103, "Miller", 45500.00);
emp[4] = new Employee(105, "Scott", 55500.00);
for (Employee e : emp) {
[Link](e);
}
}
}
Output:
Employee@15db9742
null
Employee@6d06d69c
null
Employee@7852e922
Example Display Employee Information:
class Employee {
int empId;
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
String empName;
double empSalary;
public Employee(int empId, String empName, double empSalary) {
[Link] = empId;
[Link] = empName;
[Link] = empSalary;
}
void empDetails() {
[Link](empId + "\t" + empName + "\t" +
empSalary);
}
}
public class Test {
public static void main(String[] args) {
Employee[] emp = new Employee[5];
emp[0] = new Employee(101, "John", 35500.00);
emp[1] = new Employee(102, "Miller", 45500.00);
emp[2] = new Employee(103, "Scott", 55500.00);
emp[3] = new Employee(104, "Smith", 45500.00);
emp[4] = new Employee(105, "King", 55500.00);
for (Employee e : emp) {
[Link]();
}
}
}
Output:
101 John 35500.0
102 Miller 45500.0
103 Scott 55500.0
104 Smith 45500.0
105 King 55500.0
Example for Display Employee Information:
class Employee {
int empId;
String empName;
double empSalary;
public Employee(int empId, String empName, double empSalary) {
[Link] = empId;
[Link] = empName;
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
[Link] = empSalary;
}
void empDetails() {
[Link](empId + "\t" + empName + "\t" +
empSalary);
}
}
public class Test {
public static void main(String[] args) {
Employee[] emp = new Employee[5];
emp[0] = new Employee(101, "John", 35500.00);
emp[1] = new Employee(102, "Miller", 45500.00);
emp[3] = new Employee(104, "Smith", 45500.00);
emp[4] = new Employee(105, "King", 55500.00);
for (Employee e : emp) {
[Link]();
}
}
}
Output:
101 John 35500.0
102 Miller 45500.0
Exception in thread "main" [Link]
Example for Handling Null Values
class Employee {
int empId;
String empName;
double empSalary;
public Employee(int empId, String empName, double empSalary) {
[Link] = empId;
[Link] = empName;
[Link] = empSalary;
}
void empDetails() {
[Link](empId + "\t" + empName + "\t" +
empSalary);
}
}
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
public class Test {
public static void main(String[] args) {
Employee[] emp = new Employee[5];
emp[0] = new Employee(101, "John", 35500.00);
emp[1] = new Employee(102, "Miller", 45500.00);
emp[3] = new Employee(104, "Smith", 45500.00);
emp[4] = new Employee(105, "King", 55500.00);
for (Employee e : emp) {
if (e == null) {
continue;
}
[Link]();
}
}
}
Output:
101 John 35500.0
102 Miller 45500.0
104 Smith 45500.0
105 King 55500.0
Case:
for (Employee e : emp) {
if (e != null) {
[Link]();
}
}
Case:
for (Employee e : emp) {
if (e instanceof Employee) {
[Link]();
}
}
Example:
Problem: One employee array is there with 1000 elements. Out of 1000 elements 2 elements
locations are null. I want that 2 locations.
class Employee {
int empId;
String empName;
double empSalary;
public Employee(int empId, String empName, double empSalary) {
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
[Link] = empId;
[Link] = empName;
[Link] = empSalary;
}
void empDetails() {
[Link](empId + "\t" + empName + "\t" +
empSalary);
}
}
public class Test {
public static void main(String[] args) {
Employee[] emp = new Employee[5];
emp[0] = new Employee(101, "John", 35500.00);
emp[1] = new Employee(102, "Miller", 45500.00);
emp[3] = new Employee(104, "Smith", 45500.00);
for (int i = 0; i < [Link]; i++) {
if (emp[i] == null) {
[Link]("Location : "+i);
}
}
}
}
Output:
Location : 2
Location : 4
Day 9/24/2021 10:19 PM
length Vs length():
[Link]: length is a final variable applicable for arrays. With the help of the length variable,
we can obtain the size of the array.
[Link]() : length() method of String class returns the length of this string. The length is
equal to the number of Unicode code units in the string.
Java program to illustrate the concept of length and length()
public class Test {
public static void main(String[] args) {
// Here array is the array name of int type
int[] array = new int[4];
[Link]("The size of the array is " +
[Link]);
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
// Here str is a string object
String str = "Leela Soft";
[Link]("The size of the String is " +
[Link]());
}
}
Output:
The size of the array is 4
The size of the String is 10
What will be the output of the following program?
public class Test {
public static void main(String[] args) {
// Here str is the array name of String type.
String[] str = { "Java", "Python", "C#" };
[Link]("The length of the String array : " +
[Link]);
}
}
Output:
The length of the String array : 3
What will be the output of the following program?
public class Test {
public static void main(String[] args) {
String[] str = { "Java", "Python", "C#" };
[Link]([Link]());
}
}
Output: Compile Time Error
error: cannot find symbol
[Link]([Link]());
^
symbol: method length()
location: variable str of type String[]
What will be the output of the following program?
public class Test {
public static void main(String[] args) {
// Here str[0] pointing to a string i.e. Java
String[] str = { "Java", "Python", "C#" };
[Link](str[0].length());
}
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
Output: 4
What will be the output of the following program?
public class Test {
public static void main(String[] args) {
String[] s = new String[5];
s[0] = "Java";
s[1] = "Python";
s[3] = "C#";
[Link](s[2].length());
}
}
Output:
Exception in thread "main" [Link]
What will be the output of the following program?
public class Test {
public static void main(String[] args) {
String[] s = new String[5];
s[0] = "Java";
s[1] = "Python";
s[3] = "C#";
[Link](s[5].length());
}
}
Output:
Exception in thread "main" [Link]: 5
Method return type is an array:
public class Test {
public static void main(String[] args) {
int[] i = Test.m1();
for (int i1 : i)
[Link](i1);
}
static int[] m1() {
int[] x = { 10, 20, 30, 40, 50 };
return x;
}
}
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
Method argument is an array:
public class Test {
public static void main(String[] args) {
double[] d = new double[5];
d[0] = 12.3;
d[2] = 22.3;
d[4] = 32.3;
Test.m1(d);
}
static void m1(double[] d) {
for (double d1 : d)
[Link](d1);
}
}
Output:
12.3
0.0
22.3
0.0
32.3
Multi-Dimensional Arrays:
public class Test {
public static void main(String[] args) {
int[][] x = new int[2][3];
x[0][0] = 10;
x[0][1] = 20;
x[0][2] = 30;
x[1][0] = 40;
x[1][1] = 50;
x[1][2] = 60;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
[Link](x[i][j] + " ");
}
[Link]();
}
}
}
Day 9/25/2021 10:04 PM
Different Types of Array Objects:
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
class Employee {
int empId;
String empName;
public Employee(int empId, String empName) {
[Link] = empId;
[Link] = empName;
}
}
class Customer {
int custId;
String custName;
public Customer(int custId, String custName) {
[Link] = custId;
[Link] = custName;
}
}
public class Test {
public static void main(String[] args) {
Employee[] emp = new Employee[2];
emp[0] = new Employee(101, "John");
emp[1] = new Employee(103, "Miller");
Customer[] cust = new Customer[2];
cust[0] = new Customer(201, "Adam");
cust[1] = new Customer(202, "King");
String[] str = new String[2];
str[0] = "Java";
str[1] = "Python";
for (String s : str)
[Link](s);
for (Employee e : emp)
[Link]([Link] + "\t" + [Link]);
for (Customer c : cust)
[Link]([Link] + "\t" + [Link]);
}
}
Output:
Java
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
Python
101 John
103 Miller
201 Adam
202 King
Different Types of object within single array object:
class Employee {
int empId;
String empName;
public Employee(int empId, String empName) {
[Link] = empId;
[Link] = empName;
}
}
class Customer {
int custId;
String custName;
public Customer(int custId, String custName) {
[Link] = custId;
[Link] = custName;
}
}
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee(101, "John");
Employee e2 = new Employee(103, "Miller");
Customer c1 = new Customer(201, "Adam");
Customer c2 = new Customer(202, "King");
String s1 = "Java";
String s2 = "Python";
Object[] obj = new Object[10];
obj[0] = e1;
obj[1] = e2;
obj[3] = c1;
obj[4] = c2;
obj[6] = s1;
obj[7] = s2;
obj[9] = new Test();
for(Object o : obj)
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
[Link](o);
}
}
Different Types of object within single array object with Exception:
class Employee {
int empId;
String empName;
public Employee(int empId, String empName) {
[Link] = empId;
[Link] = empName;
}
}
class Customer {
int custId;
String custName;
public Customer(int custId, String custName) {
[Link] = custId;
[Link] = custName;
}
}
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee(101, "John");
Employee e2 = new Employee(103, "Miller");
Customer c1 = new Customer(201, "Adam");
Customer c2 = new Customer(202, "King");
String s1 = "Java";
String s2 = "Python";
Object[] obj = new Object[10];
obj[0] = e1;
obj[1] = e2;
obj[3] = c1;
obj[4] = c2;
obj[6] = s1;
obj[7] = s2;
obj[9] = new Test();
for (Object o : obj) {
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
Employee e = (Employee) o;
[Link]([Link] + "\t" + [Link]);
}
}
}
101 John
103 Miller
Exception in thread "main" [Link]
Example:
class Employee {
int empId;
String empName;
public Employee(int empId, String empName) {
[Link] = empId;
[Link] = empName;
}
}
class Customer {
int custId;
String custName;
public Customer(int custId, String custName) {
[Link] = custId;
[Link] = custName;
}
}
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee(101, "John");
Employee e2 = new Employee(103, "Miller");
Customer c1 = new Customer(201, "Adam");
Customer c2 = new Customer(202, "King");
String s1 = "Java";
String s2 = "Python";
Object[] obj = new Object[10];
obj[0] = e1;
obj[1] = e2;
obj[2] = c1;
obj[3] = c1;
obj[4] = c2;
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
obj[5] = e1;
obj[6] = s1;
obj[7] = s2;
obj[8] = s2;
obj[9] = new Test();
for (Object o : obj) {
Employee e = (Employee) o;
[Link]([Link] + "\t" + [Link]);
}
}
}
Example:
class Employee {
int empId;
String empName;
public Employee(int empId, String empName) {
[Link] = empId;
[Link] = empName;
}
}
class Customer {
int custId;
String custName;
public Customer(int custId, String custName) {
[Link] = custId;
[Link] = custName;
}
}
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee(101, "John");
Employee e2 = new Employee(103, "Miller");
Customer c1 = new Customer(201, "Adam");
Customer c2 = new Customer(202, "King");
String s1 = "Java";
String s2 = "Python";
Object[] obj = new Object[10];
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
obj[0] = e1;
obj[1] = e2;
obj[2] = c1;
obj[4] = c2;
obj[6] = s1;
obj[8] = s2;
obj[9] = new Test();
for (Object o : obj) {
if (o instanceof Employee) {
Employee e = (Employee) o;
[Link]([Link] + "\t" + [Link]);
} else if (o instanceof Customer) {
Customer c = (Customer) o;
[Link]([Link] + "\t" + [Link]);
} else if (o instanceof String) {
String s = (String) o;
[Link](s);
} else if (o instanceof Test) {
Test t = (Test) o;
t.m1();
} else {
[Link](o);
}
}
}
void m1() {
[Link]("m1 from Test");
}
}
Jagged Array in Java:
class Employee {
int empId;
String empName;
public Employee(int empId, String empName) {
[Link] = empId;
[Link] = empName;
}
}
class Customer {
int custId;
String custName;
public Customer(int custId, String custName) {
[Link] = custId;
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
[Link] = custName;
}
}
public class Test {
public static void main(String[] args) {
Employee[] emp = new Employee[2];
emp[0] = new Employee(101, "John");
emp[1] = new Employee(103, "Miller");
Customer[] cust = new Customer[2];
cust[0] = new Customer(201, "Adam");
cust[1] = new Customer(202, "King");
String[] str = new String[2];
str[0] = "Java";
str[1] = "Python";
Object[][] obj = { emp, cust, str };
for (Object o : obj) {
[Link](o);
}
[Link](obj);
}
}
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
Command Line Arguments:
public class Test {
public static void main(String[] args) {
[Link]("Main");
[Link](args[0] + 200);
float i1 = [Link]("123");
float i2 = [Link](args[1]);
[Link](i1);
[Link](i2);
[Link](i1 + i2);
}
}
Java and Object-Oriented Programming Terminology:
The following terms are common in Java application development in Oracle Database
environment:
• Classes
• Objects
• Interfaces
• Encapsulation
• Inheritance
• Polymorphism
Encapsulation
• Encapsulation describes the ability of an object to hide its data and methods from the rest
of the world and is one of the fundamental principles of object-oriented programming.
• In Java, a class encapsulates the fields, which hold the state of an object, and the methods,
which define the actions of the object.
• Encapsulation enables us to write reusable programs.
• It also enables us to restrict access only to those features of an object that are declared
public.
• All other fields and methods are private and can be used for internal object processing.
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
In the example illustrated in Figure 1-1, the id field is private, and access to it is restricted to the
object that defines it. Other objects can access this field using the getId() method. Using
encapsulation, you can deny access to the id field either by declaring the getId() method as
private or by not defining the getId() method.
Encapsulated Class
public class Employee {
private int empId;
private String empName;
private double empSalary;
public void setEmpId(int empId) {
[Link] = empId;
}
public int getEmpId() {
return empId;
}
public void setEmpName(String empName) {
[Link] = empName;
}
public String getEmpName() {
return empName;
}
public double getEmpSalary() {
[Link]@[Link] Cell: 78 42 66 47 66
Core Java (J2SE) Madhusudhan
return empSalary;
}
public void setEmpSalary(double empSalary) {
[Link] = empSalary;
}
Driver Class:
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee();
[Link](101);
[Link]("JOhn");
[Link](4500.00);
int id = [Link]();
String name = [Link]();
double sal = [Link]();
[Link](id + "\t" + name + "\t" + sal);
}
}
Day 10/12/2021 11:12 PM
Inheritance
Inheritance is an important feature of object-oriented programming languages. It enables classes
to include properties of other classes. The class that inherits the properties is called a child class
or subclass, and the class from which the properties are inherited is called a parent class or
superclass. This feature also helps in reusing already defined code.
In the example illustrated in Figure 1-1, you can create a FullTimeEmployee class that inherits the
properties of the Employee class. The properties inherited depend on the access modifiers
declared for each field and method of the superclass.
[Link]@[Link] Cell: 78 42 66 47 66