Page 468 - Introduction to Programming with Java: A Problem Solving Approach
P. 468

                434 Chapter 11 Type Details and Alternate Coding Mechanisms 11.1 Introduction
In Chapters 3 and 4, you learned Java language basics. Among other things, you learned about data types, type conversions, and control statements. This chapter describes some additional data types and additional type conversions. It also describes some alternative control statement coding mechanisms.
Chapter 3 introduced you to some of Java’s integer and floating-point types of numbers, and Chapter 5 showed you how to find the limits of their ranges. In this chapter, you’ll see two more integer types, and for all of the numerical types you’ll learn the amount of storage needed, the precision provided, and how to use range limits. Chapter 3 introduced you to the use of the character type, char. In this chapter, you’ll see that each character has an underlying numeric value, and you’ll learn how to use those values. Chapter 3 intro- duced you to type conversion with the cast operator. In this chapter, you’ll learn more about type conver- sions. Chapter 3 introduced you to the increment and decrement operators. In this chapter, you’ll discover that you can move the positions of these operators (before or after the variable) to control when they act. Chapter 3 introduced you to assignment operators. In this chapter, you’ll see how you can embed assign- ments within expressions to make code more compact.
Chapter 4 introduced you to several kinds of conditional evaluations. In this chapter you’ll learn about the conditional operator that can take on either of two possible values depending on a boolean condition. You’ll also learn about short-circuit evaluation which can prevent errors by stopping a “dangerous” condi- tional evaluation in certain situations. In addition, you’ll learn more about loops. Specifically, you’ll see empty-bodied loops and loops that terminate from within the loop’s body. And you’ll see alternative coding techniques for for loop headers.
 The material in this chapter will improve your understanding of several Java nuances and subtleties.
Apago PDF Enhancer
This will help you avoid problems in the first place, and it will help you create code that is more efficient and easier to maintain. It will also help you debug code that has problems. It might be your code, or it might be someone else’s code. As a real-world programmer, you’ll have to work with other people’s code, and you’ll need to understand what that code is doing.
Much of the material in this chapter could have been inserted at various places earlier in the text. However, it was not necessary for anything we did up until now, and we deferred it until now to keep from encumbering earlier presentations. The assembly of these details into one chapter at this point in the book provides an excellent opportunity for review. As you go through this chapter, integrate this new material into what you learned before and see how it enriches your understanding of those topics.
11.2 Integer Types and Floating-Point Types
This section supplements the numeric data types material you studied in Chapter 3, Section 3.13.
Integer Types
Integer types hold whole numbers (whole numbers are numbers without a decimal point). Figure 11.1 shows the four integer types. The types are ordered in terms of increasing memory storage requirements. Type byte variables require only 8 bits, so they take up the least amount of storage. If you have a program that’s taking too much space in memory, you can use smaller types for variables that hold small values. Using smaller types means less storage is needed in memory. Now that memory has become relatively cheap, types byte and short are not used very often.
 






















































































   466   467   468   469   470