• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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

 
Ranch Hand
Posts: 207
27
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:
 
Sheriff
Posts: 11338
177
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: 11338
177
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: 207
27
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
 
author & internet detective
Marshal
Posts: 38687
677
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!