- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Initialize and Compare Strings in Java?
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 Demopublic 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
- Related Articles
- How to initialize and compare strings?
- How to Initialize and Compare Strings in C#?
- How to compare strings in Java?
- How to declare and initialize constant strings in C#?
- Java Program to Compare Strings
- How do I compare strings in Java?
- Compare two Strings in Java
- Program to Compare two strings in Java
- Java Program to Compare Two Strings
- How to compare two strings without case sensitive in Java
- Compare two strings lexicographically in Java.
- Java Program to compare strings for equality
- Java Program to Compare two strings lexicographically
- How does the Java compareTo() method, compare strings?
- Compare the two Strings lexicographically in Java

Advertisements