Found 9321 Articles for Object Oriented Programming

Compare two strings lexicographically in Java.

Fendadis John
Updated on 26-Feb-2020 06:41:22

5K+ Views

The compareTo() method of the String class. This method compares two Strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. This method returnsa negative integer if current String object lexicographically precedes the argument string.a positive integer if current String object lexicographically follows the argumenttrue when the strings are equal.ExampleLive Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str1 = "tutorials", str2 = "point";   ... Read More

What is Java String literal?

George John
Updated on 05-Sep-2022 11:34:39

3K+ Views

Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects. The Java platform provides the String class to create and manipulate strings. You can also create a String directly as − String greeting = "Hello world!"; A string literal should be enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a String object with its value in this case, "Hello world!'. Example Live Demo public class StringDemo { public static void main(String args[]) { ... Read More

Java program for String Concatenation.

Akshaya Akki
Updated on 26-Feb-2020 05:56:38

108 Views

The concat() method of the String class concatenates the specified string to the end of this string.Exampleimport 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

Java String.equals() versus ==.

Sai Nath
Updated on 26-Feb-2020 06:22:25

180 Views

The equals() 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 s1 = "tutorialspoint";       String s2 = "tutorialspoint";       String s3 = new String ("Tutorials Point");       System.out.println(s1.equals(s2));       System.out.println(s2.equals(s3));    } }Outputtrue falseYou can also compare two strings using == operator. But, it compares references to the given ... Read More

How to create String object in Java?

Moumita
Updated on 26-Feb-2020 05:55:48

1K+ Views

You can create a String by -Assigning a string value wrapped in " " to a String type variable.String message = "Hello Welcome to Tutorialspoint";Creating an object of the String class using the new keyword by passing the string value as a parameter of its constructor.String message = new String ("Hello Welcome to Tutorialspoint");Passing a character array to the String constructor.char arr[] = {'H','e','l','l','o'}; String message = new String(arr);

The Optimum Method to Concatenate Strings in Java.

Manikanth Mani
Updated on 26-Feb-2020 06:02:08

136 Views

The Optimum Method to Concatenate Strings in Java is the concat() method.This method 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.ExampleLive Demopublic 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 time

Java String Concatenation with Other Data Types.

Ayyan
Updated on 26-Feb-2020 05:44:11

943 Views

If you try to concatenate strings using concat() method, you can concat variables of string data type only.To concatenate strings with other data type you need to use the ‘+’ operator.Examplepublic class Test {    public static void main(String args[]){       String st1 = "Hello";       int data = 230;       String res = st1+data;       System.out.println(res);    } }OutputHello230

Concatenate Multiple Strings in Java.

Anjana
Updated on 26-Feb-2020 05:43:30

4K+ Views

You can concatenate multiple strings using the ‘+’ operator of Java.Examplepublic 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

Differences between HashMap and Hashtable in Java

Arushi
Updated on 18-Jun-2020 07:12:18

1K+ Views

Hashtable was part of the original java.util and is a concrete implementation of a Dictionary. However, Java 2 re-engineered Hashtable so that it also implements the Map interface. Thus, Hashtable is now integrated into the collections framework. It is similar to HashMap, but is synchronized.Like HashMap, Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want to be linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is ... Read More

Java string concat() method vs "+" operator

Akshaya Akki
Updated on 30-Jul-2019 22:30:21

208 Views

The notable differences between concat() method and +operator are:concat() method+operatorYou cannot concat a null value to a String using the concat() method.You can concat null value using the ‘+’ operator.Using the concat() method you can concat only two String variables.Using the ‘+’ operator you can concat multiple values.Using the concat() method you can concat only String type of arguments.Using the ‘+’ operator you can concat any type of arguments.

Advertisements