X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

String :

String is a collection of characters. In Java, String is represented in three ways :

  • String class




  • StringBuffer class




  • StringBuilder class




String which contain numbers and alphabets then it is called alphanumeric String.

Whenever we create a String it internally creates a character array.

String class is available within java.lang package (i.e. default package imported by Java).



String class :

The String class represents a character Strings.

String literals in Java program are implemented as an instance of String class.

Example :

class StringExample { public static void main(String[ ] args) { String s = new String("Algo"); s.concat("Java"); System.out.println(s); } }

Output :

Algo Note :

Once we have created a String object then we can't perform any changes in the existing object. If we are trying to perform any changes then a new object will be created. This behaviour is nothing but immutability of String object.



String() :-

It is a constructor which initializes a newly created String objects so that it represents an empty character sequence.


String(byte[] bytes) :-

It is a constructor which construct a new String by decoding the specified array of bytes using the platform's default character set.


String(char[] value) :-

It is a constructor which allocates a new String so that it represents the sequence of characters currently contained in the character array argument.


toString() :-

It is a method to find String representation of an object. Whenever we are trying to print any object reference internally toString() will be executed. toString() gives textual information. If we want to give hashcode then we must override toString().

X

hashcode :

It refers to a way of getting a number from an object so it can be stored in a Hashtable. In Java a hash code can be any integer, and each object type is responsible for generating its own.

Example 1 :

class StringExample { public static void main(String[ ] args) { String s1 = "ALGO"; String s2 = "JAVA"; System.out.println(s1); System.out.println(s2); System.out.println(s1.toString()); System.out.println(s2.toString()); } }

Output :

ALGO JAVA ALGO JAVA

Example 2 :

class StringExample { public static void main(String[ ] args) { String s1 = "ALGO"; String s2 = "JAVA"; String s3 = "ALGO"; String s4 = "JAVA"; System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); } }

Output :

ALGO JAVA ALGO JAVA
Note :

String constant is stored in constant pool of method area (class context area). Here, we have two String objects which are created in constant pool area.

Example 3 :

class StringExample { public static void main(String[ ] args) { String s1 = "ALGO"; String s2 = "JAVA"; String s3 = "ALGO"; String s4 = "JAVA"; System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); System.out.println(s3.hashCode()); System.out.println(s4.hashCode()); } } }

Output :

ALGO JAVA ALGO JAVA 2011731 2269730 2011731 2269730
Note :

hashCode() is a method of object class which returns hashcode of current object. For every object JVM will assign one unique id which is nothing but hashcode. Hashcode is a 32-bit integer value.

Example 4 :

class StringExample { public static void main(String[ ] args) { String s1 = "ALGO"; String s2 = "JAVA"; String s3 = "EXPERT"; s1 = "JAVA"; s2 = "ALGO"; System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); System.out.println(s3.hashCode()); } }

Output :

JAVA ALGO EXPERT 2269730 2011731 2059133482
Note :

Here, three String objects are created in constant pool area.

Comparing Strings :-

Comparing of String can be done using :

  • equals()




  • equalsIgnoreCase()




  • compareTo()




  • compareToIgnoreCase()




  • Equality operator (==)




equals() :

It is a method use to check equality of two Strings. It is a method of Object class and it is overridden within String class in order to compare content.

This method is case sensitive. It returns boolean value i.e. true or false.

Example :

import java.util.*; class StringExample { public static void main(String[ ] args) { Scanner s = new Scanner(System.in); System.out.print("Input Username :"); String username = s.nextLine(); System.out.print("Input Password :"); String password = s.nextLine(); if(username.equals("Algo") && password.equals("Java")) { System.out.println("Welcome to Algo World"); } else { System.out.println("Invalid Username or Password"); } } }

Output 1 :

Input Username :Algo Input Password :Java Welcome to Algo World

Output 2 :

Input Username :Algo Input Password :JAVA Invalid Username or Password

equalsIgnoreCase() :

It is a method use to check equality of two Strings ignoring case consideration (i.e. case insensitive).

It returns boolean value i.e. true or false.

Example :

class StringExample { public static void main(String[ ] args) { String s1 = "Algo"; String s2 = "ALGO"; String s3 = "algo"; System.out.println(s1.equals(s2)); System.out.println(s1.equalsIgnoreCase(s2)); System.out.println(s2.equals(s3)); System.out.println(s2.equalsIgnoreCase(s3)); } }

Output :

false true false true

compareTo() :

It is a method which compares one String to another String lexicographically.

It returns integral value.

It return '0' if String1 == String2

It return '> 0' if String1 > String2

It return '< 0' if String1 < String2

It compares character by character according to ASCII value. The difference of ASCII value between first occurring dissimilar character in two compared Strings is return as an output.

X

lexicographically :

It refers to a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters.

Example :

class StringExample { public static void main(String[ ] args) { String s1 = "abc"; String s2 = "abc"; String s3 = "ab"; String s4 = "abcd"; String s5 = "abC"; String s6 = "aBC"; String s7 = "ABC"; String s8 = "bbc"; System.out.println(s1.compareTo(s2)); System.out.println(s1.compareTo(s3)); System.out.println(s1.compareTo(s4)); System.out.println(s1.compareTo(s5)); System.out.println(s1.compareTo(s6)); System.out.println(s1.compareTo(s7)); System.out.println(s1.compareTo(s8)); System.out.println(s8.compareTo(s1)); } }

Output :

0 1 -1 32 32 32 -1 1

compareToIgnoreCase() :

It is a method which compares one String to another String lexicographically, ignoring case difference.

It returns integral value.

It return '0' if String1 == String2

It return '> 0' if String1 > String2

It return '< 0' if String1 < String2

It compares character by character according to ASCII value. The difference of ASCII value between first occurring dissimilar character in two compared Strings is return as an output.

Example :

class StringExample { public static void main(String[ ] args) { String s1 = "abc"; String s2 = "abc"; String s3 = "ab"; String s4 = "abcd"; String s5 = "abC"; String s6 = "aBC"; String s7 = "ABC"; String s8 = "bbc"; System.out.println(s1.compareToIgnoreCase(s2)); System.out.println(s1.compareToIgnoreCase(s3)); System.out.println(s1.compareToIgnoreCase(s4)); System.out.println(s1.compareToIgnoreCase(s5)); System.out.println(s1.compareToIgnoreCase(s6)); System.out.println(s1.compareToIgnoreCase(s7)); System.out.println(s1.compareToIgnoreCase(s8)); System.out.println(s8.compareToIgnoreCase(s1)); } }

Output :

0 1 -1 0 0 0 -1 1
Equality operator (==) :

This operator is use for comparing object references.

It compares hashcode.

We can apply equality operators for all primitive data types.

Example :

class StringExample { public static void main(String[ ] args) { System.out.println(100 == 200); System.out.println('A' == 65.0); System.out.println(false == false); System.out.println(10 == 10.0); System.out.println(-128 == 127); } }

Output :

false true true true false

We can apply equality operator for object types also.

Let object references be 'r1' and 'r2' then r1 == r2 returns true if and only if both references r1 and r2 pointing to the same object.

Example :

class StringExample { public static void main(String[ ] args) { Thread t1 = new Thread(); Thread t2 = new Thread(); Thread t3 = new Thread(); Thread t4 = t1; System.out.println(t1 == t2); System.out.println(t1 == t3); System.out.println(t1 == t4); System.out.println(t2 == t1); System.out.println(t2 == t3); System.out.println(t2 == t4); System.out.println(t3 == t1); System.out.println(t3 == t2); System.out.println(t3 == t4); System.out.println(t4 == t1); System.out.println(t4 == t2); System.out.println(t4 == t3); } }

Output :

false false true false false false false false false true false false

There should be some relation between argument types (either parent to child or child to parent or same type) otherwise we will get compile time error saying "incompatible types".

Example :

class StringExample { public static void main(String[ ] args) { String s = new String("Algo"); Object o = new Object(); Thread t = new Thread(); System.out.println(s == o); System.out.println(o == s); System.out.println(o == t); System.out.println(t == o); } }

Output :

false false false false Note :

If we try to compare s == t then we get compile time error as CE: incompatible types : java.lang.String and java.lang.Thread or else if we try to compare t == s then we get compile time error as CE: incompatible types : java.lang.Thread and java.lang.String


Any object reference 'r' is compared to null using equality operator (i.e. r == null) then it will return false but null compared with null using equality operator (i.e. null == null) then it will return true.

Example :

class StringExample { public static void main(String[ ] args) { String s1 = new String("Algo"); String s2 = null; System.out.println(s1 == null); System.out.println(s2 == null); System.out.println(s1 == s2); System.out.println(null == null); } }

Output :

false true false true

In general, we can use equality operator (==) for reference comparison and equals() for content comparison.

Example :

class StringExample { public static void main(String[ ] args) { String s1 = new String("Algo"); String s2 = new String("Algo"); System.out.println(s1 == s2); // Reference comparison System.out.println(s1.equals(s2)); // Content comparison } }

Output :

false true

length() :

It is a method which return the number of characters in the String.

Example :

class StringExample { public static void main(String[ ] args) { String s = "AlgoJava"; System.out.println(s.length()); } }

Output :

8 Note :

length variable applicable for arrays where as length() is applicable for String objects



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.