Variables

You must explicitly declare all variables before using them. The basic form of a variable declaration is this:

type name;

For example:

int x;

String lastName;

double radius;

Note that variable declarations end with semicolons because a variable declaration is a type of statement.

Variable names follow the same rules as other Java identifiers. In short, a variable name can be any combination of letters and numerals but must start with a letter.

CrossRef.eps For more information about naming identifiers, see Identifiers.

Most programmers prefer to start variable names with lowercase letters and then use camel case to capitalize the first letter of individual words within the name. firstName and sales TaxRate, for example, are typical variable names.

tip.eps You can declare two or more variables of the same type in a single statement by separating the variable names with commas and spaces:

int x, y, z;

CrossRef.eps Variables can be defined as class variables, instance variables, or local variables. For more information, see Class Variables, Instance Variables, and Local Variables.

Get Java For Dummies Quick Reference 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.