0% found this document useful (0 votes)
24 views

Arrays and Strings

The document discusses arrays and strings in Java. It covers topics like single and multi-dimensional arrays, declaring and initializing arrays, passing arrays to methods. It also discusses strings in Java including how they are created using literals or new keyword, string class methods for operations like compare, concat, length etc. The document is presented by Prof. Pradeep. K. V and Prof. Nachiyappan .S on August 7, 2019 for a Java workshop.

Uploaded by

Rohit Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Arrays and Strings

The document discusses arrays and strings in Java. It covers topics like single and multi-dimensional arrays, declaring and initializing arrays, passing arrays to methods. It also discusses strings in Java including how they are created using literals or new keyword, string class methods for operations like compare, concat, length etc. The document is presented by Prof. Pradeep. K. V and Prof. Nachiyappan .S on August 7, 2019 for a Java workshop.

Uploaded by

Rohit Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Arrays and Strings in Java.

Prof. Pradeep. K. V and Prof. Nachiyappan .S

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

Example of Multi Dimensional Array.

Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 8 / 19
Multi Dimensional Arrays in Java

What is the Class Name of an Array..?.


In java, array is an object. For array object, an proxy class is created whose name can be
obtained by getClass().getName() method on the object

Prof. Pradeep. K. V and Prof. Nachiyappan .S Qubit - Java Workshop August 7, 2019 9 / 19
Arrays in Java

Copyingg a Java Array..!.


We can copy an array to another by the arraycopy method of System class.

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

Prof. Pradeep. K. V and Prof. Nachiyappan .S

August 7, 2019

Prof. Pradeep. K. V and Prof. Nachiyappan .S Strings in Java August 7, 2019 12 / 19


Java Strings
String is an object that represents sequence of char values. An array of characters works
same as Java string.
For example:

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.

Prof. Pradeep. K. V and Prof. Nachiyappan .S Strings in Java August 7, 2019 13 / 19


java Strings

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.

Prof. Pradeep. K. V and Prof. Nachiyappan .S Strings in Java August 7, 2019 14 / 19


java Strings
What is String in Java..?
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.
How to create a string object?
There are two ways to create String object:
By string literal
By new keyword

By String Literal :

Note: String objects are stored in a special memory area known as the ”string constant
pool”.

Prof. Pradeep. K. V and Prof. Nachiyappan .S Strings in Java August 7, 2019 15 / 19


java Strings

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).

Prof. Pradeep. K. V and Prof. Nachiyappan .S Strings in Java August 7, 2019 16 / 19


Java Strings
Java String class methods

Prof. Pradeep. K. V and Prof. Nachiyappan .S Strings in Java August 7, 2019 17 / 19


Java Strings

Java String class methods

Prof. Pradeep. K. V and Prof. Nachiyappan .S Strings in Java August 7, 2019 18 / 19


Thanks

Prof. Pradeep. K. V and Prof. Nachiyappan .S Strings in Java August 7, 2019 19 / 19

You might also like