Beginning Programming with Java For Dummies
Book image
Explore Book Buy On Amazon
You can choose to go for a variation on a theme. Let’s take a look at assigning values in Java. Here, it takes two lines to give the amount variable its first value:

double amount;

amount = 5.95;

You can do the same thing with just one line:

double amount = 5.95;

When you do this, you don’t say that you’re “assigning” a value to the amount variable. The line double amount=5.95 isn’t called an “assignment statement.” Instead, this line is called a declaration with an initialization. You’re initializing the amount variable. You can do all sorts of things with initializations, even arithmetic:

doublegasBill = 174.59;

doubleelecBill = 84.21;

double H2OBill = 22.88;

double total = gasBill + elecBill + H2OBill;

Moving Java variables from place to place

It helps to remember the difference between initializations and assignments. For one thing, you can drag a declaration with its initialization outside of a method:

//This is okay:

classSnitSoft {

static double amount = 5.95;

public static void main(String args[]) {

amount = amount + 25.00;

System.out.print("We will bill $");

System.out.print(amount);

System.out.println(" to your credit card.");

}

}

You can’t do the same thing with assignment statements:

//This does not compile:

classBadSnitSoftCode {

static double amount;

amount = 5.95; //Misplaced statement

public static void main(String args[]) {

amount = amount + 25.00;

System.out.print("We will bill $");

System.out.print(amount);

System.out.println(" to your credit card.");

}

}

java variables
A failed attempt to compile BadSnitSoftCode.

You can’t drag statements outside of methods. (Even though a variable declaration ends with a semicolon, a variable declaration isn’t considered to be a statement. Go figure!)

Notice how the word static was added to each declaration that was pulled out of the main method. This was done because the main method’s header has the word static in it. Not all methods are static. In fact, most methods aren’t static. But whenever you pull a declaration out of a static method, you have to add the word static at the beginning of the declaration.

Combining variable declarations in Java

This code has only one variable (as if variables are in short supply). You can get the same effect with several variables:

classSnitSoftNew {

public static void main(String args[]) {

doubleflashDrivePrice;

doubleshippingAndHandling;

double total;

flashDrivePrice = 5.95;

shippingAndHandling = 25.00;

total = flashDrivePrice + shippingAndHandling;

System.out.print("We will bill $");

System.out.print(total);

System.out.println(" to your credit card.");

}

}

The new code has three declarations — one for each of the program’s three variables. Because all three variables have the same type (the type double), you can modify the code and declare all three variables in one fell swoop:

doubleflashDrivePrice, shippingAndHandling, total;

Which is better — one declaration or three declarations? Neither is better. It’s a matter of personal style.

You can even add initializations to a combined declaration. When you do, each initialization applies to only one variable. For example, with the line

doubleflashDrivePrice, shippingAndHandling = 25.00, total;

the value of shippingAndHandling becomes 25.00, but the variables flashDrivePrice and total get no particular value.

TIP THE PARKING ATTENDANT

An online blog advises a $2 tip when a parking attendant fetches your car in a New York City garage. When the program runs, you type the garage's posted price for parking your car. The program tells you how much you'll pay after adding the $2 tip.

DOUBLE PRICE

Modify the code so that, whatever a flash drive normally costs, the program charges twice that amount. In other words, the price for a $5 flash drive ends up being $10, and the price for a $100 flash drive becomes $200.

About This Article

This article is from the book:

About the book author:

Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.

This article can be found in the category: