How to compare strings in java?



How to compare strings in java?..

Answer / Avinash Kumar Gupta

In Java, you can compare two strings using the `equals()` method. Here's an example:
```java
String str1 = "Hello";
String str2 = "Hello";
if (str1.equals(str2)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are not equal.");
}
```
This will output: `"The strings are equal."` if both str1 and str2 contain the exact same characters.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is interface and its use?

8 Answers   HCL,


Can we use a switch statement with strings?

1 Answers  


What is functional interface in javatpoint?

1 Answers  


What is the symbol for average?

1 Answers  


What is an abstract class and what is it’s purpose?

1 Answers  


What is r in java?

1 Answers  


How many types of literals are there in JAVA?

1 Answers   Cap Gemini,


How do you achieve singleton?

1 Answers  


How can you add and remove nodes in jtree?

1 Answers  


What are different access specifiers in java? Explain

1 Answers  


WAP to illustrate the use of interface

2 Answers  


In which order the iterator iterates over collection?

1 Answers  


Categories