Thursday, June 12, 2025

Why character array is better than String for Storing password in Java? Example

Why character array is better than String for storing a password in Java was a recent question asked to one of my friends in a java interview. he was interviewing for a Technical lead position and has over 6 years of experience. Both Character array and String can be used to store text data but choosing one over the other is a difficult question if you haven't faced the situation already. But as my friend said any question related to String must have a clue on a special property of Strings like immutability and he used that to convince the interviewer. here we will explore some reasons why should you use char[] for storing passwords than String.

Thursday, November 14, 2024

How to Compare Two Strings Without Case Sensitivity in Java? Example

How to Compare Two Strings Without Case Sensitivity in Java

Java provides several ways to compare strings without taking case sensitivity into account. In this article, we'll explore different methods to perform case-insensitive string comparisons in Java.

Method Use Case Efficiency
equalsIgnoreCase() Checking equality High
compareToIgnoreCase() Ordering comparison High
String.CASE_INSENSITIVE_ORDER Sorting collections High
equals() + toUpperCase() Not recommended Low

1. Using equalsIgnoreCase()

The equalsIgnoreCase() method

This method is ideal for checking if two strings are equal without considering their case. It returns true if both strings contain the same characters, regardless of their case.

String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equalsIgnoreCase(str2); // Returns true

2. Using compareToIgnoreCase()

The compareToIgnoreCase() method

Use this method when you want to check which string is greater or smaller, or to find out their relative ordering in lexicographical or alphabetical order, without considering case.

String str1 = "Apple";
String str2 = "banana";
int result = str1.compareToIgnoreCase(str2); // Returns a negative value

3. Using String.CASE_INSENSITIVE_ORDER

The String.CASE_INSENSITIVE_ORDER comparator

This comparator is useful for sorting a list of strings in case-insensitive order.

List<String> fruits = Arrays.asList("Apple", "banana", "Cherry");
Collections.sort(fruits, String.CASE_INSENSITIVE_ORDER);
// Result: [Apple, banana, Cherry]

4. Using equals() + toUpperCase()

Combining equals() and toUpperCase()

This is a tricky way to compare two strings in Java in a case-insensitive manner. First, convert both strings to uppercase, then compare them using equals().

String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.toUpperCase().equals(str2.toUpperCase()); // Returns true

Note: This method is not recommended as it creates new String objects, which can be inefficient for large-scale comparisons.

Conclusion

When comparing strings without case sensitivity in Java, it's generally best to use the built-in methods like equalsIgnoreCase() for equality checks and compareToIgnoreCase() for ordering comparisons. 

These methods are optimized and provide clear, readable code. For sorting collections of strings, the String.CASE_INSENSITIVE_ORDER comparator is an excellent choice.

Remember, all these methods compare strings in lexicographical order. Choose the method that best fits your specific use case to ensure efficient and accurate string comparisons in your Java applications.

Sunday, October 8, 2023

2 Ways to remove whitespaces from String in Java? Example Tutorial

You can remove blanks for whitespaces from a Java String by using the trim() method of java.lang.String class of JDK API. Unlike SQL Server which have LTRIM() and RTRIM() methods to remove whitespace from String, Java doesn't have ltrim() and rtrim() methods to trim white spaces from left and right, instead it just have a trim() method which removes all spaces from String including space at the beginning and at the end. Since String is Immutable in Java, it's worth noting that this method return a new String if original String contains space either at beginning or end, otherwise it will return the same String back.