[Go to site: main page, start]

0% found this document useful (0 votes)
7 views56 pages

Java String and Array Fundamentals

The document covers Java programming concepts related to Strings and Arrays, including memory allocation, garbage collection, and the String pool. It explains the differences between String, StringBuilder, and StringBuffer, as well as how to declare, construct, and initialize arrays in Java. Additionally, it discusses the usage of the Arrays class and ArrayList, highlighting their advantages over traditional arrays.

Uploaded by

moneytalks036
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)
7 views56 pages

Java String and Array Fundamentals

The document covers Java programming concepts related to Strings and Arrays, including memory allocation, garbage collection, and the String pool. It explains the differences between String, StringBuilder, and StringBuffer, as well as how to declare, construct, and initialize arrays in Java. Additionally, it discusses the usage of the Arrays class and ArrayList, highlighting their advantages over traditional arrays.

Uploaded by

moneytalks036
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

Java Programming – String & Array

Jalpesh Vasa ([Link]@[Link])


Department of Information Technology
Agenda

• Memory Allocation & Garbage Collection

• Strings in Java

• Arrays in Java
Memory Allocation & Garbage
Collection
Memory Allocation & Garbage
Collection
Memory Allocation & Garbage
Collection
String in JAVA
Stringpool Concept in JAVA
Stringpool Concept in JAVA

• Strings are immutable objects in java

• All Strings are stored in String pool allocated within Java Heap Space

• Using new operator, we force String class to create a new String object in heap
space
The String Class
• String x = "Java";
• [Link](" Rules!");
• [Link]("x = " + x);
• x = Java
• [Link]();
• [Link]("x = " + x);
• x = Java
• [Link]('a', 'X');
• [Link]("x = " + x);
• x = Java
[Link] API
[Link] API Example
Converting String to Numbers and vice-versa
The StringBuilder Class
• It should be used when you have to make a lot of modifications to
strings of characters.
The StringBuilder Class
• String x = "abc";
• [Link]("def");
• [Link]("x = " + x);
• // output is "x = abc“

• StringBuilder sb = new StringBuilder("abc");


• [Link]("def");
• [Link]("sb = " + sb);
• // output is "sb = abcdef"
The StringBuilder methods
• StringBuilder sb = new StringBuilder("abc");
• [Link]("def").reverse().insert(3, "---");
• [Link]( sb );
• // output is "fed---cba"
String v/s StringBuffer v/s StringBuilder

Feature String StringBuilder StringBuffer

Mutability Immutable Mutable Mutable


Thread safety Not thread-safe Not thread-safe Thread-safe
Slower than
Performance Slower Faster StringBuilder, but
faster than String
Creates a new Modifies the Modifies the
Memory Usage object when existing object in existing object in
modified place place
Strings that will
Strings that will Strings that will
be modified
not be modified, be modified
frequently, or
Use cases or strings that will frequently and
strings that will
be modified by a need to be
be modified by
single thread thread-safe
multiple threads
Using Arrays
• How to make an array reference variable (declare)
• How to make an array object (construct)
• How to populate the array with elements (initialize)
Creating an Array in JAVA
Declare
Assigning type of the array

Construct
Assigning size of the array

Initialize
Assigning values to the elements of the array
Array in JAVA
Arrays in JAVA
• Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.

• To declare an array, define the variable type with square brackets:


• It can be of primitive type
E.g. array of integers, floats, boolean etc.
• It can be of Class type
E.g. array of String, Demo, Buttons etc.

String[] name;
int age[];
int[] number = {10,20,30,40}
String cars[] = new String[4]
cars[0] = “BMW”
cars[1] = “Audi”
Different Ways to create an Array

int array1 []; array1 = new int[4];

int[] array2 = new int[4];

String names[] = new String[10];

int[] carList = new int[]; // Will not compile; needs a size


Different ways to initialize the array
int array[] = new int[2]; array[0] = 1; array[1] = 2;

int [][] vector = {{5, 2, 4, 7}, {8, 2}, {1}};

int[] values = new int[] {6, 3, 1};

int array2[] = {2 ,3 ,4 ,5}; int bArray[] = array;

int [] a, b; // or int a[], b[];


Array with multiple references

• int a[] = new int[3];


• a[0]=1, a[1]=1, a[2]=1;
• int b[] = a;
• b[0] = 2;
Heap
b
Memory
a
Types of Array in JAVA
Constructing an Array
• int[] testScores;
• testScores = new int[4];

• int[] testScores = new int[4];

• Thread[] threads = new Thread[5];

• int[] carList = new int[];


• // Will not compile; needs a size
Constructing Multidimensional Arrays
• int[][] myArray = new int[3][];
Initializing an Array
• int[ ][ ] myArray = new int[3][ ];
• myArray[0] = new int[2];
• myArray[0][0] = 6;
• myArray[0][1] = 7;
• myArray[1] = new int[3];
• myArray[1][0] = 9;
• myArray[1][1] = 8;
• myArray[1][2] = 5;
Example
• int[] x = new int[5];
• x[4] = 2; // OK, the last element is at index 4
• x[5] = 3; // Runtime exception. There is no element at index 5!

• int[] z = new int[2];


• int y = -3;
• z[y] = 4; // Runtime exception. y is a negative number
A two-dimensional array
• int[][] scores = new int[3][];
• scores[0] = new int[4];
• scores[1] = new int[6];
• scores[2] = new int[1];

• int[][] scores = {{5,2,4,7}, {9,2}, {3,4}};


Constructing and Initializing an Anonymous
Array
• int[] testScores;
• testScores = new int[] {4,7,2};

• new Object[3] {null, new Object(), new Object()};


• // not legal; size must not be specified
Create Array in JAVA
• Declare
• Assigning type of the array
• Construct
• Assigning size of the array
• Initialize
• Assigning values to the elements of the array
Array Processing in JAVA
Example: Matrix Addition
Example: Matrix Multiplication
Usage of Array
Usage of Array
Iterate through Array
Excercise
Excercise
Excercise
Excercise
Arrays class in JAVA
Class Hierarchy:
[Link]
↳ [Link]
Class Declaration:
public class Arrays
extends Object
Syntax to use Array:
Arrays.<function name>;
Example
int intArr[] = {10, 20, 15, 22, 35};
[Link](intArr); // [10, 15, 20, 22, 35]
int intKey = 10;
[Link](intArr, intKey); // 10 found at index = 0
[Link](intArr, 1, 3, intKey); // 10 found at index = -2
int intArr1[] = {10, 15, 22};
[Link](intArr, intArr1); // Integer Arrays on comparison: true
int intArr2[] = [Link](intArr, 10); // [10, 15, 20, 22, 35, 0, 0, 0, 0,
0]
int intArr3[] = [Link](intArr, 1, 3); // [15, 20]
ArrayList class in JAVA
Class Hierarchy:
[Link]
↳ [Link]
↳ [Link]
↳ [Link]
Class Declaration:
public class ArrayList
implements List extends Collection
Syntax to use ArrayList:
ArrayList list=new ArrayList();
Example
Iterating ArrayList
List<Integer> numbers = new ArrayList<>([Link](1, 2, 3, 4, 5, 6, 7, 8));

// Iterating using for loop // Using iterator


// For each loop
for (int i = 0; i < [Link](); i++) Iterator iterator = [Link]();
for(Integer var : numbers)
[Link]([Link](i) + " while([Link]())
[Link](var);
"); [Link]([Link]());

// Iterating using while loop


int val = 0; int i = 0; // Using lambda function
while([Link]()>val){ [Link](number->[Link](number));
[Link]([Link](i) + "
");
val++;
// Using enumeration
}
Enumeration<Integer> e = [Link](numbers);
while([Link]())
[Link]([Link]());
Advantages of ArrayList over array
• It can grow dynamically.
• It provides more powerful insertion and search mechanisms than arrays.

You might also like