Found 9326 Articles for Object Oriented Programming

How to Initialize and Compare Strings in Java?

George John
Updated on 30-Jul-2019 22:30:21

122 Views

You can compare Strings using compareTo() method or, equals() method or, or == operator. Following example demonstrates how to initialize and compare strings in Java. Example Live Demo public class StringDemo { public static void main(String[] args) { String str1 = "tutorials"; String str2 = "point"; // comparing str1 and str2 int retval = str1.compareTo(str2); // prints the return value of the comparison if (retval < 0) { System.out.println("str1 is greater than str2"); } else if (retval == 0) { System.out.println("str1 is equal to str2"); } else { System.out.println("str1 is less than str2"); } } } Output str1 is less than str2

Append a single character to a string or char array in java?

Paul Richard
Updated on 26-Feb-2020 06:04:55

1K+ Views

The append() method in the StringBuffer class adds the specified String to the contents of the current String buffer. Using this method you can append a single character to a string or char array in java.Examplepublic class Test {    public static void main(String args[]) {       StringBuffer sb = new StringBuffer("Hi hello how are yo");       sb.append("u");       System.out.println(sb);    } }OutputHi hello how are you

How is the java memory pool divided?

Rama Giri
Updated on 18-Jun-2020 07:13:47

332 Views

Java memory pool is divided into 5 parts namely −Method area − The method area stores the class code − code of the variables and methods.Heap−The Java objects are created in this area.Java Stack− While running methods the results are stored in the stack memory.PC registers− These contain the address of the instructions of the methods.Native method stacks− Similar to Java stack, native methods are executed on the Native method stacks.

Java (JVM) Memory Types

Kumar Varma
Updated on 18-Jun-2020 07:14:20

3K+ Views

Java Virtual Machine is a program/software which takes Java bytecode (.class files)and converts the byte code (line by line) into machine understandable code.JVM contains a module known as a class loader. A class loader in JVM loads, links and, initializes a program. It−Loads the class into the memory. Verifies the byte code instructions.Allocates memory for the program.The memory in the JVM is divided into five different parts namely− Method area− The method area stores the class code − code of the variables and methods. Heap − The Java objects are created in this area. Java Stack− While running methods the results are stored in ... Read More

Comparing Strings and Portions of Strings in Java

Paul Richard
Updated on 26-Feb-2020 07:17:17

160 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

Difference between == and equals() method in Java.

Alankritha Ammu
Updated on 26-Feb-2020 06:42:24

2K+ 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.Example Live 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 of the given ... Read More

How do I compare strings in Java?

Kumar Varma
Updated on 26-Feb-2020 07:09:39

224 Views

You can compare two Strings in Java using the compareTo() method, equals() method or == operator.The compareTo() method compares two strings. 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. The result is a negative integer if this String object lexicographically precedes the argument string.The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would ... Read More

Why StringBuffer is mutable in Java?

Arjun Thakur
Updated on 26-Feb-2020 06:01:26

1K+ Views

We all know that the String class in Java is mutable i.e. once we create a String variable we cannot modify its data or do any manipulations.But, there may be scenarios where we need to modify the data of String variables. In such cases, we could use StringBuffer class.This class −is like a String, but can be modified. It contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.Is safe for use by multiple threads.

What is the difference between equals and compareTo in Java?

Rama Giri
Updated on 18-Jun-2020 07:22:14

2K+ Views

The compareTo() 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.The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would return true.ExampleLive Demopublic class StringDemo {    public static void main(String[] args) {   ... Read More

How to check if two strings are equal in Java?

Anjana
Updated on 26-Feb-2020 06:39:51

15K+ Views

You can check the equality of two Strings in Java using the equals() method. This 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.Exampleimport java.lang.* public class StringDemo {    public static void main(String[] args) {       String str1 = "Tutorialspoint";       String str2 = "Tutorialspoint";       String str3 = "Hi";             // checking for equality       boolean retval1 = ... Read More

Advertisements