Chapter 15. Primitive Variable Wrapper and String Classes


The previous chapter provided an overview of some of the classes and interfaces in the Java APIs. In this chapter we will explore in more detail some of the classes from the java.lang package that you are likely to use in your scientific or engineering programming work. In Chapter 8 we discussed that there are two general types of Java variables ”primitive variables that store a single value and reference type variables that contain references to arrays or to objects.

The two variable types are treated differently by Java. You usually cannot mix and match. For instance, you cannot pass a primitive variable to a method that expects a reference type argument. There may be times when you want to use a primitive type variable with a class or method that takes reference type arguments. For example, you might want to use one of the java.util collections classes to store a collection of integer variables.

Java provides a number of primitive variable wrapper classes that provide a reference type representation of a primitive type. These classes, in effect, are a bridge between the primitive and reference type worlds . They are referred to as wrapper classes, because they are used to wrap a class around a primitive type value. In addition to methods that convert their underlying value to any primitive type, the primitive variable wrapper classes define methods for converting a String representation of a value into a primitive value. One of the handy uses of the primitive variable wrapper classes is for parsing and converting character input data.

We will also discuss the String class in more detail in this chapter. We have already used String objects in many examples in the preceding chapters. A String is an object that represents a sequence of characters . You will use Strings when you read data from a file, display output to the screen, store the names of things as data members of a class, and countless other uses. We will outline the various ways to create a String , some of the important String methods, and how to convert Strings to and from other types.

The specific topics this chapter will cover are ”

  • Primitive variable wrapper classes

  • Creating primitive variable wrapper class objects

  • Converting a wrapper class object to a primitive value

  • The parse() methods

  • The String class

  • Obtaining String objects

  • Concatenating Strings

  • Other important String class methods

  • Converting primitive and reference types to Strings

  • Converting Strings to primitive values



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net