Unit 7 String, Packages and Interfaces
Unit 7 String, Packages and Interfaces
2. It is a group of character which are enclosed written double quotes are (“ ”).
5. We can store the string literal by creating object for the following classes.
a. java.lang.String ;
b. java.lang.StringBuffer ;
c. java.lang.StringBuilder ;
7. Whenever we create a string object, implicitly our compiler will create one object for java.lang.String class
inside the String Constant Pool.
• String Literal:
Anything which is enclose written inside in double quotes is known as String Literals.
1. Whenever we used string literal in our java program an object is created implicitly by our
compiler for java.lang.String class inside String constant pool.
2. In case if the object is already present with same data then instead of creating new object,
the existing object reference is given.
Ex.
// When we give same data in the form of String Literal.
public class Demo
{
public static void main(String[] args)
{
String s1 = " Car ";
String s2 = " Car ";
System.out.println(s1 == s2 );
System.out.println(s1.equals(s2));
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
}
}
Note: In the string class the equals( ) method, hashCode( ), and toString( ) is already
overridden. Based on overridden it will compare two object on their states.
• java.lang.String
2. String ( string literal ) : It is used to creates string object by initializing with string literals.
3. String ( char[] ch) : It is used to creates strings by converting character array into string.
4. String ( byte[] b ) : It is used to creates string by converting byte array into string.
Output
Laila
Laila
False
true
• String Handling Methods:
String Handling in Java the methods are provided by the String class that allow you to
manipulate, analyze, and work with strings. The String class is part of the java.lang package
and provides a full set of methods for various string operations. Here are some of the key
string handling functions in Java.
Method Description Return Type
equals() Compares two strings. Returns true if the strings are equal, and false if not boolean
getBytes() Encodes this String into a sequence of bytes using the named charset, storing byte[]
the result into a new byte array
isEmpty() Checks whether a string is empty or not boolean
lastIndexOf() Returns the position of the last found occurrence of specified characters in a int
string
indexOf() Returns the position of the first found occurrence of specified characters in a Int
string
substring() Returns a new string which is the substring of a specified string String
toCharArray() Converts this string to a new character array char[]
These are just a subset of the many functions provided by the String class in Java.
Remember that strings in Java are immutable, meaning that operations that appear to modify
a string actually create a new string object. This is why classes like StringBuilder and
StringBuffer are often used when there are a lot of string concatenation or modification
operations involved.
Ex.
public class StringManipulation
{
public static void main(String[] args)
{
String s1 = new String("Hello");
String s2 = "Hello";
String s3 = "Hi";
String s4 = s3;
String s5 = new String("Hi");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(s3 == s4);
System.out.println(s3.equals(s4));
System.out.println(s5 == s3);
System.out.println(s5.equals(s3));
}
}
Ex.
public class StringManipulation
{
public static void main(String[] args)
{
String s1 = new String("Hello");
String s2 = new String("Hello");
String s3 = "Hello";
String s4 = "Hello";
String s5 = s1;
String s6 = s5;
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(s3 == s4);
System.out.println(s3.equals(s4));
System.out.println(s5 == s6);
System.out.println(s5.equals(s6));
}
Note: String is immutable in nature that’s why you cannot be modified the string. Still if you
try to modify the string object then the new object is created and modified data and reference
will return.
Ex.
public class StringImmutable
{
public static void main(String[] args)
{
String s1 = "Hello";
System.out.println(s1);
s1 = s1.concat(" World");
System.out.println(s1);
}
Que: How to iterate the String?
Ans:
}
Que: How to Reverse the String with concatenation?
Ans:
3. It is a final class.
4. String buffer doesn’t have string constant pool hence the object of String Buffer class are
mutable in nature.
6. toString( ) method, hashCode( ) method and equals( ) method of object class are
overridden in String Buffer class.
3) String class overrides the equals() method of StringBuffer class doesn't override the equals()
Object class. So you can compare the method of Object class.
contents of two strings by equals() method.
4) String class is slower while performing StringBuffer class is faster while performing
concatenation operation. concatenation operation.
5) String class uses String constant pool. StringBuffer uses Heap memory
Que: Difference between StringBuffer and StringBuilder
1) StringBuffer is synchronized i.e. thread safe. It StringBuilder is non-synchronized i.e. not thread
means two threads can't call the methods of safe. It means two threads can call the methods of
StringBuffer simultaneously. StringBuilder simultaneously.
2) StringBuffer is less efficient than StringBuilder. StringBuilder is more efficient than StringBuffer.
3) StringBuffer was introduced in Java 1.0 StringBuilder was introduced in Java 1.5
• Importing Packages:
Importing packages in Java allows you to use classes from other packages in your code. It
helps you organize and modularize your codebase by separating functionalities into different packages.
Here's how you can import packages and classes in Java
1. Importing a Specific Class: If you want to use a specific class from a package, you can import it
using the import statement at the beginning of your Java file. For example, if you want to use the
ArrayList class from the java.util package.
Ex.
import java.util.ArrayList;
public class MyArrayListExample
{
public static void main(String[] args)
{
ArrayList<String> myList = new ArrayList<>();// Use the ArrayList class here
}
}
2. Importing the Entire Package:
You can also import the entire package using the import statement. This allows you to
use any class within that package without specifying the package name each time. However,
you need to be careful if there are class name conflicts between packages.
Ex.
import java.util.*;
Note:
1. Remember that classes from the java.lang package (like String) are automatically imported, so you
don't need to explicitly import them.
2. Import statements must appear at the beginning of your Java file, before the package statement (if
present) and before the class declaration.
3. It's good practice to only import the specific classes you need rather than importing entire
packages, as this helps keep your code more organized and prevents potential naming conflicts.
public class PalindromeNaiveMethod
{
public static boolean isPalindrome(String str)
{
String rev = " ";// Initializing an empty string to store the reverse of the
original str
boolean ans = false;// Initializing a new boolean variable for the answer
for (int i = str.length() - 1; i >= 0; i--)
{
rev = rev + str.charAt(i);
}
if (str.equals(rev)) // Checking if both the strings are equal
{
ans = true;
}
return ans;
}
public static void main(String[] args)
{
String str = "bhushan";// Input string
str = str.toLowerCase();// Convert the string to lowercase
boolean A = isPalindrome(str);
System.out.println(A);
}
}