• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic