0% found this document useful (0 votes)
14 views2 pages

Understanding Arrays and Methods in Java

The document explains arrays in programming, highlighting their key features such as fixed length, contiguous memory allocation, and zero-based indexing. It provides syntax for declaring arrays of different data types and examples of initializing and using arrays in Java. Additionally, it covers user-defined methods, detailing their structure, parameters, and how they can be invoked to perform specific tasks.
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)
14 views2 pages

Understanding Arrays and Methods in Java

The document explains arrays in programming, highlighting their key features such as fixed length, contiguous memory allocation, and zero-based indexing. It provides syntax for declaring arrays of different data types and examples of initializing and using arrays in Java. Additionally, it covers user-defined methods, detailing their structure, parameters, and how they can be invoked to perform specific tasks.
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

Arrays Arrays

 Arrays are variables that can hold more than one value, they can hold a list of values of  Declaring an array to store 7 character elements.
the same type.
 Key Features of Arrays: char AL[]=new char[7]; // declaration
Fixed Length: after creating an array, its size is fixed, we can not change it. AL
Contiguous Memory Allocation: In array elements are stored in contiguous locations. AL[0] AL[1] AL[2] AL[3] AL[4] AL[5] AL[6]
Zero Based Indexing: The first element of the array start from index 0.
Total memory allocated to array AL=7*2=14 bytes.
Array index goes from 0 to n-1 for an array of size n
 Declaring an array to store 7 integer elements.
 Syntax to declare an array- int m[]=new int[7]; // declaration
dataType arrayName[ ]= new dataType[size]; m
OR
dataType[ ] arrayName= new dataType[size]; m[0] m[1] m[2] m[3] m[4] m[5] m[6]
Total memory allocated to array m=7*4=28 bytes.
dataType: The type of elements the array will hold(e.g., int, char, double, float)
arrayName: The name of the array variable. Declaring and initializing an array-
size: The number of elements the array can store.
new : it is a keyword and dynamically(run-time) allocates memory to array. double size[]={20.45,17.50,40.25, 10.75, 35.06}; //static initialization

size 20.45 17.50 40.25 10.75 35.06


size[0] size[1] size[2] size[3] size[4]

Arrays Arrays
public class Array1 //program to enter 5 students marks and display on screen
{ import [Link];
public static void main(String[] args) public class Array2
{ {
int []num={20,30,45,15,60}; public static void main(String[] args)
[Link](num[0]); {
[Link](num[1]); //try below code in place of 5 lines int i;
[Link](num[2]); for(int i=0; i<5,i++) int marks[]=new int[5];
[Link](num[3]); [Link](num[i]); Scanner ob=new Scanner([Link]);
[Link](num[4]);
[Link]("Enter marks for 5 students");
}
for(i=0;i<5;i++)
}
marks[i]=[Link]();
Output: [Link]("The marks are: ");
20 for(i=0;i<[Link];i++)
30 [Link](marks[i]+" ");
45 }}
15 Note: An array variable has a useful property, the length property, which is the size of the
array. For example, for the marks array, [Link] will be 5.
60
Note: [Link](num[5]); Error- ArrayIndex out of bounds
User-Defined Method User-Defined Method
A method in Java is a block of statements grouped together to perform a specific task. *The method body is enclosed within a pair of curly braces and contains the statements of the
A method has- method.
 a name, *The statements end with an optional return statement that returns a value back to the calling
 a return type, method.
 an optional list of parameters, and *The return type is the type of the value that the method returns.
 a body. Example- User defined method to calculate area of rectangle
The structure of Java Method:
return_type method_name(list of parameters separated by commas) returntype methodname Parameters list
{
statements static double rectangle_area(double length, double breadth)
::::::::::::::::: {
return statement return (length * breadth);
} }
* return_type can be any dataType (void, char, int , float,double).
*method_name- this is a single word followed by round bracket. *The static(keyword) modifier allows us to call this method without an invoking object.
*The parameter list placed within the round brackets allows data to be passed into the method. *A method is called/invoked from another method.
*This list is a comma separated list of variables (as many as needed) along with their data types. *When a method is called, control is transferred from the calling method to the called method.
*The variables of the parameter list act just like regular local variables inside the method body. *The statements inside the called method's body are executed.
*Control is then returned back to the calling method.

User-Defined Method
//program to calculate area of rectangle using User Defined Method
public class UDM
{
static double RA(double L, double B)
{
return L*B;
}
public static void main(String[ ] args)
{
double A; //main() is calling method
A=RA(20.5,10.05); // RA() is called method
[Link]("area of rectangle is:"+A);
}
}
Output:
area of rectangle is:206.025

You might also like