• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String indexOf method

 
Clint Smith
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

The documentation for indexOf method shows that it can take an int. Yet I can pass in a character and it works just fine. What is going on here? Why wouldn't they build that method with a signature with a char which seems more appropriate for when you are searching a string for a specific character.

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet I can pass in a character and it works just fine. What is going on here?


A char can be implicitely casted to an int, so it works just fine.

Why wouldn't they build that method with a signature with a char which seems more appropriate for when you are searching a string for a specific character.


Apparently, there are some unicode characters that don't fit into a char -- something called a codepoint, that is the size of an int. Maybe someone else can explain this, as I never used codepoints before.

Henry

 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clint Smith wrote:Hi folks,

The documentation for indexOf method shows that it can take an int. Yet I can pass in a character and it works just fine. What is going on here? Why wouldn't they build that method with a signature with a char which seems more appropriate for when you are searching a string for a specific character.

Thanks


It's a good question. I think if you better understand the relationship between int and char you will be ok.

for example you can have a line

int n = 'Q';

Then system.out.println(n); will display the number 81.

I found the following page to be very helpful as a reference. The key columns for what we are discussing is DEC and Symbol

http://www.ascii-code.com/


p.s. ok Henry snuck in ahead of me.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic