-2
String s="rahul".substring(0, 1);
  s==Character.toString('r') //2nd

2nd evaluates to false but it should evaluate to true since i am converting 'r' to a String. Why i am getting false as a result?

By doing this i am able to make the condition true

 //char s=name.charAt(0);
 // s=='r'

;

Pale Blue Dot
  • 67
  • 2
  • 12

3 Answers3

1

Try it as: s.equals(Character.toString('r'). For more details What's the difference between ".equals" and "=="?

Community
  • 1
  • 1
Abhi
  • 341
  • 7
  • 15
0

Instead of comparing Strings with == you should use equals().

String s="rahul".substring(0, 1);
System.out.println(s.equals(Character.toString('r'))); 
Uli
  • 1,390
  • 1
  • 14
  • 28
0

Use .equals instead, see code below:

    //current code
    System.out.println(s==Character.toString('r')); 

    //change to be as follows
    s.equals( Character.toString('r'));
    System.out.println(s.equals( Character.toString('r')));
Sudhir
  • 1,432
  • 3
  • 15
  • 16