0% found this document useful (0 votes)
6 views7 pages

Java Into

The document explains the creation and usage of arrays in Java, highlighting their advantages such as code optimization and random access, as well as their fixed size limitation. It also covers the declaration syntax, accessing and updating elements, and examples of using loops with arrays. Additionally, it introduces a modular approach to programming by demonstrating a simple addition program using a Calculator class and a Demo class, promoting code reusability and organization.

Uploaded by

VSAB
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Java Into

The document explains the creation and usage of arrays in Java, highlighting their advantages such as code optimization and random access, as well as their fixed size limitation. It also covers the declaration syntax, accessing and updating elements, and examples of using loops with arrays. Additionally, it introduces a modular approach to programming by demonstrating a simple addition program using a Calculator class and a Demo class, promoting code reusability and organization.

Uploaded by

VSAB
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9.

2-Creation of Array
Introduction
An array is a collection of elements of the same type, stored in contiguous memory locations.
In Java, an array is an object that can hold multiple values of the same data type. This data
structure is useful when you need to store a fixed set of elements, as it allows efficient data
storage and retrieval.
Advantages of Arrays
● Code Optimization: Arrays help optimize code by allowing efficient data retrieval
and sorting.
● Random Access: You can directly access any element in an array using its index,
making data manipulation straightforward.
Disadvantages of Arrays
● Size Limit: Arrays have a fixed size; once declared, their size cannot change during
runtime. To overcome this limitation, Java provides a collection framework that
allows dynamic resizing of data structures.
Types of Arrays in Java
There are two main types of arrays in Java:
1. Single-Dimensional Array: A linear array with elements stored in a single row.
2. Multidimensional Array: An array of arrays, where elements are stored in a matrix
form, like a table with rows and columns.
Syntax to Declare an Array in Java
You can declare an array in Java using any of the following syntaxes:
dataType[] arr; // Example: int[] arr;
dataType []arr; // Example: int []arr;
dataType arr[]; // Example: int arr[];
Implementation of Arrays
Example 1: Declaring and Initializing an Array
If you know the number of elements you want to store, you can directly initialize the array as
follows:
int num[] = {5, 6, 7, 8, 9};
Alternatively, you can declare the array first and then allocate memory:
int num[] = new int[4]; // This creates an array with 4 elements, initially set to the default
value (0 for int).
Accessing Array Elements
In arrays, elements are stored in indexed positions, starting from 0. For example:

[Link](num[0]); // Output: 5
This code will print the value at index 0, which is 5. You can access other elements similarly.
Example 2: Updating an Array Element
Suppose you want to change the value at index 1 (currently 6) to 0:
num[1] = 0;
[Link](num[1]); // Output: 0
This updates the value at index 1 to 0 and then prints it.
Example 3: Working with Arrays of Unknown Size
If you don't know how many values you need to store initially, you can declare an array and
allocate memory for a fixed number of elements:
int nums[] = new int[4]; // Allocates memory for 4 elements, all initially set to 0.
Later, you can assign values to these elements:
nums[0] = 3;
nums[1] = 4;
nums[2] = 5;
nums[3] = 6;
You can print the values individually:
[Link](nums[0]); // Output: 3
[Link](nums[1]); // Output: 4
[Link](nums[2]); // Output: 5
[Link](nums[3]); // Output: 6
Example 4: Using a Loop to Access Array Elements
Instead of manually accessing each element, you can use a loop to make the task easier,
especially when dealing with larger arrays:

for(int i = 0; i <= 3; i++) {


[Link](nums[i] + " "); // Output: 3 4 5 6
}
This loop prints all the elements in the array in a single line.
03-Class and Object Practical
We will first write a simple program to add two numbers in the Demo class.
Then, we will create a separate Calculator class to handle the addition
functionality. This will make our code modular and reusable.
Simple Addition in Demo Class
public class Demo {
public static void main(String[] args) {
int num1 = 4;
int num2 = 5;

int result = num1 + num2;


[Link](result);
}
}
Creating the Calculator Class
We will create a Calculator class with an add method to handle the addition.
This will allow us to reuse the Calculator class for various addition operations.
public class Calculator {

// Method to add two numbers


public int add(int n1, int n2) {
[Link]("In add method");
int res = n1 + n2;
return res;
}
}
Accessing the Calculator Class Method in Demo Class
To use the add method from the Calculator class in the Demo class, we need to
create an object of the Calculator class. This is done using the new keyword.
Complete Code
Calculator Class
public class Calculator {

// Method to add two numbers


public int add(int n1, int n2) {
[Link]("In add method");
int res = n1 + n2;
return res;
}
}
Demo Class
public class Demo {
public static void main(String[] args) {
int num1 = 4;
int num2 = 5;
// Creating an object of the Calculator class
Calculator calc = new Calculator();

// Calling the add method of Calculator class


int result = [Link](num1, num2);
// Printing the result
[Link]("The sum is: " + result);
}
}
Explanation of Code Using Steps
1. Define the Calculator Class
o Class Definition: public class Calculator { }
o Method to Add Two Numbers:
public int add(int n1, int n2) {
[Link]("In add method");
int res = n1 + n2;
return res;
}
2. Create the Demo Class
o Class Definition: public class Demo { }
o Main Method: public static void main(String[] args) { }
3. Initialize Variables in Main Method
o Define Variables:
int num1 = 4;
int num2 = 5;
4. Create an Object of the Calculator Class
o Object Creation: Calculator calc = new Calculator();
▪ Explanation:
▪ Calculator is the class name.
▪ calc is a reference variable (similar to how num1 and
num2 are variables, but it references an object).
▪ new Calculator() creates a new object of the
Calculator class.
▪ The JVM handles the creation of the object and
allocates memory for it.
5. Call the Add Method Using the Calculator Object
o Method Call: int result = [Link](num1, num2);
▪ Explanation:
▪ [Link](num1, num2) calls the add method of the
Calculator class using the calc object.
▪ The method adds num1 and num2, prints "In add
method", and returns the result.
6. Print the Result
o Output: [Link]("The sum is: " + result);
▪ This line prints the sum of num1 and num2.

Summary
By separating the addition functionality into the Calculator class, we make our
code more modular and reusable. The Demo class then creates an object of the
Calculator class and calls the add method to perform the addition. This approach
follows the principles of object-oriented programming and promotes better code
organization and reuse.

You might also like