All variables in a Java program must be declared. This is something like defining a picture of a variable in COBOL. In Java, declaring a variable means stating its data type and name, like this:
String name;
This declares a variable named name which can contain a string.
The other most common data types in Java are:
char -- to hold a single character
int -- to hold an integer (a whole number without a decimal part)
double -- to hold a number with a decimal part
boolean -- can hold the value true or the value false. These are both keywords in Java. You do not need to put quotes around them.
Variable names can include letters, digits, and underscores (_). A variable name should usually begin with a small letter, although it can also begin with an underscore..
age
name2A
If there is more than one word in the variable name, you can either capitalize each word after the first:
numberOfGuests
or you can use underscores between the words:
number_of_guests
Usually, you declare each variable on a separate line:
int numberOfGuests;
double pricePerPound;
String firstName;
However, if two variables are the same type, you can declare them both on one line:
double regularPrice, salePrice;
You initialize Java variables with the assignment operator, =. The assignment operator assigns the value on the right of the equals sign to the variable on the left side of the equals. For example:
name = "Bob";
places the string "Bob" in the variable "name", and
age = 7;
places the value 7 in the variable "age".
The code below declares several values, initializes them, then prints them out: Notice that it also shows how strings and variables can be combined in the System.out.println method.
import java.io.*;
public class HW
{
public static void main (String
argv[])
{
String name
= "Bob Hartley";
boolean
registered = true;
int age;
double
salary;
char gender;
age = 42;
salary =
32.15;
gender =
'M';
System.out.println("My name is " + name + ".");
System.out.println("My age is " + age + " and I make " +
salary + " per hour.");
System.out.println ("Registered = " + registered);
}
}
Exercise 3:
Write a program (name the application class Exercise3) that declares variables for:
The name of a movie, its rental price (which could include cents!), whether or not it is currently in-stock, and a group number (1 to 5) which indicates what type of movie it is (1 = Drama, 2 = Comedy, etc.)
Assign values of your choice to each variable, then print them out like this (for example)::
Gone With the Wind
Rents for: $2.50
In stock: true
Group: 1
Solution is here.
Comments are lines that are not compiled. They are used to document the code. To place comments in your Java code, put // in front of the comment. All characters following the // to the end of the line will be ignored by the compiler. For example:
// next 5 lines declare all variables
// some of
them are initialized at declaration
// others
are assigned variables later in the program
String name
= "Bob Hartley";
boolean
registered = true; // registered voter variable
int age;
double
salary;
char
gender;
age = 42;
salary =
32.15;
gender =
'M';
If you are writing a multiline comment (such as the 3 first lines above), you can comment them all out together by using /* and */ as shown below:
/* next 5 lines declare all variables
some of them are initialized at declaration
others are assigned variables later in the program
*/
String name
= "Bob Hartley";
boolean
registered = true; // registered voter variable
int age;
double
salary;
char
gender;
Exercise 4:
Add comments of your choice to the previous exercise. Test your code by compiling it to demonstrate that the comments do not affect the compilation.
Solutions to this exercise will vary, so no solution is provided.
For next syntax lesson, click here.