Strings and Object References in Java
9. Class String
Answer:
The variables and methods of a class will be documented somewhere.
Class String
String
Use Google or another search engine to find documentation for class String
. Search for Java String.
Here is a short version of the documentation for String
.
// Constructors public String(); public String(String value); // Methods public char charAt(int index); public String concat(String str); public boolean endsWith(String suffix); public boolean equals(Object anObject); public boolean equalsIgnoreCase(String anotherString); public int indexOf(int ch); public int indexOf(String str); public int length(); public boolean startsWith(String prefix); public String substring(int beginIndex ); public String substring(int beginIndex, int endIndex); public String toLowerCase(); public String toUpperCase(); public String trim(); |
public String concat(String str); --+--- --+--- --+-- ----+---- | | | | | | | | | | | +---- says that the method requires a | | | String reference parameter | | | | | +----- the name of the method | | | +----- the method returns a reference | to a new String object | +----- anywhere you have a String object, you can use this method
public
will be explained in greater detail in another chapter.
Question 9:
Is the following code correct?
String first = "Red " ; String last = "Rose" ; String name = first.concat( last );
You don't have to know what
concat()
does (yet); look at the documentation and see
if all the types are correct.