• Tidak ada hasil yang ditemukan

FOUR WAYS TO STORE WHOLE NUMBERS

Dalam dokumen Buku Java For Dummies, 8th Edition (Halaman 81-86)

Java has four types of whole numbers. The types are byte, short, int, and long. Unlike the complicated story about the accuracy of types float and double, the only thing that matters when you choose among the whole number types is the size of the number you’re trying to store. If you want to use numbers larger than 127, don’t use byte. To store numbers larger than 32767, don’t use short.

Most of the time, you’ll use int. But if you need to store numbers larger than

When you do this, you don’t say that you’re assigning values to variables. The pieces of the declarations with equal signs in them aren’t really called assignment statements. Instead, you say that you’re initializing the variables. Believe it or not, keeping this distinction in mind is helpful.

For example, when you initialize a variable inside a program’s main method, you don’t have to specify the new variable’s type. You can start Listing 4-2 like this:

public class Millionaire {

public static void main(String[] args) { var amountInAccount = 0.0;

If you want, you can start Listing 4-3 this way:

public class ElevatorFitter {

public static void main(String[] args) { var weightOfAPerson = 150;

var elevatorWeightLimit = 1400;

var numberOfPeople = elevatorWeightLimit/weightOfAPerson;

In either case, the word var replaces the name of a type. This trick works because Java is smart. When you write

var amountInAccount = 0.0;

Java looks at the number 0.0 and realizes that amountInAccount is a double value.

It’s a number with digits to the right of the decimal point. In the same way, Java sees

var weightOfAPerson = 150;

and figures out on its own that weightOfAPerson has to be int value.

This var business doesn’t work if you don’t initialize your new variable. For example, the lines

// BAD CODE:

var numberOfCats;

numberOfCats = 3;

are unacceptable as far as Java is concerned. Java can’t wait until the assignment statement numberOfCats = 3 to decide what type of variable numberOfCats is.

Java wants to know immediately.

There are other situations in which the use of var is illegal, even with an initial- ization. For details, see Chapter 7.

Like everything else in life, initializing a variable has advantages and disadvantages:

»

When you combine six lines of Listing 4-3 into just one declaration, the code becomes more concise. Sometimes concise code is easier to read.

Sometimes it’s not. As a programmer, it’s your judgment call.

»

By initializing a variable, you might automatically avoid certain pro- gramming errors. For an example, see Chapter 7.

»

In some situations, you have no choice. The nature of your code forces you either to initialize or not to initialize. For an example that doesn’t lend itself to variable initialization, see the deleting-evidence program in Chapter 6.

Experimenting with JShell

The programs in Listings  4-2 and 4-3 both begin with the same old, tiresome refrain:

public class SomethingOrOther {

public static void main(String[] args) {

A Java program requires this verbose introduction because

»

In Java, the entire program is a class.

»

The main method is called into action automatically when the program begins running.

I explain all of this in Chapter 3.

Anyway, retyping this boilerplate code into an editor window can be annoying, especially when your goal is to test the effect of executing a few simple state- ments. To fix this problem, the stewards of Java came up with a new tool in Java 9. They call it JShell.

Instructions for launching JShell differ from one computer to the next. For instructions that work on your computer, visit this book’s website (http://

JavaForDummies.allmycode.com).

When you use JShell, you hardly ever type an entire program. Instead, you type a Java statement, and then JShell responds to your statement, and then you type a second statement, and then JShell responds to your second statement, and then you type a third statement, and so on. A single statement is enough to get a response from JShell.

Some folks have tweaked JShell to make its behavior a bit different from what I describe in this book. If you’re running IntelliJ IDEA’s JShell Console or some other specialized JShell variant, be sure to check the vendor’s documentation.

JShell is only one example of a language’s Read Evaluate Print Loop (REPL). Many programming languages have REPLs and, with Java 9, the Java language finally has a REPL of its own.

In Figure 4-7, I use JShell to find out how Java responds to the assignment state- ments in Listings 4-2 and 4-3.

FIGURE 4-7:

An intimate conversation between JShell and me.

When you run JShell, the dialogue goes something like this:

jshell> You type a statement JShell responds

jshell> You type another statement JShell responds

For example, in Figure 4-7, I type double amountInAccount and then press Enter.

JShell responds by displaying amountInAccount ==> 0.0

Here are a few things to notice about JShell:

»

You don’t have to type an entire Java program.

Typing a few statements such as double amountInAccount amountInAccount = 50.22

amountInAccount = amountInAccount + 1000000.00

does the trick. It’s like running the code snippet in Listing 4-1 (except that Listing 4-1 doesn’t declare amountInAccount to be a double).

»

In JShell, semicolons are (to a large extent) optional.

In Figure 4-7, I type a semicolon at the end of only one of my nine lines.

For some advice about using semicolons in JShell, see Chapter 5.

»

JShell responds immediately after you type each line.

After I declare amountInAccount to be double, JShell responds by telling me that the amountInAccount variable has the value 0.0. After I type amountInAccount = amountInAccount + 1000000.00, JShell tells me that the new value of amountInAccount is 1000050.22.

»

You can mix statements from many different Java programs.

In Figure 4-7, I mix statements from the programs in Listings 4-2 and 4-3.

JShell doesn’t care.

»

You can ask JShell for the value of an expression.

You don’t have to assign the expression’s value to a variable. For example, in Figure 4-7, I type

JShell responds by telling me that the value of elevatorWeightLimit / weightOfAPerson is 9. JShell makes up a temporary name for that value.

In Figure 4-7, the name happens to be $8. So, on the next line in Figure 4-7, I ask for the value of $8 +1, and JShell gives me the answer 10.

»

You can even get answers from JShell without using variables.

On the last line in Figure 4-7, I ask for the value of 42 + 7, and JShell generously answers with the value 49.

While you’re running JShell, you don’t have to retype commands that you’ve already typed. You don’t even have to copy and paste commands. If you press the up-arrow key once, JShell shows you the command that you typed most recently.

If you press the up-arrow key twice, JShell shows you the next-to-last command that you typed. And so on. When JShell shows you a command, you can press the left- and right-arrow keys to move to any character in the middle of the com- mand. You can modify characters in the command. Finally, when you press Enter, JShell executes the newly modified command.

To end your run of JShell, you type /exit (starting with a slash). But /exit is only one of many commands you can give to JShell. To ask JShell what other kinds of commands you can use, type /help.

With JShell, you can test your statements before you put them into a full-blown Java program. That makes JShell a truly useful tool.

Dalam dokumen Buku Java For Dummies, 8th Edition (Halaman 81-86)

Dokumen terkait