No. Once a String
object has been created you cannot change its
data. (Although you can create a new String
object that is an altered
version of the original.)
A group of related classes is often put into a collection
called a package.
Many packages come standard with Java.
For example, the String
class is part of the
package called java.lang
.
Several useful classes are part of that package.
You can use the classes in this package without doing anything special.
Other standard packages also come with Java.
To use a class from one of these packages your program must
tell the compiler what package contains the class.
One way to do this is to use the package name with the class.
Here is a program that explicitly mentions where the String
class
is found:
class ImportDemo01 { public static void main ( String[] args ) { java.lang.String str; int len; str = new java.lang.String( "Elementary, my dear Watson!" ); len = str.length(); System.out.println("The length is: " + len ); } }
This program works the same way as the previous version.
The package
java.lang
does not need to be explicitly
mentioned when you need to use String
,
although it does not hurt to do so.
The class Scanner
is not part of the java.lang
package.
Does a program that creates a Scanner
object need to tell the compiler
where that class can be found?