7 - Week Java Notes 20cs43p
7 - Week Java Notes 20cs43p
Week – 07
Java Arrays
An array is a collection of similar type of elements which has contiguous memory location.
Java array is an object which contains elements of a similar data type ( homogeneous). Additionally,
the elements of an array are stored in a contiguous memory location. It is a data structure where we
store similar elements (homogeneous). We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is
stored on 1st index and so on.
Features
Single dimensional array − A single dimensional array of Java is a normal array where; the array
contains sequential elements (of same type) –
An array declaration has two components: the type and the name. type declares the element
type of the array. The element type determines the data type of each element that comprises the
array.
int[] myArray; or int myArray[];
// declaring and initializing 1D array
int[] myArray = {10, 20, 30, 40}
1D array also known as a linear array, the elements are stored in a single row. For example:
In this example, we have an array of five elements. They are stored in a single line or adjacent memory
locations.
int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to array OR
int[] intArray = new int[20]; // combining both statements in one
In a situation where the size of the array and variables of the array are already known, array literals can
be used.
Obtaining an array is a two-step process. First, you must declare a variable of the desired array type.
Second, you must allocate the memory to hold the array, using new, and assign it to the array variable.
Thus, in Java, all arrays are dynamically allocated.
int[][][]
intArray; //3D
ARRAY
// declaring and initializing 2D array
int arr[][] = { {2,7,9},{3,6,1},{7,4,2}
};
// so on... arr[2] =
new Student(3,"Arun"); arr[3]
= new Student(4,"Aryan");
arr[4] = new Student(5,"Badhri");
}
}
}
Java Strings
Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence
of characters. The java.lang.String class is used to create a string object.
1. By string literal
2. By new keyword
1) String Literal
String s="welcome";
Each time you create a string literal, the JVM checks the "string constant pool" first. If the string
already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the
pool, a new string instance is created and placed in the pool. For example:
String s1="Welcome";
In the above example, only one object will be created. Firstly, JVM will not find any string object with
the value "Welcome" in string constant pool that is why it will create a new object. After that it will
find the string with the value "Welcome" in the pool, it will not create a new object but will return the
reference to the same instance.
2) By new keyword
In such case, JVM will create a new string object in normal (non-pool) heap memory, and the literal
"Welcome" will be placed in the string constant pool. The variable s will refer to the object in a heap
(non-pool).
1 char charAt(int index) It returns char value for the particular index
8 String replace(char old, char new) It replaces all occurrences of the specified
char value.
class StringDemo
{
public static void main(String args[])
{
System.out.println("-------------STRING DEMO-------");
String s1=new String("Sjpcs");
String s2="sjp ";
//String s3=new String("");
String s3="";
System.out.println("the string s1="+s1);
System.out.println("the string s2="+s2);
System.out.println("Char at ="+s2.charAt(2));
Output:
-------------STRING
DEMO------- the string
s1=Sjpcs the string s2=sjp
Char at =p
the length of the string s1=5
SubString Begin index =jpcs
SubString Begin & end index
=jpc s1 equals s2 is=false
check isempty=true
s1 concatination s2 is=Sjpcssjp
the length of the string s1=5
Replace=Ajpcs
Upper case is=SJPCS
Lower case is=sjpcs
s1 equals ignore case s2=false
2. StringBuffer( int size): It accepts an integer argument that explicitly sets the size of the buffer.
3. StringBuffer(String str): It accepts a string argument that sets the initial contents of the
StringBuffer object and reserves room for 16 more characters without reallocation.
length() The length of a StringBuffer can be found by the length( ) method capacity()
setCharAt(pos,string) Sets the Char at the specified position insert() Inserts text
class StringBufferDemo
{
public static void main(String args[])
{
System.out.println("-------------STRING DEMO-------");
StringBuffer str=new StringBuffer("Object Language");
System.out.println("original string="+str);
System.out.println("Length:"+str.length());
System.out.println("Capacity:"+str.capacity());
int pos=str.indexOf(" Language");
str.insert(pos," Oriented");
System.out.println("modified string:"+str);
str.setCharAt(6,'-');
System.out.println("String now:"+str);
}
}
Output:
-------------STRING DEMO-------
original string=Object Language
Length:15
Capacity:31
modified string:Object Oriented Language
String now:Object-Oriented Language
appended string:Object-Oriented Language improve security
Length:41