Storing Information in Variables

You can store a value in a variable at the same time that you create the variable in a Java program. You also can put a value in the variable at any time later in the program.

To set a starting value for a variable upon its creation, use the equal sign (=). Here’s an example of creating a double floating-point variable called pi with the starting value of 3.14:

double pi = 3.14;

All variables that store numbers can be set up in a similar fashion. If you’re setting up a character or a string variable, quotation marks must be placed around the value as described earlier in this hour.

You also can set one variable equal to the value of another variable if they both are of the same type. Consider the following example: ...

Get Sams Teach Yourself Java™ in 24 Hours, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.