Packages

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

No. Once a String has been created you cannot change its data. (Although you can create a new String that is an altered version of the original.)

Packages

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 runs the same as its previous version. Unlike most packages, the explicit mention of the package java.lang is not needed.

QUESTION 15:

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?