CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Data

Data
Basic skills
Variables

How to initialize a variable in two statements

Syntax
type variableName;
variableName = value;
Example
int counter;            // declaration statement
counter = 1;            // assignment statement

How to initialize a variable in one statement

Syntax
type variableName = value;
Examples
int counter = 1;        	 // initialize an int variable
double price = 14.95;   	 // initialize a double variable
float interestRate = 8.125F; // F indicates a floating-point value
long numberOfBytes = 20000L; // L indicates a long integer
double distance = 3.65e+9;   // scientific notation
char letter = 'A'; 		// stored as a two-digit Unicode character
char letter = 65; 		// integer value for a Unicode character
boolean valid = false;     	 // where false is a keyword
int x = 0, y = 0;	 // initialize 2 variables with 1 statement

Previous | Primitive data types | Variables | Constants | Assignment statements and arithmetic expressions | Order of preference | Casting | Next