[Page
33]
2.5. Concepts
Summary
This chapter introduced many basic concepts:
printing the result of a statement (expression), math operators,
relational operators, types, casting, and
variables
.
2.5.1.
Statements
Java programs are made of statements. Java
statements can end in semicolons
';'
just like sentences
can end in periods "." in English. When you type statements in the
definitions pane (when you define
methods
) they
must
have some
sort
of punctuation to show the
end of the statement. One way to do this is to use a semicolon
';'
.
If you leave off the semicolon in the
interactions pane, it will print the result of the statement. If
you do end a statement with a semicolon in the interactions pane,
and you want to print the result, use
System.out.println(expression);
to print the result of the
expression.
> int numPeople = 3;
> double bill = 52.49;
> double amountPerPerson = bill / numPeople;
> System.out.println("Each person should pay: " + amountPerPerson);
Each person should pay: 17.496666666666666
Math
Operators
|
+
|
Addition
|
Used to add
numbers
together (3 + 4) = 7
|
|
-
|
Subtraction
|
Used to subtract one number from another (5 - 2)
= 3
|
|
*
|
Multiplication
|
Used to multiply two numbers together (2 * 3) =
6
|
|
/
|
Division
|
Used to divide one number by another (18/2) =
9
|
|
%
|
Modulus (Remainder)
|
Used to find the remainder of one number divided
by another (19 % 2) = 1
|
2.5.2. Relational
Operators
[Page
34]
|
<
|
Less Than
|
Used to check if one value is less than another
(2 < 3) is true
|
|
>
|
Greater Than
|
Used to check if one value is greater than
another (3 > 2) is true
|
|
==
|
Equals
|
Used to check if two values are the same (2 ==
2) is true
|
|
!=
|
Not Equals
|
Used to check if two values aren't equal (2 !=
3) is true
|
|
<=
|
Less Than or Equal
|
Used to check if one value is less than or equal
to another (2 <= 3) is true
|
|
>=
|
Greater Than or Equal
|
Used to check if one value is greater than or
equal to another (3 >= 2) is true
|
2.5.3. Types
A type is a description of the "kind of" thing
something is. It affects how much space is reserved for a variable
and how the bits in that space are interpreted. In this chapter, we
talked about several kinds of types (encodings) of data.
|
Floating point numbers
|
Java primitive types
double
or
float
, e.g., 5.2, -3.01, 928.3092
|
Numbers with a decimal point in them.
|
|
Integers
|
Java primitive types
int
,
byte
,
short
,
long
, e.g., -3, 5,239,
0
|
Numbers without a decimal pointthey can't
represent fractions.
|
|
Characters
|
Java primitive type
char
, e.g.,
'a'
,
'b'
,
'?'
|
A character is delimited by a pair of single
quotes.
|
|
Strings
|
Java
String
object, e.g.,
"Hello!"
|
A sequence of characters (including spaces,
punctuation, etc.) delimited on either end with a double quote
character.
|
|
Booleans
|
Java primitive type
boolean
with only
two possible values
|
The value of a boolean can be the reserved word
true
or the reserved word
false
.
|
2.5.4.
Casting
Java compilers recognize integer (-3) and
floating point values (32.43). The result of a mathematical
expression depends on the types involved in the expression.
Expressions that involve integer values will have integer results.
Expressions that have floating point (decimal) values will have
floating point results.
This can lead to unexpected results.
> 1 / 2
0
There are two ways to fix this problem. One is
to make one of the numbers a floating point number by adding
'.0'
(it doesn't matter which one) and the other is to use
casting to change the type of one of the numbers to a floating
point number (the primitive type
float
or
double
).
> 1.0 / 2
0.5
> (double) 1 / 2
0.5
[Page
35]
2.5.5.
Variables
Variables are used to store and access values.
You create variables by declaring them:
type
name
;
or
type name = expression;
. Declaring a
variable
reserves
space for the variable and allows the computer to
map the variable name to the address of that reserved space.
We introduced two types of variables: primitive
and object. Primitive variables are any of the types:
int,
byte, short, long, float, double, char
, or
boolean
.
Object variables refer to an object of a class. Use the class name
as the type when declaring object variables
ClassName
name;
or
ClassName name = expression;
.
Primitive variables store a value in the
reserved space for that variable. You can change the value using
variableName = value;
. You can access the value using
variableName
.
Object variables store a reference to an object
in the reserved space for that variable. Object variables do not
just store the address of the object. They store a reference to an
object which allows the address of the object to be determined.
If the object variable doesn't refer to any
object yet it has the value
null
. You can change what
object a variable references using
variableName =
object-Reference;
. You can access the referenced object using
variableName
.
|