You are on page 1of 12

Identifiers

• The name of a class, method, or variable in Java is called an identifier, which can be any string of characters as long as it
begins with a letter and consists of letters, numbers and underscore
• Exceptions: Reserved words cannot be used as identifiers

© 2014 Goodrich, Tamassia,


Java Primer 1 1
Goldwasser
Base Types

• Java has several base types, which are basic ways of storing data.
• An identifier variable can be declared to hold any base type and it can later be reassigned to hold another value
of the same type.

© 2014 Goodrich, Tamassia,


Java Primer 1 2
Goldwasser
Binary Numbers
• We use decimal number system
• There are 10 digits- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• Place value: 10 4 10 3 10 2 10 1 10 0
• Interpreting 231: 2 * 10 2 + 3 * 10 1 + 1 * 10 0 = 231

• Binary number system


• Only two digits- 0, 1
• Place value: 2 4 2 3 2 2 2 1 2 0
• Interpreting 101: 1 * 2 2 + 0 * 2 1 + 1 * 2 0 = 5
Arithmetic of binary numbers

carry 1 1
1 1 1
+ 1 0 1
sum 1 1 0 0
How to represent negative numbers
• Use a sign bit
• Let us say, we have 4 bits words
• 1 bit for sign (0: +ve, 1: -ve), 3 bits for number
• Largest (positive) number: 0111: which is 1*22 + 1 * 21 + 1 * 20 = 7
• Smallest (negative) number: 1111: which is -7
• Two representations for 0: 1000, 0000 which are not required
• Two’s complement is used which has only one representation for 0
• The range of numbers: -8 to 7
• For n bit word, range is -2 n-1 to 2 n-1 -1
Arithmetic Operators
• Java supports the following arithmetic operators:

• If both operands have type int, then the result is an int; if


one or both operands have type double, the result is a
double.
• Both operands int, division gives truncated result.
• %gives remainder as output; int i = 5/2; i will be 1.
© 2014 Goodrich, Tamassia, Goldwasser Java Primer 1 6
Increment and Decrement Ops
• Java provides the plus-one increment (++) and
decrement (−−) operators.
• If such an operator is used in front of a variable
reference, then 1 is added to (or subtracted from) the
variable and its value is read into the expression.
• If it is used after a variable reference, then the value is
first read and then the variable is incremented or
decremented by 1.

© 2014 Goodrich, Tamassia, Goldwasser Java Primer 1 7


Logical Operators: output is of boolean type which is either true or false

• Java supports the following operators for numerical values, which result in Boolean values:

• Boolean values also have the following operators:

• The and and or operators short circuit, in that they do not evaluate the second operand if
the result can be determined based on the value of the first operand.

© 2014 Goodrich, Tamassia, Goldwasser Java Primer 1 8


Operator Precedence

© 2014 Goodrich, Tamassia, Goldwasser Java Primer 1 9


Casting
• Casting is an operation that allows us to change the type of a value.
• We can take a value of one type and cast it into an equivalent value of
another type.
• There are two forms of casting in Java: explicit casting and implicit
casting.

© 2014 Goodrich, Tamassia, Goldwasser Java Primer 1 10


Explicit Casting
• Java supports an explicit casting syntax with the following form:
(type) exp
• Here “type” is the type that we would like the expression exp to have.
• This syntax may only be used to cast from one primitive type to another primitive type, or from one
reference type to another reference type.
• Examples:

© 2014 Goodrich, Tamassia, Goldwasser Java Primer 1 11


Implicit Casting
• There are cases where Java will perform an implicit cast
based upon the context of an expression.
• You can perform a widening cast between primitive types
(such as from an int to a double), without explicit use of
the casting operator.
• However, if attempting to do an implicit narrowing cast, a
compiler error results.

© 2014 Goodrich, Tamassia, Goldwasser Java Primer 1 12

You might also like