Session 2.5 & 2.6 Packages and Strings
Session 2.5 & 2.6 Packages and Strings
Strings 1
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
2 Method Overiding
5 Packages
Strings 2
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Packages
• user-defined package.
Module 1 3
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Packages
Creating a packages
• Choose a name for the package and include a package statement
along with that name at the top of source file
package nameOfPackage;
Example :
package animals;
Module 1 4
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Packages
Importing a packages
• Packages can be imported in a program using the import
statement.
import packageName;
Example :
import java.awt.event.*;
// * signifies all classes in this package are imported
Module 1 5
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
package pack;
public class A{
public void msg() Output:Hello
{
System.out.println("Hello");
}
}
package mypack;
import pack.*;
class B
{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Module 1 6
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Strings
Creating a String
There are two ways to create a String in Java
• String literal
• Using new keyword
• Strings in Java are not primitive types (like int, char, etc).
• Instead, all strings are objects of a predefined class named String
• In Java, we use double quotes to represent a string.
Strings 7
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
String literal
String type = "java programming";
• Each time you create a string literal, the JVM checks the "string constant
pool" first. If the string already exists in the pool, a reference to the
pooled instance is returned
• If the string doesn't exist in the pool, a new string instance is created and
placed in the pool
Strings 8
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Using New Keyword
• Assign the same string object to two different literals use New Keyword
String str1 = new String("Welcome");
String str2 = new String("Welcome");
Strings 9
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
String Methods
. Methods Description
Strings 10
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Strings 11
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Program on Strings
https://onlinegdb.com/HJHC3GwWw
Strings 12
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
class Main {
public static void main(String[] args) {
ArrayList
• It is like an array, but there is no size limit. We can add or remove
elements anytime. So, it is much more flexible than the traditional array.
Strings 14
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Strings 15
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Creating an ArrayList
Strings 16
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Add Methods
alist.add("Steve");
Method Description
Strings 17
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Strings 18
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
import java.util.ArrayList;
class Main {
public static void main(String[] args){
ArrayList<String> animals = new ArrayList<>();
// Add elements
animals.add(0,"Dog");
animals.add(2,"Cat");
animals.add(1,"Horse");
System.out.println("ArrayList: " + animals);
}
} ArrayList: [Dog, Cat, Horse]
Strings 19
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
import java.util.ArrayList;
public class JavaExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("Jim");
names.add("Jack");
names.add("Ajeet");
names.add("Chaitanya");
names.set(0, "Lucy");
System.out.println(names);
}
}
Strings 20
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
import java.util.*;
public class ArrayListExample3{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("Mango");//Adding object in arraylist
list.add("Apple");
list.add("Banana");
list.add("Grapes");
//Traversing list through for-each loop
for(String fruit:list) Mango
System.out.println(fruit); Apple
Banana
}
Grapes
}
Strings 21
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
mport java.util.*;
System.out.println(alist);
class JavaExample{
//Removing "Steve" and "Angela"
public static void main(String args[]){
alist.remove("Steve");
ArrayList<String> alist=new ArrayList<String>();
alist.remove("Angela");
alist.add("Steve");
//displaying elements
alist.add("Tim");
System.out.println(alist);
alist.add("Lucy");
//Removing 3rd element
alist.add("Pat");
alist.remove(2);
alist.add("Angela");
//displaying elements
alist.add("Tom");
System.out.println(alist);
//displaying elements
}
}
Sort ArrayList
import java.util.*;
class SortArrayList{
public static void main(String args[]){
//Creating a list of fruits
List<String> list1=new ArrayList<String>();
list1.add("Mango"); Apple
list1.add("Apple"); Banana
Grapes
list1.add("Banana"); Mango
list1.add("Grapes");
//Sorting the list
Collections.sort(list1);
//Traversing list through the for-each loop
for(String fruit:list1)
System.out.println(fruit);
}}
Strings 23
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
import java.util.*;
public class Details {
public static void main(String args[]){
ArrayList<String> arraylist = new ArrayList<String>();
arraylist.add("AA");
arraylist.add("ZZ");
arraylist.add("CC");
arraylist.add("FF");
Collections.sort(arraylist, Collections.reverseOrder());
System.out.println("ArrayList in descending order:");
for(String str: arraylist){
System.out.println(str);
}
}
Strings 24
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
indexOf() Method
• IndexOf(Object o) is used to find out the index of a particular element in a
list.
import java.util.ArrayList;
public class JavaExample {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(90);
al.add(15);
al.add(20);
al.add(90);
Strings 25
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals
Try Yourself
• print all duplicate character and their count in Java. For example, if given
is "Programming" then your program should print
g-2 . m-2 ,r -2
Strings 26