Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

problem in "instanceof" operator and "searching(regex)"

 
Ranch Hand
Posts: 91
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys,

I am not able to understand two problems that i encountered in the kathy sierra book..

1) given that,


the book says that if we have a Foo array, then, Foo[] instanceof Foo/Bar/Face produces false. In the next line it is written that Foo[1] instanceof Foo/Bar/Face/Object produces true. Could you please explain me how we got the false and true in the above cases. The question is easy i suppose, but i donno, i guess i am getting confused over some point, so please help!

2) there is a problem (self-test) in the book as follows:



and the command line: java Regex2 "\d*" ab34ef

The answer to the above question is 01234456. I could not understand how the answer came. According to me we have to find "any zero or more occurence of a digit". in the source expression we have the number at position 2 and the group is 34, so the answer should be 234. then how come the answer is 1234456? Please explain!

Regards,
Komal


[HENRY: Added code tags]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Komal,

You queries are discussed in the following threads

Question:1

java ranch

Question2 :

SCJPFAQ



-
Gouri
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Komal Arora wrote:hey guys,

1) given that,
interface Face{}
class Bar implements Face{}
class Foo extends Bar{}

the book says that if we have a Foo array, then, Foo[] instanceof Foo/Bar/Face produces false. In the next line it is written that Foo[1] instanceof Foo/Bar/Face/Object produces true. Could you please explain me how we got the false and true in the above cases. The question is easy i suppose, but i donno, i guess i am getting confused over some point, so please help!



The instanceof operator has the following syntax:
<reference> instanceof <type>

At compile time, the <reference> and <type> should satisfy subclass-supertype relationship.
Otherwise, there will be a compile time error.

Following code prints false, because Bar IS-NOT-A Foo


But the following code prints true, because Foo IS-A Bar


If you try to compare Foo Array with Foo, it'll give compile time error. As they are inconvertible types - do not satisfy subtype-supertype relationship.

 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Threads in the links provided by Gouri discusses the mentioned problems...
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you.
 
Komal Arora
Ranch Hand
Posts: 91
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Gouri, the links really helped..

Thanks Trivikram for the explanation, it helped too..

Wouter, I will make sure to use codetags from the next time
 
Right! We're on it! Let's get to work tiny ad!
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic