Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

String's function contains()

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the documentation String's function contains(CharSequence cs) takes an argument of type CharSequence. http://developer.android.com/reference/java/lang/String.html
How come that we can pass to the function argument of type String ?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Moose wrote:
How come that we can pass to the function argument of type String ?



The String class implements CharSequence. So, passing an argument of type String is the same as passing an argument of type CharSequence.

Henry
 
Marshal
Posts: 76476
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
… and welcome to the Ranch
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

… and welcome to the Ranch


thanks

The String class implements CharSequence. So, passing an argument of type String is the same as passing an argument of type CharSequence.


How do you know that String class implements CharSequence? On this documentation's page(http://developer.android.com/reference/java/lang/CharSequence.html) I don't see "String class implements CharSequence"
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Losty wrote:How do you know that String class implements CharSequence? On this documentation's page(http://developer.android.com/reference/java/lang/CharSequence.html) I don't see "String class implements CharSequence"



Click on the link on your first post. The documentation for String states that it implements CharSequence.

Henry
 
Campbell Ritchie
Marshal
Posts: 76476
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not the official site: this is. But if you look on both links, you will see

Oracle Site wrote:Interface CharSequence

All Known Subinterfaces:
Name

All Known Implementing Classes:
CharBuffer, Segment, String, StringBuffer, StringBuilder

and

Android website wrote:java.lang.CharSequence
Known Indirect Subclasses
AlteredCharSequence, CharBuffer, Editable, GetChars, Spannable, SpannableString, SpannableStringBuilder, Spanned, SpannedString, String, StringBuffer, StringBuilder

(My underlining in both cases.)
You can also look for the String class

Oracle website wrote:Class String

java.lang.Object
java.lang.String

All Implemented Interfaces:
Serializable, CharSequence, Comparable<String>

 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, thanks

That is not the official site:

how do you know that is not?

EDIT: This is official Android's site.
 
Campbell Ritchie
Marshal
Posts: 76476
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I linked to was the official Java® site. Many Java® classes are also used by Android, but the Oracle site has precedence.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the link that John first posted is the official documentation for Android.

If you're programming for Android, it's better to look at the Android documentation, because there might be small differences between what's available in Android and what's in standard Java.
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Android website wrote:
java.lang.CharSequence
Known Indirect Subclasses
AlteredCharSequence, CharBuffer, Editable, GetChars, Spannable, SpannableString, SpannableStringBuilder, Spanned, SpannedString, String, StringBuffer, StringBuilder



"String class implements CharSequence." and "String is indirect Subclasses of CharSequence " - Does that means the same and why so?
 
Campbell Ritchie
Marshal
Posts: 76476
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String is a subtype of CharSequence. The String code says explicitly implements CharSequence (also Serializable and Comparable<String>).

If class Foo implements interface Bar, then Foo is a subtype of Bar. If class Foo extends class Baz and Baz implements Bar, then Foo is still a subtype of Bar (but at one remove).
Look at the class Exception. You will see it extends Throwable and Throwable extends Object. You will see it says it implements Serializable. If you scroll down a bit, you do not see
public class Exception implements Serializable
but
public class Exception extends Throwable
Now if you look for Throwable, and scroll down, you will there see
public class Throwable extends Object implements Serializable
So Serializable is directly implemented by Throwable, but Exception is a subtype (indirectly) of Serializable too. If you look for any of the Exception subtypes (or Error subtypes) you will see they all implement Serializable, too. Examples NullPointerException and NoClassDefFoundError. If you look at their inheritance trees, you will see they are Serializable even though Serialzable is only declared in the Throwable class.

Do those examples make it easier to understand?
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do those examples make it easier to understand?



Yes but it would be more helpful if I see example in code.
 
Campbell Ritchie
Marshal
Posts: 76476
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both should print true.
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not like that, I meant to crate class Bar,Baz and interface Foo ...
 
Campbell Ritchie
Marshal
Posts: 76476
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
John Losty
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be a lot better if I could copy/paste it into IDE and run it.
 
Campbell Ritchie
Marshal
Posts: 76476
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely you can write you own code.
 
The only thing that kept the leeches off of me was this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic