Storing Information in Variables

You can put a value into 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 up a starting value for a variable upon its creation, use the equal sign (=). The following is an example of creating a floating-point variable called pi with the starting value of 3.14:

float 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 shown previously.

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 Programming with Java™ in 24 Hours, FOURTH 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.