Saved Bookmarks
| 1. |
In Java, how can two strings be compared? |
|
Answer» In Java, there are several ways for comparing two strings. The following are a few of them:
Syntax: str1.equals(STR2);For example: Input 1= ScalerInput 2= InterviewBitOutput= falseInput 1= ScalerInput 2= ScalerOutput= trueInput 1= ScalerInput 2= scalerOutput= false
For Example: Input 1= ScalerInput 2= InterviewBitOutput= falseInput 1= ScalerInput 2= ScalerOutput= trueInput 1= ScalerInput 2= scalerOutput= true
Syntax: Object.equals(str1, str2)For example: Input 1= ScalerInput 2= InterviewBitOutput= falseInput 1= ScalerInput 2= ScalerOutput= trueInput 1= ScalerInput 2= nullOutput= falseInput 1= nullInput 2= nullOutput= True
Syntax: str1.compareTo(str2)Example: Input 1= InterviewBitInput 2= ScalerOutput= -10Input 1= ScalerInput 2= ScalerOutput= 0Input 1= ScalerInput 2= InterviewBitOutput= 10 |
|