Arrays and Strings
Arrays and Strings
August 7, 2019
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 1 / 19
Contents...
Java Array’s
1 Single Dimensional Array
2 Multi Dimensional Array
Java String’s
1 String Functions in Java
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 2 / 19
Introduction to Arrays
It is a collection of similar type of elements that have contiguous memory location.
It is an object the contains elements of similar data type. It is a data structure where we
store similar elements. We can store only fixed set of elements in a java array.
It is index based, first element of the array is stored at 0 index.
Types of Array :
1. Single Dimensional Array
2. Multi Dimensional Array
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 3 / 19
Array Example
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 4 / 19
Declaration, Instantiation and Initialization of an Array
We can declare, instantiate and initialize the java array together by:
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 5 / 19
Passing Array to method in Java
We can pass the java array to method so that we can reuse the same logic on any array.
Let’s see the simple example to get minimum number of an array using method.
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 6 / 19
Multi Dimensional Arrays in Java
Data is stored in row and column based index (also known as matrix form).
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 7 / 19
Multi Dimensional Arrays in Java
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 8 / 19
Multi Dimensional Arrays in Java
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 9 / 19
Arrays in Java
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 10 / 19
Example of 2DArrays in Java
Addition of Two 2D Array.!.
Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 11 / 19
Strings in Java
August 7, 2019
Java String class provides a lot of methods to perform operations on string such as
compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(),
substring() etc.
The java.lang.String class implements Serializable, Comparable and CharSequence
interfaces.
CharSequence Interface
It is used to represent the sequence of characters. String.
StringBuffer and StringBuilder classes implement it.
It means, we can create strings in java by using these three classes.
The Java String is immutable which means it cannot be changed. Whenever we change
any string, a new instance is created. For mutable strings, you can use StringBuffer and
StringBuilder classes.
By String Literal :
Note: String objects are stored in a special memory area known as the ”string constant
pool”.
By new keyword :
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).