File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String indexOf method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String indexOf method" Watch "String indexOf method" New topic
Author

String indexOf method

Clint Smith
Greenhorn

Joined: Sep 20, 2009
Posts: 2
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
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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

Joined: Oct 13, 2005
Posts: 36478
    
  16
And welcome to JavaRanch
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: String indexOf method
 
Similar Threads
Searching and extracting
If String Contains (symbols) then call main method
find particular characters within a String ?
string to int
illegal characters