Professional Documents
Culture Documents
Introduction To Java Datatypes and Control Constructs (1) As SIR
Introduction To Java Datatypes and Control Constructs (1) As SIR
Achintya Singhal
Primitive DATA Types
• The primitive types byte, short, int and long defined in the
Java language allow for the representation of discrete
integer values of widths 8, 16, 32, and 64 bits,
respectively.
• The primitive types float and double allow for the
representation of single and double precision floating-
point real values with representational widths of 32 and
64 bits, respectively.
• The primitive types char and boolean allow for 16-bit
multi-byte characters and false/true boolean values,
respectively.
Operators
• Arithmetic Operators
• The arithmetic operators in Java include the common
addition “+”, subtraction “-”, multiplication “*” and
division “/” operations.
– These operators apply to numeric operands, and return the type
of operands. When operands are mixed, the widest is used to
prevent unexpected truncation
• The “%” operator returns the remainder of integer
division.
• When used as a unary operator, “-” negates the numeric
operand.
Operators
• Logical Operators
• The logical operators in Java include the
standard and “&&”, or “||” and not “!”. Each
operator returns a boolean result
Operators
• Relational Operators
• The equality “==” and inequality “!=”
operators in Java operate on all values and
objects to return a boolean result.
• < less than
• > greater than
• <= less than or equal
• >= greater than or equal
Operators
• Bitwise Operators
• The following bitwise operators in Java operate on
corresponding bits of byte, short, int and long values.
• & bitwise “and”
• ^ bitwise exclusive “or”
• | bitwise inclusive “or”
• ~ bitwise one’s complement
• >> << right and left bitwise shift
• >>> right bitwise unsigned shift
Operators
• Assignment
• Having seen the basic means of providing new
values to variables, assignment “=” is more
correctly viewed as an operator rather than a
statement. In addition to assigning the right-side
value to the left-side variable, it also returns the
value assigned.
• a = b = 2;
• x op= f; is same as x = x op f;
Operators
Auto increment & Auto decrement (Prefix and Postfix)
int f, g; int f, g;
f = 6; f = 6;
g = f++; g = ++f;
// g has 6 // g has 7
// f has 7 // f has 7
int f, g; int f, g;
f = 6; f = 6;
g = f--; g = --f;
// g has 6 // g has 5
// f has 5 // f has 5
There is a subtle difference between x[i++] += 4 and x[i++] = x[i++] + 4, in that i++ is
evaluated once in the former but twice in the latter.
Operators
• Conditional Expression
– The conditional expression operator ? : returns one
of two values depending on the boolean condition.
For example, the expression A?B:C returns the value
of B if A is true, else the value of C is returned.
• Typecast
– The typecast operator (type)E for a numeric type
(such as int, float, or double), converts the value to
the specified type.
Operator Precedence
Operator Precedence
• Precedence allows the expression “a>>b+c” to be
unambiguously interpreted as “a>>(b+c)”, and not “(a>>b)
+c”. Similarly, “!m&&n” is interpreted as “(!m)&&n” and
not “!(m&&n)”.
• Associativity rules come into effect when equal precedence
levels do not help in resolving evaluation order. Due to
associativity rules (which is left-to-right for “/” and “*”, that
is, evaluating the left operator and then right), “a/b*c” is
interpreted as “(a/b)*c” and not “a/(b*c)”.
• Similarly, due to right-to-left associativity, “~y++” is
interpreted as “~(y++)” instead of “(~y)++”.
Object Instantiation
• A class construct provides a description for
objects of that class, and serves as a template
for objects to be created. However, no
instances of the class is crested, except by
calling the object allocator function new(). The
expression new Counter() returns a newly
created instance of the Counter class. However,
in order that this new object may be referred
to, it is assigned to an appropriate variable.
Representational Independence
• While accessing object attributes directly is permissible, it is
not ideal because it couples implementation code to the
current object representation.
• A common object-oriented programming practice is
information hiding—to make object representations
inaccessible to clients so that modifications in object
representations do not propagate excessively.
• Limiting access to object representations in Java is mainly
achieved by the two main constraint specifiers i.e. private and
public. The former limits access of the following entity to
within the class construct, while the latter makes it accessible
to any client code.
Overloading