
![]() Java Statements and Expressions
A statement defines a single Java operation. The following are simple Java statements:
A statement can return a value. If a statement should return a value, it is called an
expression. For example, int i = 1; is a statement; int i = 1 + 2; is an
expression.
As in Pascal and C, white space within statements is unimportant. A statement can be on
one line or on multiple lines. The thing you must remember is to end all Java statements
with a semicolon (;). If you don't remember this, the Java compiler will not compile your
source code.
Java programs can also contain compound statements. Compound statements are placed
within braces ({}). They can be placed anywhere a single statement can be placed.
Java Variables and Data Types
Variables are addresses in memory where values are stored. Each address has a name,
a type and a value. Before you can use a variable, you have to reserve a location in
memory for it. This is done by a declaration statement. After a variable has been
declared, you can assign values to it. As you will see later, you can declare a variable and
assign it a value at the same time.
Java has three kinds of variables: class variables, instance variables and local variables.
Unlike other languages, it does not have global variables. Instance variables are used to
define the attributes of a specific object. Class variables are similar except their values
apply to all of a class's instances (including the class itself). Local variables are declared
and used inside method definitions. Once a method containing a local variable has
executed, the variable ceases to exist.
Declaring Variables
Before a variable can be used in a Java program it must be declared. A variable
declaration consists of a type and a variable name:
Usually variable definitions are declared at the beginning of a method definition, however
they can be placed anywhere within a method definition that a regular Java statement can
go.
Local variables must be given a value before they can be used. If they are not, your
program will not compile. This restriction does not apply to instance or class variables.
Variable Names
Variable names in Java cannot start with a number. They can start with an underscore ( _ ),
a dollar sign ($) or a letter. After the initial character, a name can contain any letter or
number. You should be aware that the Java language is case sensitive. This means
variable count is not the same a variable Count. Java variables are usually made up of
several words combined into one word. By convention, the first word is lowercase, with
all following words capitalized:
All variable declarations must have a type, which determines how much memory is set
aside for the variable. A variable type can be one of three things:
This lesson will concentrate on the primitive and class types. You will learn about arrays
in lesson 5.
Primitive Types
These data types are called primitive because they are build into the system and are not
actual objects. They consist of such types as integers, floating-point numbers, characters
and boolean values.
Java has two types of floating-point numbers (numbers containing a decimal point). They
are float (32 bits, single precision) and double (64 bits, double precision).
The type char is used for individual characters (16 bits of precision, unsigned).
The boolean type can have one of two values, true or false.
All the primitive types are in lowercase. You have to be careful when you use these types
that they are in lowercase, because there are classes with the same name, but the first
letter is in uppercase. The primitive type boolean is different than the class Boolean.
Assigning Values to Variables
After a variable has been declared, you can assign a value to it by using the assignment
operator =. For example:
The symbols /* and */ are used to enclose multiline comments. All the text between the
two delimiters is ignored. For example:
Expressions and Operators
The simplest form of statement in Java that actually accomplishes anything is called an
expression. All expressions when executed, return a value. Most expressions use
operators. Operator are special symbols used for arithmetic, assignments, incrementing,
decrimenting and logical operations.
Arithmetic
There are five operators used for basic arithmetic:
Examples of Simple Arithmetic Output
If several variables are to initialized to the same value, you can string them together like
this:
In the assignment x = x + 4 the right side of the expression is evaluated first, then the
assignment takes place, adding 4 to the value of x. Because this operation is so common,
Java has several shorthand versions of the operation; see the table below:
Incrementing and Decrementing
The ++ and -- operators can be used to increment or decrement a variable's value by one
(1). For example, x++ results in the same value as the expression x=x+1.
The operators (++ or --) can be placed in front of or behind the value being incremented
or decremented. However, where you place the operators (++ or --) can make a
difference in the end value. For example, the following expressions yield very different
results:
When you place the operator in front of x, the value of x is assigned to y after x has been
incremented. If you place the operator after x, y gets the value of x before x is changed.
The program below demonstrates the difference:
Output
Comparisons
There are several expressions you can use to test for equality and magnitude. They all
return a boolean value of true or false. See the table below:
The equality operators == and != test if two values are equal or not:
The relational operators <, <=, > and >= test if the first value is less than, less than or
equal to, greater than, or greater than or equal to the second value. For example:
Boolean Operators
There are two boolean operators && (logical AND) and || (logical OR). There is no
conditional XOR operator.
AND evaluates to true if both its operands are true; else its false.
OR evaluates to false if both its operands are false, else its true.
Operator Precedence
Certain operator are evaluated before others. For example, multiplication and division are
always done before addition or subtraction, unless the addition and subtraction are
enclosed in parentheses. The following table shows the operator precedence. Operators
with the highest precedence are at the top of the chart. Operators at the same level of
precedence are evaluated from left to right.
String Arithmetic
The addition operator (+) can be used in Java to create and concatenate strings. You
have seen some examples of this in this lesson. For example:
The output is a single string. The values of the variables (x and y) are inserted in the appropriate locations in the string. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||