Arrays&Strings
Arrays&Strings
2. Creation of Arrays
After declaring an array, we need to create it in the memory. Java allows us to
create array using new operator.
dataType[size] array=new dataType; Example :
dataType [size]array=new dataType; int[5] Array=new int;
int [5]Array=new int;
dataType array[size]=new dataType; int Array[5]=new int;
3. Initialization of Arrays
The final step is to put values into the array created. This process is known as
initialization.
Example :
Array[0]=10;
Array[1]=20;
Array[2]=30;
Array[3]=40;
Array[4]=50;
We can declare, instantiate and initialize the java array together by:
class OneDimentional
{
public static void main(String args[])
{
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]); >javac OneDimentional.java
} >java OneDimentional
} 10
} 20
30
40
50
Program for Sorting a List of Numbers
System.out.println("The sorted array is ");
import java.util.Scanner; for(int i=0;i<n;i++)
class SortingNumber {
{ System.out.println(A[i]);
public static void main(String args[]) }
{ System.out.println();
int A[]=new int[50]; }
Scanner scan=new Scanner(System.in); }
System.out.println("Enter Array Size");
int n=scan.nextInt();
System.out.println("Enter Array Elements");
for(int i=0;i<n;i++)
{ Output
A[i]=scan.nextInt();
>javac SortingNumber.java
}
for(int i=0;i<n;i++) >java SortingNumber
{ Enter Array Size
for(int j=i+1;j<n;j++) 5
{ Enter Array Elements 13 24 11 65 18
if(A[i]>A[j]) The sorted array is
{ 11
int temp=A[i]; 13
A[i]=A[j];
18
A[j]=temp;
} 24
} 65
}
Two Dimensional Array
Reading Array elements: System.out.println("Enter Array
Elements"); for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
A[i][j]=scan.nextInt();
}
}
Note: String objects are stored in a special memory area known as string constant pool.
2) By new keyword
String s=new String("Welcome");
Example:
public class StringExample
{
public static void main(String args[]) Output
{ java
String s1="java”; strings
char ch[]={'s','t','r','i','n','g','s'}; example
//converting char array to string
String s2=new String(ch);
//creating java string by new keyword
String s3=new String("example");
}
}
Program to implement string operations on Strings
class StringDemo
{
public static void main(String args[])
{
String s1="Bachelor";
String s2="Of";
String s3="Computer";
String s4="Application"; Output
} >javac StringDemo.java
System.out.println(s1.toUpperCase()); >java StringDemo
System.out.println(s1.toLowerCase()); BACHELOR
System.out.println(s3.concat(s4)); bachelor
System.out.println(s4.length()); ComputerApplication
System.out.println(s1.charAt(2)); 11
System.out.println(s1.indexOf('c')); c
System.out.println(s2.replace('f','o')); 2
System.out.println(s1.substring(5)); Oo
System.out.println(s4.substring(0,3)); lor
System.out.println(s1.compareTo(s4)); App
System.out.println(s1.equals(s2)); 1
System.out.println(s4.startsWith("App"));
false
System.out.println(s1.endsWith("or"));
true
}
true
}
Methods
• A method is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a method.
• Methods are used to perform certain actions, and they are also known
as functions.
Create a Method
A method must be declared within a class. It is defined with the name of
the method, followed by parentheses ().
Method Declaration
The method declaration provides information about method attributes, such as
visibility, return-type, name, and arguments.
It has six components that are known as method header, as we have shown in
the following figure.
iv) Method Name: It is a unique name that is used to define the name of a
method.
vi) Method Body: It is a part of the method declaration. It contains all the
actions to be performed. It is enclosed within the pair of curly braces.
Types of Method
There are two types of methods in Java:
1. Predefined Method
2. User-defined Method
1. Predefined Method
In Java, 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.
2. User-defined Method
The method written by the user or programmer is known as a user-
defined method. These methods are modified according to the requirement.
public class Main
{
static void myMethod()
{ Output
System.out.println("I just got executed!");
I just got executed!
}
I just got executed!
public static void main(String[] args) I just got executed!
{
myMethod();
myMethod();
myMethod(); public class Main
} {
} static void myMethod(String fname, int age)
{
System.out.println(fname + " is " + age);
}
Output
public static void main(String[] args)
Suman is 5 {
Jony is 8 myMethod(“Suman", 5);
myMethod("Jony", 8);
Anil is 31 myMethod("Anil", 31);
}
}
Java program to illustrate Method Overloading.
import java.lang.Math;
class Box class Lab6
{ {
int area(int l,int b) public static void main(String args[])
{ {
return l*b; Box b=new Box();
} System.out.println("Area of rectangle\
double area(double r) t"+b.area(8,9));
{ System.out.println("Area of circle\t"+b.area(5.0));
return Math.PI*r*r; System.out.println("Area of square\t"+b.area(9));
} System.out.println("String Concatination\
int area(int a) t"+b.area("Java ","Is ","Super "));
{ }
return a*a; }
}
String area(String s1,String s2,String s3)
{
return s1+s2+s3;
} Output
} Area of rectangle 72
Area of circle 78.53981633974483
Area of square 81
String Concatenation Java Is Super