• Post Reply Bookmark Topic Watch Topic
  • New Topic

String's function contains()  RSS feed

 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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 ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and welcome to the Ranch
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 56553
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 76
  • Mark post as helpful
  • send pies
  • 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: 56553
172
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 76
  • Mark post as helpful
  • send pies
  • 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: 56553
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 76
  • Mark post as helpful
  • send pies
  • 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: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both should print true.
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not like that, I meant to crate class Bar,Baz and interface Foo ...
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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: 56553
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you can write you own code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!