Found 9321 Articles for Object Oriented Programming

How to compare two strings without case sensitive in Java

George John
Updated on 26-Feb-2020 07:04:22

2K+ Views

The equalsIgnoreCase() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.ExampleLive Demopublic class Sample { public static void main(String args[]) { String Str1 = new String("This is really not immutable!!"); String Str2 = Str1; String Str3 = new String("THIS IS REALLY NOT IMMUTABLE!!"); boolean retVal; retVal = Str1.equalsIgnoreCase(Str2); System.out.println("Returned Value = " + retVal ); retVal = Str1.equalsIgnoreCase( Str3 ); System.out.println("Returned Value = " + retVal ); } }OutputReturned Value = true Returned Value = true

concat(), replace(), and trim() Strings in Java.

Manikanth Mani
Updated on 26-Feb-2020 06:27:31

1K+ Views

The concat() method of the String class appends one String to the end of another. The method returns a String with the value of the String passed into the method, appended to the end of the String, used to invoke this method.Examplepublic class Test {    public static void main(String args[]) {       String s = "Strings are immutable";       s = s.concat(" all the time");       System.out.println(s);    } }OutputStrings are immutable all the timeThis replace() method of the String class returns a new string resulting from replacing all occurrences of oldChar in ... Read More

Case sensitive string comparison in Java.

Moumita
Updated on 26-Feb-2020 06:45:45

3K+ Views

You can compare two strings using either equals() method or compareTo() method.Where, The equals() method compares this string to the specified object.The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.These two methods compare the given strings with respective to Case i.e. String with the different case are treated differently.ExampleFollowing example demonstrates the comparison of two strings using the equals() method.Live Demopublic class Sample{    public static void main(String args[]) {       String str = "Hello World";       String anotherString = "hello world";     ... Read More

String Concatenation by concat() method.

Alankritha Ammu
Updated on 26-Feb-2020 05:57:18

147 Views

You can concatenate two strings using the concat() method of the String class. This class concatenates the specified string to the end of this string.ExampleLive Demopublic class Test {    public static void main(String args[]){       String str1 = "Krishna";       String str2 = "Kasyap";       System.out.println(str1.concat(str2));    } }OutputkrishnaKasyap

Java String Comparison Methods.

Kumar Varma
Updated on 26-Feb-2020 06:49:35

470 Views

Java String class provides different comparison methods namely:The compareTo() methodThis method compares two Strings lexicographically. This method returns  a negative integer if current String object lexicographically precedes the argument string.  a positive integer if current String object lexicographically follows the argument  true when the strings are equal.Exampleimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str1 = "tutorials", str2 = "point";       // comparing str1 and str2       int retval = str1.compareTo(str2);       // prints the return value of the comparison     ... Read More

String Concatenation by + (string concatenation) operator.

Ayyan
Updated on 26-Feb-2020 05:57:58

317 Views

You can concatenate strings using the ‘+’ operator of Java.ExampleLive Demopublic class Test {    public static void main(String args[]){       String st1 = "Hello";       String st2 = "How";       String st3 = "You";       String res = st1+st2+st3;       System.out.println(res);    } }OutputHelloHowYou

Immutable String in Java

Sai Nath
Updated on 30-Jul-2019 22:30:21

511 Views

In Java immutable objects are those whose data can’t be changed or modified (once modified). String class is immutable i.e. once we create a String object its data cannot be modified.

Java Substring Comparisons

Rama Giri
Updated on 26-Feb-2020 06:24:32

956 Views

Following is an example which compares Strings and portion of strings in Java? Explain with an example.ExampleLive Demopublic class StringDemo {    public static void main(String[] args) {       String str1 = "tutorials point";       String str2 = str1.substring(10);       int result = str1.compareTo(str2);       // prints the return value of the comparison       if (result < 0) {          System.out.println("str1 is greater than str2");       }else if (result == 0) {          System.out.println("str1 is equal to str2");       }else {          System.out.println("str1 is less than str2");       }    } }Outputstr1 is less than str2

Java Program to Concatenate String.

Anjana
Updated on 26-Feb-2020 05:59:28

258 Views

The concat() method of the String class concatenates the specified string to the end of this string.ExampleLive Demoimport java.lang.*;    public class StringDemo {       public static void main(String[] args) {       // print str1       String str1 = "self";       System.out.println(str1);       // print str2 concatenated with str1       String str2 = str1.concat(" learning");       System.out.println(str2);       // prints str3 concatenated with str2(and str1)       String str3 = str2.concat(" center");       System.out.println(str3);    } }Outputself self learning self learning center

What is string constant pool in Java?

Jai Janardhan
Updated on 30-Jul-2019 22:30:21

642 Views

When you store a String asString str1 = "Hello";directly, then JVM creates a String object with the given value in a separate block of memory known as String constant pool.And whenever we try to create another String asString str2 = "Hello";JVM verifies whether any String object with the same value exists in the String constant pool, if so, instead of creating a new object JVM assigns the reference of the existing object to the new variable.And when we store String asString str = new String("Hello");using the new keyword, a new object with the given value is created irrespective of the ... Read More

Advertisements