Chapter 4
Chapter 4
Ashok
Chapter – 4
• Arrays
• String
• StringBuffer
• StringBuffer
• Command Line Args
47
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
Arrays
-> Array is an object which contains elements of similar data type
-> Array is a container object that hold values of homogeneous type.
-> Array is also known as static data structure because size of an array must be specified at
the time of its declaration.
-> Array starts from zero index and goes to n-1 where n is length of the 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.
-> In Java, array is treated as an object and stores into heap memory. It allows to store
primitive values or reference values.
48
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
-> In the above example, we created an array arr of int type and can store 5 elements. We
iterate the array to access its elements and it prints five times zero to the console. It prints
zero because we did not set values to array, so all the elements of the array initialized to 0
by default.
-> Here, we are assigning values at the time of array creation. It is useful when we want to
store static data into the array.
-> We can set value based on index position also
49
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
50
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
Multi-Dimensional Array
A multi-dimensional array is very much similar to a single dimensional array. It can have
multiple rows and multiple columns unlike single dimensional array, which can have only
one row index.
It represents data into tabular form in which data is stored into row and columns.
51
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
Strings
-> String is an object that represents sequence of characters.
Ex: "hello”, "ashokit"
-> In Java, String is represented by String class which is available java.lang package
-> One important thing to notice about string object is that string objects are immutable
that means once a string object is created it cannot be changed.
How to create String object in Java?
-> To handle string data in Java, we need an object of string class. Basically, there are three
ways to create a String object in Java.
1) By string literal.
2) By new keyword.
3) By converting character arrays into strings
Working with String literal
-> String literal in Java is created by using double quotes.
String str = "hello";
-> The string literal is always created in the string constant pool.
-> In Java, String constant pool is a special area that is used for storing string objects.
-> Whenever we create a string literal in Java, JVM checks string constant pool first. If the
string already exists in string constant pool, no new string object will be created in the string
pool by JVM.
-> JVM uses the same string object by pointing a reference to it to save memory. But if string
does not exist in the string pool, JVM creates a new string object and placed it in the pool.
52
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
For example:
String s1 = "Hello";
String s2 = "Hello";
53
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
-> Now JVM will create the second object as a copy for literal “Hello” in string constant pool
for future purposes. There is no explicit reference variable pointing to the copy object in the
pool but internally, JVM maintains the reference variable implicitly.
-> Remember that the object created in the SCP area is not eligible for garbage collection
because implicitly, the reference variable will be maintained by JVM itself.
By converting Character Arrays into String
-> The third way to create strings is by converting the character arrays into string. Let’s take
a character type array: arr[ ] with some characters as given below:
char arr[ ] = {'j','a','v','a'};
-> Now create a string object by passing array name to string constructor like this:
String s = new String(arr);
-> Now string object ‘s’ contains string “java”. It means that all the characters of the array
are copied into string.
-> If you do not want all the characters of the array into string then you can also mention
which character you need, like this:
String s = new String(arr, 1,3);
-> From the above statement, total of three characters will be copied into string s. Since the
original characters are j-a-v-a. So, the counting will start from 0 i.e 0th character in the array
is ‘j’ and the first character is ‘a’. Starting from ‘a’, total of three characters ‘aja’ will copy
into string s.
How many total objects will be created in memory for following string objects?
String s1 = new String("ashokit");
String s2 = new String("ashokit");
String s3 = "ashokit";
String s4 = "ashokit";
54
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
1. During the execution of first statement using new operator, JVM will create two objects,
one with content in heap area and another as a copy for literal “ashokit” in the SCP area for
future purposes as shown in the figure.
The reference variable s1 is pointing to the first object in the heap area.
2. When the second statement will be executed, for every new operation, JVM will create
again one new object with content “ashokit” in the heap area.
But in the SCP area, no new object for literal will be created by JVM because it is already
available in the SCP area. The s2 is pointing to the object in the heap area as shown in the
figure.
3. During the execution of third and fourth statements, JVM will not create a new object
with content “ashokit” in the SCP area because it is already available in string constant pool.
It simply points the reference variables s3 and s4 to the same object in the SCP. They are
shown in the above figure.
Thus, three objects are created, two in the heap area and one in string constant pool.
String Manipulations
String class provided several methods to perform operations on Strings
#1) Length
The length is the number of characters that a given string contains. String class has a
length() method that gives the number of characters in a String.
55
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
#2) concatenation
Although Java uses a ‘+’ operator for concatenating two or more strings. A concat() is an
inbuilt method for String concatenation in Java.
56
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
57
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
58
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
59
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
StringBuffer Class
-> StringBuffer class is used to create a mutable string object. It means, it can be changed
after it is created.
-> It is similar to String class in Java both are used to create string, but stringbuffer object
can be changed.
-> StringBuffer class is used when we have to make lot of modifications to our string.
-> It is also thread safe i.e multiple threads cannot access it simultaneously.
StringBuffer defines 4 constructors.
StringBuffer(): It creates an empty string buffer and reserves space for 16 characters.
StringBuffer(int size): It creates an empty string and takes an integer argument to set
capacity of the buffer.
StringBuffer(String str): It creates a stringbuffer object from the specified string.
StringBuffer(charSequence []ch): It creates a stringbuffer object from the charsequence
array.
60
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
Note: In the above program Output is such because String objects are immutable objects.
Hence, if we concatenate on the same String object, it won't be altered But StringBuffer
creates mutable objects. Hence, it can be altered.
61
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
#2) insert() :
This method inserts one string into another. Here are few forms of insert() method
#3) reverse()
This method reverses the characters within a StringBuffer object.
62
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
#4) replace()
This method replaces the string from specified start index to the end index.
#5) capacity()
This method returns the current capacity of StringBuffer object.
63
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
StringBuilder (String str): create a StringBuilder object and initialize it with string str.
StringBuilder (CharSequence seq): It creates stringbuilder object by using CharSequence
object.
-> When we want a mutable String without thread-safety then StringBuilder should be used
-> When we want a mutable String with thread-safety then StringBuffer should be used
-> When we want an Immutable object then String should be used.
64
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com
Core Java Mr. Ashok
-> When we pass command-line arguments, they are treated as strings and passed to the
main method in the string array argument.
-> The arguments have to be passed as space-separated values.
-> We can pass strings and primitive data types as command-line arguments.
-> The arguments will be converted to strings and passed into the main method string array
argument.
Java Program with Command Line Arguments
65
Ashok IT, Phone: +91 9985396677 , Email: [email protected], www.ashokitech.com