ch06 - 6 Java.lang-The Math Class, Strings, and Wrappers...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
6 Java.lang—The Math Class, Strings, and Wrappers CERTIFICATION OBJECTIVES Using the java.lang.String Class Using the java.lang.Math Class Using Wrapper Classes Using the equals() Method with Strings and Wrappers and Objects Two-Minute Drill Q&A Self Test
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
T his chapter focuses on the aspects of the java.lang package that you’ll need to understand for the exam. The java.lang package contains many of the most fundamental and often-used classes in the Java API. The exam will test your knowledge of String and StringBuffer basics, including the infamous immutability of String objects, and how the more common String and StringBuffer methods work. You will be tested on many of the basic methods included in the Math class (extremely interesting), and you will need to know all about wrappers—those methods that allow you to encapsulate your favorite primitives into objects, so that you can do object-like stuff with them (like put them in collections). Finally, we’ll reveal more than you’ve ever wanted to know about how the equals() method and == operator work when dealing with String objects and wrappers. As always, our focus will be on the knowledge you’ll really need to pass the exam. Undoubtedly some very wonderful methods will be overlooked in our tour of java.lang, but we’re dedicated to helping you pass this test. CERTIFICATION OBJECTIVE Using the String Class (Exam Objective 8.2) Describe the significance of the immutability of String objects. This section covers the String and StringBuffer classes. The key concepts we’ll cover will help you understand that once a String object is created, it can never be changed—so what is happening when a String object seems to be changing? We’ll find out. We’ll also cover the differences between the String and StringBuffer classes and when to use which. Strings Are Immutable Objects Let’s start with a little background information about strings. Strictly speaking you may not need this information for the test, but a little context will help you learn what you do have to know. Handling “strings” of characters is a fundamental aspect of most programming languages. In Java, each character in a string is a 16-bit 2 Chapter 6: Java.lang—The Math Class, Strings, and Wrappers
Background image of page 2
Unicode character. Because Unicode characters are 16 bits (not the skimpy 7 or 8 bits that ASCII provides), a rich, international set of characters is easily represented in Unicode. In Java, strings are objects. Just like other objects, you can create an instance of a String with the new keyword, as follows: String s = new String(); This line of code creates a new object of class String, and assigns the reference variable s to it. So far String objects seem just like other objects. Now, let’s give the String a value: s = "abcdef"; As you might expect the String class has about a zillion constructors, so you can use a more efficient shortcut: String s = new String("abcdef"); And just because you’ll use strings all the time, you can even say this: String s = "abcdef";
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 53

ch06 - 6 Java.lang-The Math Class, Strings, and Wrappers...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online