Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

CO_IF_22412_CO2

Manisha Pokharkar, Lecturer, Vidyalankar Polytechnic

Date: 10 February 2021

<<MSBTE LEAD>>
Unit 02 :
Derived Syntactical
Constructs in Java

Written by

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
Unit Outcome 5 : Develop
Programs using vectors and
wrapper classes for the given
problem.

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
Learning Outcome 5-a : Student
should understand the string and
StringBuffer Class

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
What we will learn today

1. About string in Java Key takeaways


2 Methods of string in Java Concept of String class in Java

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar
Polytechnic, Mumbai

Page 5 Maharashtra State Board of Technical Education


Learning Objective/ Key learning

► Understand concept of String classes in Java

Page 6 Maharashtra State Board of Technical Education


Concept Explanation:

► The String class is commonly used for holding and manipulating strings of text in Java programs.
► In java strings are class objects and implemented using two classes namely, String and StringBuffer.

Page 7 Maharashtra State Board of Technical Education


String Class

► Strings which are widely used in Java programming, are a sequence of


characters. In Java Programming language, strings are treated as objects.
► The Java platform provides the string class to create and manipulate
strings.

Page 8 Maharashtra State Board of Technical Education


String-Object or Primitive?
- String could be considered a primitive type in Java; but in fact they are not. As a String is actually made
up of an array of char primitives.

String objects are immutable!


- That means once a String object is created it cannot be altered. For mutable String, you can use
StringBuffer and StringBuilder classes.

Page 9 Maharashtra State Board of Technical Education


How to create the String?

► There are two ways two create String Object:


1)By String literals:
Example:
String s=“welcome”;

2)By new keyword:


Example:
String s=new String(“Welcome”);

Page 10 Maharashtra State Board of Technical Education


Example

Public class stringexample


{
public static void main(string args[])
String s1=“java”; Output:
char ch={‘s’,’t’,’r’,’i’,’h’,’g’,’s’};
String s2=new String(ch); Java
String s3=new String(“example”); Strings
System.out.println(s1); Example
System.out.println(s2);
System.out.println(s3);
}
}

Page 11 Maharashtra State Board of Technical Education


Some important operations of Strings

► String Concatenation
► String Comparison
► Substring
► Length of string and etc.

Page 12 Maharashtra State Board of Technical Education


String Concatenation

There are two methods to concatenate two or more strings.

Concatenate two
or more strings

Using concate()
Using + Operator
method

Page 13 Maharashtra State Board of Technical Education


Example

Public class stringexample


{ public static void main(String args[])
{
String s = “Hello”;
String str = “World”;
String str1 = s.concat(str);
String str2 = “Hello”.concat(“World”); Using concat() method
String str3 = s + str;
Output:
String str4 = “Hello” + ”World”;
Using + method Hello World
System.out.println(str1); Hello World
System.out.println(str2); Hello World
System.out.println(str3); Hello World
System.out.println(str4);
}}
Page 14 Maharashtra State Board of Technical Education
String Comparison

► String comparison can be done in 3 ways

Using equals() Using


Using == Operators
method compareTo()
method

String Comparison

Page 15 Maharashtra State Board of Technical Education


Example

class stringexample
{ String s = “Hell”;
String s1 = “Hello”; Output
String s2 = “Hello”;
String s3 = “Java”; true
System.out.println(s1.equals(s2)); //true false
System.out.println(s.equals(s1)); //false true
String s4 = new String(“Java”) -1
System.out.println(s1==s2); //true 0
1
System.out.println(s3==s4); //false
System.out.println(s.compareTo(s2)); //returns-1 because s<s2
System.out.println(s1.compareTo(s2)); //returns 0 because s1=s2
System.out.println(s2.compareTo(s)); //returns 1 because s2>s
}
}
Page 16 Maharashtra State Board of Technical Education
Substring in Java

► A part of String is called substring. In other words, substring is a subset of another string.
► We can get substring from the given string object by one of the two methods:

1. public String substring(int startindex);


2. public String substring(int startindex, int endIndex);
Example:
public class StringExample
{
Output:
public static void main(String args[])
{
World
String s = “Hello World”;
Hello
System.out.println(s.substring(6));
System.out.println(s.substring(0,5));
}
}
Page 17 Maharashtra State Board of Technical Education
Length of String

► The java string length() method find the length of the string. It returns the count of total number of
characters.

Example:
public class lengthexample{
public static void main(String args[])
{
String s1 = “Hello World”;
System.out.println(“String length is:” + s1.length());
}
Output:
}

String length is: 12

Page 18 Maharashtra State Board of Technical Education


Some more String methods

► The java.lang.String class provides a lot of methods to work on string. By the help pf these methods, we
can perform operations on Strings.
► We will learn the following methods:

1)charAt()
2)contains()
3)getChars()
4)indexOf()
5)replace()
6)toLowerCase()
7)toUpperCase

Page 19 Maharashtra State Board of Technical Education


charAt() and contains()

► charAt(): Returns a char value at the given index number. The index number starts from 0.
► Contains(): searches the sequence of characters in this string. It returns true if sequence of char values
are found in this string otherwise returns false.
Example:
class method
{
public static void main(String args[])
String name=“Hello World!”; 0
char ch=name.charAt(4); true
System.out.println(ch); false
System.out.println(name.contains(“Hello”));
System.out.println(name.contains(“hello”));
}
}

Page 20 Maharashtra State Board of Technical Education


indexOf()

► Return index of given character value or substring.


► If it is not found it returns -1. the index count starts from 0.

Example:
Class methods
{
Public static void main(String args[]) Output:
{ String s1=“Hello World”;
int index1 = s1.indexOf(‘o’);
System.out.println(index1); 4
int index2 = s1.indexOf(‘l’,4); 8
System.out.println(index2);
}
}

Page 21 Maharashtra State Board of Technical Education


replace()

► Returns a string replacing all the old char to new char.


Example:
class Methods
{
public static void main(String args[])
{
Output:
String s1 = “Hello World”;
String s2 = s1.replace(‘o’,’e’);
Helle Werld
System.out.println(s2);
}
}

Page 22 Maharashtra State Board of Technical Education


toLowerCase() and toUpperCase()

► toLowerCase(): it converts all the characters of the string into lowercase letter.
► toUpperCase(): it converts all the characters of the string into uppercase letter.

Example:
class methods
{ public static void main(String args[])
{ Output:
String s1 = “Hello World”;
String lowercase = s1.toLowerCase(); hello world
HELLO WORLD
System.out.println(lowercase);
String uppercase = s1.toUpperCase();
System.out.println(uppercase);
}
}

Page 23 Maharashtra State Board of Technical Education


Quick Revision

1) What is the value returned by function compareTo() if the invoking string is less than the string
compared?
a) Zero
b) value less than zero
c) value greater than zero
d) none of the mentioned

2) Which method is used to find the character at the given position?


a) char()
b) charAt()
c)charat()
d)Char()

Page 24 Maharashtra State Board of Technical Education


CO_IF_22412_CO2

Manisha Pokharkar, Lecturer, Vidyalankar Polytechnic

Date: 10 February 2021

<<MSBTE LEAD>>
Unit 02 :
Derived Syntactical
Constructs in Java

Written by

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
Unit Outcome 5 : Develop
Programs using vectors and
wrapper classes for the given
problem.

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
Learning Outcome 5-a : Student
should understand the string and
StringBuffer Class

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
What we will learn today

1. StringBuffer class in Java Key takeaways


2 Methods of StringBuffer class Concept of StringBuffer class in Java

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar
Polytechnic, Mumbai

Page 5 Maharashtra State Board of Technical Education


Learning Objective/ Key learning

► Understand concept of StringBuffer classes in Java

Page 6 Maharashtra State Board of Technical Education


Concept Explanation:

► The String class is commonly used for holding and manipulating strings of text in Java programs.
► In java strings are class objects and implemented using two classes namely, String and StringBuffer.

Page 7 Maharashtra State Board of Technical Education


StringBuffer Class

► The StringBuffer class is used to represent characters that can be modified.


► This is simply used for concatenation or manipulation of the strings.
► We can insert characters and substring in the middle of a string or append another string to the end.
► Methods of StringBuffer class:
1) append()
2) insert()
3) delete()
4) deleteCharAt()
5) reverse()
6) setChartAt()

Page 8 Maharashtra State Board of Technical Education


append()

► Appends the string s2 to s1 at the end.

Example:
Class a
{
public static void main(String args[])
Output:
{
StringBuffer s1 = new StringBuffer(“Hello");
StringBuffer s2 = new StringBuffer(“ World");
Hello World

s1.append(s2);
System.out.println(s1);
}
}

Page 9 Maharashtra State Board of Technical Education


Insert()

► It insert the string at the given position.

Example:
Class a
{
public static void main(String args[])
{ Output:
StringBuffer s1 = new StringBuffer(“Hello");
StringBuffer s2 = new StringBuffer(“ World"); Hel World lo

S1.insert(3,s2);
System.out.println(s1);

}
}

Page 10 Maharashtra State Board of Technical Education


delete()

► This is the delete() function is used to delete multiple character at once from n position to m position
(n and m are will be fixed by you.) in the buffered string.

Example:
Class a
{
public static void main(String args[]) Output:
{
StringBuffer s1 = new StringBuffer(“Hello"); Hlo
s1.delete(1,3);
System.out.println(s1);

}
}

Page 11 Maharashtra State Board of Technical Education


deletCharAt()

► This is the deleteCharAt() function which is used to delete the specific character from the buffered
string by mentioning that's position in the string.
Example:
class a
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer(“Hello"); Output:
s1.deleteCharAt(2);
System.out.println(s1); Helo
}
}

Page 12 Maharashtra State Board of Technical Education


reverse()

► This is the reverse() function used to reverse the string present in string buffer.

Example:
Class a
{
public static void main(String args[])
{ Output:
StringBuffer s1 = new StringBuffer(“Hello");
s1.reverse();

System.out.println(s1);
olleH
}
}

Page 13 Maharashtra State Board of Technical Education


setCharAt()

► It modifies the character at the specified position by the given character.

Example:
Class a
{
public static void main(String args[]) Output:
{
StringBuffer s1 = new StringBuffer(“Hello"); Heleo
s1.setCharAt(3,’e’);
System.out.println(s1);

}
}

Page 14 Maharashtra State Board of Technical Education


Difference between string class and string buffer class

String String buffer


String: It is the most commonly used class StringBuffer: It is a peer class of string
which can be used to store string which can be modified in terms of length
constants. String constants can be any & contents.
characters from keyboard enclosed in
double quotes. Strings can be used when
data need not be altered.
String is a major class StringBuffer is a peer class of String
Length is fixed Length is flexible
Contents of object cannot be modified Contents of can be modified
Object can be created by assigning String Objects can be created by calling
constants enclosed in double quotes. constructor of StringBuffer class using
“new”
Ex:- String s=”abc”‖; Ex:- StringBuffer s=new StringBuffer
(“abc”);
Page 15 Maharashtra State Board of Technical Education
Quick Revision

1) Which of this method of class StringBuffer is used to concatenate the string representation to the end of invoking
string?
a) concat()
b) append()
c) join()
d) concatenate()

2) Which of these class is superclass of String and StringBuffer class?


a) java.util
b) java.lang
c) ArrayList
d) None of the mentioned

Page 16 Maharashtra State Board of Technical Education

You might also like