Assigning Variable Types

Variables are the main way that a computer remembers something as it runs a program. The Saluton program in Hour 2 used the greeting variable to hold “Saluton mondo!”, a message in Esperanto. The computer needed to remember that text a little later, so that the message could be displayed.

In a Java program, variables are created with a statement that must include two things:

  • The name of the variable

  • The type of information the variable will store

To see the different types of variables and how they are created, load the word processor you're using to write programs and set it up to start a new file. You will be creating a program called Variable.

Give your new file the name Variable.java and start writing the program by ...

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.