Sei sulla pagina 1di 17

Lecture 2: Java Basics

…Continue from last week


Statement blocks
 The body of a method is a statement block;
 A sequence of statements and declarations
 placed between the braces "{" and "}".
 Method bodies and other statement blocks can
themselves have statement blocks nested inside
of them.
 Statement blocks can perform some action, like
calling the method of some object,
Local variables
 Statement block can contain declarations of local variables
 A local variable can either be a base type (such as int,
float, double) or a reference to an instance of some class.
 There are two ways of declaring local variables:

type name;
type name = initial_value;

 The first declaration simply defines the identifier, name, to


be of the specified type.
 The second declaration defines the identifier, its type, and
also initializes this variable to the specified value
Expressions
 Expressions involve the use of literals, variables, and
operators.
 Variables and constants are used in expressions to
define new values and to modify variables;
 For example;

double product = num1 * num2;

 The above is an expression


Literals
 A literal is any "constant" value that can be used in an
assignment or other expression.
 Java allows the following kinds of literals:
 The null object reference
 Boolean: true and false
 Floating Point
 Character
 String Lateral: is a sequence of characters enclosed
in double quotes, for example, the following is a string
literal:

 "dogs cannot climb trees"


Operators
 Java expressions involve composing literals and variables
with operators. We survey the operators in Java in this
section.
 Assignment Operators (=) used to assign a value to an
instance variable or local variable. Its syntax is as follows:

variable = expression
Num1 = 34;

 Arithmetic Operators  used for addition, subtraction,


multiplication and division, and the modulo operation.
 + Addition, - subtraction, * Multiplication, / division, %
modulo operation
Operators
 Increment Operators (++) provides plus-one
(++) increment
 Decrement Operator (--)  Provides minus-one
decrement
 If 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.
Operators
 Logical Operators the standard comparison
operators between numbers

 < Less than


 <= Less than or equals to
 == equal to
 != not equal to
 > Greater than
 >= greater than or equal to
Operators
 Bitwise Operators Bitwise operators are used to
perform manipulation of individual bits of a number.
 They can be used with any of the integral types (char,
short, int, etc).
 They are used when performing update and query
operations of Binary indexed tree;

 _ bitwise complement (prefix unary operator)


 & bitwise and
 | bitwise or
 ^ bitwise exclusive-or
 << shift bits left, filling in with zeros
 >> shift bits right, filling in with sign bit
 >>> shift bits right, filling in with zeros
Operators
 Operational Assignment Operators Besides the standard
assignment operator (=), Java also provides a number of
other assignment operators that have operational side
effects
 These other kinds of operators are of the following form:

variable op= expression,


Variable = variable op expression
where opp is any binary operator

 Example:

int += 2
String Concatenation
 Strings can be composed using the
concatenation operator (+), so that the code

String rug = "carpet";


String dog = "spot";
String mess = rug + dog;
String answer = mess + " will cost me " + 5 + " dollars!";
Casting and Auto boxing
 Casting is an operation that allows us to change
the type of a variable.
 take a variable of one type and cast it into an
equivalent variable of another type.
 Casting can be useful for doing certain numerical
and input/output operations.
 The syntax for casting an expression to a desired
type is as follows:

(type) exp
Ordinary Casting
 When casting from a double to an int, we may lose
precision.
 This means that the resulting double value will be
rounded down.
 But we can cast an into a double without this worry.
 For example, consider the following:

doubled1 = 3.2;
doubled2 = 3.9999;

Int i1 = (int)d1; // i1 has value 3


Int i2 = (int)d2; // i2 has value 3
double d3 = (double)i2; // d3 has value 3.0
Casting with Operators
 Certain binary operators, like division, will have
different results depending on the variable types they
are used with.
 We must take care to make sure operations perform
their computations on values of the intended type
 When used with integers, division does not keep track
of the fractional part, for example.
 When used with doubles, division keeps this part, as
is illustrated in the following example:

inti1 = 3;
inti2 = 6;
dresult = (double)i1 / (double)i2; // dresult has value 0.5
dresult = i1 / i2; // dresult has value 0.0
Implicit Casting and Auto
boxing/Unboxing
 There are cases where Java will perform an implicit cast,
according to the type of the assignment variable, provided
there is no loss of precision.
 For example:

int iresult, i = 3;
double dresult, d = 3.2;

dresult = i / d; // dresult is 0.9375. i was cast to a double


iresult = i / d; // loss of precision −> this is a compilation error
iresult = (int) i / d; // iresult is 0, since the fractional part is lost

 since Java will not perform implicit casts where precision is


lost, the explicit cast in the last line above is required
Autoboxing/Unboxing
 A new kind of implicit cast, for going between Number
objects
 Any time a Number object is expected as a parameter
to a method, the corresponding base type can be
passed.
 In this case, Java will perform an implicit cast, called
autoboxing, which will convert the base type to its
corresponding Number object.
 Likewise, any time a base type is expected in an
expression involving a Number reference, that
Number object is changed to the corresponding base
type, in an operation called unboxing.
Autoboxing/Unboxing
 There are few caveats regarding autoboxing and
unboxing;
1. Number reference is null, it will generate an error,
called NullPointerException.
2. when testing for equality (==), try to avoid the implicit
casts done by autoboxing and unboxing.
3. implicit casts, of any kind, take time
 Incidentally, there is one situation in Java when only
implicit casting is allowed, and that is in string
concatenation.
 To perform a conversion to a string, we must instead
use the appropriate toString method or perform an
implicit cast via the concatenation operation

Potrebbero piacerti anche