Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The parameter type of indexOf() isn't understandable,on page 107(Java OCA 8 Programmer I Study Guide

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 107, first and second rows are:
When I practice this code in Netbeans I saw that the parameter type of indexOf() is int.


Then I look through Oracle documentation:
It is confusing for me, parameter type is int but we pass char type value as parameter?! When we pass int, return value always be “-1”. What happens?


When I note above question I saw another minor type. On the same page, second and fourth rows, second parameter type of method are incorrect:
should be:
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:When I note above question I saw another minor type. On the same page, second and fourth rows, second parameter type of method are incorrect:

True! That's a typo: index is of course not a valid (primitive) data type and should be replaced with int
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:It is confusing for me, parameter type is int but we pass char type value as parameter?! When we pass int, return value always be “-1”. What happens?

No, it doesn't return always -1. Let's have a look at this example:Output: 1 -1 7

If you don't know what's happening behind the scenes, it looks like magic or even voodoo If you know what's happening, it's very easy (as always). Now let's look at this code snippet:Magic isn't it? What's really happening is very well explained in this (and following) posts. (Disclaimer: these posts are mine so probably a little biased )
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Let's have a look at this example:Output: 1 -1 7

If you don't know what's happening behind the scenes, it looks like magic or even voodoo If you know what's happening, it's very easy (as always). Now let's look at this code snippet:Magic isn't it?

Wow! It is a very good example, Roel, I understand everything now I don't know we can initialize int as this: int i3 = 'a'; I also learn this. Thanks, Roel
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mushfiq. Added to the list.

The "index" parameter type is a typo. The "char" vs "int" was me simplifying the method signature as I did here. In hindsight, that wasn't a good decision as it didn't make things easier
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic