0% found this document useful (0 votes)
52 views26 pages

Session 2.5 & 2.6 Packages and Strings

The document discusses Java concepts including inheritance types, interfaces, packages, strings, and ArrayLists. It covers method overriding, abstract classes, defining and implementing interfaces, differences between classes and interfaces, creating and importing packages, string handling functions, immutable strings, and creating and using ArrayLists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views26 pages

Session 2.5 & 2.6 Packages and Strings

The document discusses Java concepts including inheritance types, interfaces, packages, strings, and ArrayLists. It covers method overriding, abstract classes, defining and implementing interfaces, differences between classes and interfaces, creating and importing packages, string handling functions, immutable strings, and creating and using ArrayLists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Module 2 –Session 2.5 & 2.

6 19CSO01 – Java Fundamentals

MODULE 2 Inheritance types ,Interfaces,Packages,Strings

Inheritance types- Method overriding - Abstract Classes- -Interfaces -


Packages - Strings :String Handling Functions

Strings 1
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

1 Inheritance – Super classes- Sub classes - Types

2 Method Overiding

3 Abstract classes and methods

4 Interfaces – defining an interface, implementing interface, differences


between classes and interfaces and extending interfaces

5 Packages

6 String Handling Function

Strings 2
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Packages

• A java package is a group of similar types of classes, interfaces and


sub-packages.

• Providing access protection and namespace management

Some of the existing packages in Java are −


• java.lang − bundles the fundamental classes

• java.io − classes for input , output functions are bundled in this


package
Package in java can be categorized in two form,

• Built-in package and

• user-defined package.

Module 1 3
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Packages

• User defined package : Packages created by user


• Built-in package : The already defined package

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

Advantage of Java Package


• Java package is used to categorize the classes and interfaces so
that they can be easily maintained.
• Java package provides access protection.
• Java package removes naming collision.

Module 1 5
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Example for packages

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

• String is a sequence of characters, for e.g. “Hello”


• In java, string is an immutable object which means it is constant and can
cannot be changed once it has been created.

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

String str1 = "Welcome";


String str2 = "Welcome";

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");

public class Example{


public static void main(String args[]){
//creating a string by java string literal
String str = "Beginnersbook";
char arrch[]={'h','e','l','l','o'};
//converting char array arrch[] to string str2
String str2 = new String(arrch);
//creating another java string str3 by using new keyword
String str3 = new String("Java String Example");
//Displaying all the three strings
System.out.println(str);
System.out.println(str2);
System.out.println(str3);
}
}

Strings 9
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

String Methods

• Java String provides various methods that allow us to perform different


string operations. Here are some of the commonly used string methods

. Methods Description

concat() joins the two strings together

equals() compares the value of two strings

charAt() returns the character present in


the specified location

converts the string to an array of


getBytes()
bytes

returns the position of the


indexOf()
specified character in the string

Strings 10
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

returns the size of the specified


length() string

replace() replaces the specified old character


with the specified new character

substring() returns the substring of the string

breaks the string into an array of


split() strings

toLowerCase() converts the string to lowercase

toUpperCase() converts the string to uppercase

returns the string representation of


valueOf() the specified data

Strings 11
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Program on Strings

Run the code

https://onlinegdb.com/HJHC3GwWw

Strings 12
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Java Strings are Immutable

In Java, creating a string means creating an object of the String class.


When we create a string, we cannot change that string in Java. This is why
strings are called immutable in Java.

class Main {
public static void main(String[] args) {

// create string using the new keyword


String var = "Hello! World";
// adds another string to the string
var.replace('!', 'o');
System.out.println(var);
}
}
Strings 13
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

ArrayList

• ArrayList is a part of collection framework and is present in java.util


package. It provides us with dynamic arrays in Java.

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

The important points about Java ArrayList class are:

• Java ArrayList class maintains insertion order.


• Java ArrayList allows random access because array works at the index
basis.

Strings 14
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Hierarchy of ArrayList class


• Java ArrayList class extends AbstractList class which implements List
interface. The List interface extends the Collection class

Strings 15
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Creating an ArrayList

ArrayList<Type> al= new ArrayList<Type>();

Here, Type indicates the type of an array list


Example

ArrayList<Integer> arrayList = new ArrayList<integer>();

// create String type arraylist


ArrayList<String> arrayList = new ArrayList<String>();

Strings 16
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Add Methods

• Add elements to an ArrayList by using add() method


• This method has couple of variations, which we can use based on the requirement

alist.add("Steve");

alist.add(3, "Steve"); //This will add "Steve" at the fourth position

Method Description

void add(int index, E element) It is used to insert the specified element at


the specified position in a list.

void clear() It is used to remove all of the elements


from this list.

Strings 17
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Program using ArrayList

");//Adding object in arraylist


list.add(import java.util.*;
public class ArrayListExample1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("Mango"Apple");
list.add("Banana");
list.add("Grapes");
//Printing the arraylist object
System.out.println(list);
}
} [Mango, Apple, Banana, Grapes]

Strings 18
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Using index number

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

Change an element in ArrayList

• Use the set method to change an element in ArrayList.


• Provide the index and new element, this method then updates the
element present at the given index with the new given element.

  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

Iterating ArrayList using For-each loop

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

Remove elements from ArrayList


 remove() method to remove elements from an ArrayList

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
}
}

[Steve, Tim, Lucy, Pat, Angela, Tom]


[Tim, Lucy, Pat, Tom]
[Tim, Lucy, Tom]
Strings 22
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

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

Sorting in Descending order

• Collections.reverseOrder() method along with Collections.sort() in order


to sort the list in decreasing order.
Collections.sort(arraylist, Collections.reverseOrder());

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

System.out.println("Index of 90: "+al.indexOf(90));


System.out.println("Index of 20: "+al.indexOf(20));
System.out.println("Index of 15: "+al.indexOf(15));
}
}

Strings 25
Module 2 –Session 2.5 & 2. 6 19CSO01 – Java Fundamentals

Try Yourself

• Print duplicate characters from String?

• Program to print first non repeated character from String?

• Reverse String in Java using Iteration and Recursion

• 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

You might also like