Sie sind auf Seite 1von 9

CASTING INCOMPATIBLE DATA TYPES 1.

Byte,short,char promoted to int


Narrowing, explicit type conversion 2. One operand long output long
General form:(target-type) value 3. One operand float output float
Modulo the target type’s range 4. One operand double output double

f*b, b become float output in float, i/c c become int


output in i, d*s short converted to double the output
in double
result=float+int-double->float-double->double

ARRAY: (grouping related information)

Group of like-typed variables referred by a common


Modulo 256/256(byte’s range)=0 name
Floating point - truncate the fraction part 1D Arrays – list of like-typed variables
323/256 remainder=67 1. Array declaration: type var-name [];
2. Memory allocation using new -allocate
AUTOMATIC TYPE PROMATION IN EXPRESSSION memory for arrays and initialize all the
values to zero(int), false(Boolean),
null(reference type)
array-var =new type[size]
All arrays are dynamically allocated
Access or read specific element using index
Evaluating expression-automatically promote byte Output:
short, char to int

Cause confusing compile time errors:

Result is int type due to automatic conversion.so use


explicit cast

TYPE PROMOTION RULES


Index1 []-shows no of arrays or rows
Index 2[]-shows no of elements in each array or
columns

Array can be initialized when they are declared


With comma, curly braces (no need to use new)

April has 30 days.


Run time error occur when try to access negative
number or number >than the length of the array
Another example for 1D array

Two dimensional array:

Average is 2.42.
Multidimensional arrays:
It is implemented as arrays of arrays.
Declaration: int twoD= new int[][];
When allocate memory -no need to specify all the
dimensions in the initialization itself. define the first
(left most) dimension then allocate the remaining
dimensions separately
No need to allocate the same number of elements for
each dimension (length of each array is under our
control)
Irregular array not appropriate -run contrary to
expectation irregular array might be perfect -need
large 2D array that is sparsely populated(not all the
elements will be used)

Dimension’s initializer-within its own curly braces.


Use expressions as well as literal values inside the The following declarations are equivalent.
array initializer.

Equivalent declarations for defining several arrays at


the same time.

Local Variable Type Inference


Adv: streamline code,simplify declaration
Var (reserved type name)as Context -sensitive
Matrix where each element contains the product of identifier
row and column indexes.

Var as user-defined identifier

Type explicitly specified-int, var is the name of the


variable being declared.
In some places, var should not be used as class
name.
Use var to declare on arrays

the type is inferred from the type of initializer.


Hence no need to specify the array type by
mentioning [] with var. Operands must be of numeric type, char type
(subset of int) but not Boolean type.

Var can be used to declare a variable only when that


variable is initialized.
Var-only to declare local variables. not used in
instance variable, parameters or return types.
It can be used as the name of the other reference
types including interface, enumeration or
annotation, generic type parameter
Some var restriction
Only one variable can be declared at a time
Variable can not use null as an initializer.

For simple variable, var can be used for initialization


not for declaration. But for arrays, var can be used
for declaration only… not for initialization
String type
Used to declare string variables, array of strings,
quoted string constant
Not a primitive type Modulus operator:
%-returns remainder in division operation

Str-object of type String.

OPERATORS
Arithmetic operators
Bitwise operator
Relational operator
Boolean logical operator
Assignment operator
The ? operator Arithmetic compound Assignment operator
Adv: shorthand(less typing), more efficient

-For all arithmetic and binary operator


+ - compound assignment operator
var = var op expression; rewritten as
var op= expression;

a = a + 4; equal to a += 4;
a = a % 2; equal to a %= 2;

Integer types are represented by binary numbers, all


of the integer (except char) are signed integer
(includes + and – values )
for -ve values using 2’s complement (zero crossing
issues is solved)(encode and decode -invert bits and
add 1)
High order bit determines the sign of an integer.
a=6 b=8 c=3 Bitwise Logical operator
Increment and Decrement operator: OR,AND,EXOR NOT(Bitwise complement)
(++ and --)
x = x + 1; equal to x++;(increases its operand by 1)
x = x - 1; equal to x--;(decreses its operand by 1)

x = 42;
y = ++x; equal to x = x + 1;
y = x;
y=43 (increment occurs before x is assigned y)

x = 42;
y = x++; equal to y=x;
x=x+1;
y=42(increment occurs after x is assigned to y
in both cases x is set 43.

Exor- Wherever the second operand has a 0 bit, the


first operand is unchanged. when it is one, the first
Bitwise Operator operand is inverted
Applied to the interger types(byte, short,int, long,
char) The Left shift (as a quick way to multiply by 2)
Each shift is equal to multiplying by 2.
Left shift by n bits = multiply by 2 n * When a value has bits that are “shifted off,” those
Value << num bits are lost.
*num specifies the no of positions to shift. 00100011 =35
*For each shift left, the high-order bit is shifted out >>2
(and lost), and a zero is brought in on the right. 00001000 =8 (discards any reminder)
*Byte or short automatically promoted to int.so left * >> operator automatically fills the high-order bit
shift for byte or short result will be in int. Use casting with its previous contents each time a shift occurs(to
to have the result in byte. preserves the sign of the value)
11111000 = -8
>>1
11111100 = -4
Sign extension (preserve the sign of the negative
numbers): top(leftmost)bit exposed to right shift are
filled in with the previous contents of the top
bit(that is 1)
*if you shift –1 right, the result always remains –1,
since sign extension keeps bringing in more ones in
the high-order bits.
*Sign extended values not desirable during right
*negative byte or short value will be sign-extended
shift when convert byte to hexadecimal string so to
when it is promoted to int. Thus, the high-order bits
discard sign-extended bits shifted value is masked by
will be filled with 1’s. For these reasons, to perform a
AND it with 0x0f
left shift on a byte or short implies that you must
discard the high-order bytes of the int result.
for int operand bits are lost once they are shifted
past bit position 31(after 4th shift). If the operand is a
long, then bits are lost after bit position 63.

If you shift a 1 bit into the high-order position (bit 31


or 63), the value will become negative.
Output: b = 0xf1 is the output

The unsigned Right shift


>>> - unsigned shift right operator
Working with pixel based values and graphics (not
numeric values) no need for sign extension.
Unsigned shift -to shift a zero into the high-order bit
no matter what its initial value was.
With this >>> operator java shifts zeros into the high-
order bit.
int a = -1;
a = a >>> 24;
in binary form
11111111 11111111 11111111 11111111 –1 in
binary as an int
after being shifted left 4 bit positions, it would >>>24
produce –32. As you can see, when a 1 bit is shifted 00000000 00000000 00000000 11111111 255 in
into bit 31, the number is interpreted as negative. binary as an int
The Right Shift:(as a efficient integer division)
Each shift is divide by 2. For n bit =divide by 2 n. Meaningfull for 32-bit and 64-bit values
Value >>num (append zero bits to the left)
Int a=32;
a =a>>2; it contains 8 now
Relational operator:
>>> - nothing to do when dealing with bytes
Determine the relationship that one operand has to
The variable b is set to an arbitrary negative
the other eg equality and ordering
byte value for this demonstration. Then c is assigned
the byte value of b shifted right by four, which is 0xff
because of the expected sign extension. Then d is
assigned the byte value of b unsigned shifted right
by four, which you might
have expected to be 0x0f, but is actually 0xff
because of the sign extension that happened when b
was promoted to int before the shift. The last
expression sets e to the byte value of b masked to 8 Result is Boolean value, frequently used in if
bits using the AND operator, then shifted right by statement and the various loop statements.
four, which produces the expected value of 0x0f. Integer, floating point num, character and Boolean
Notice that the unsigned shift right operator was not can be compared using ==, !=
used for d, since the state of the sign bit after the Equality test == (=one equal sign means assignment
AND was known. operator) and inequality test !=.
Only numeric types( interger, floating point,
character operands)can be compared using ordering
operators (> or <….)
int a=4; int b=3
Bitwise Operator compound Assignments: Boolean c =a<b; //false is stored in c.
Which combines the assignments with bitwise In java, true and false are non numeric values that
operation do not relate to zero or nonzero.
To test zero and nonzero explicitly use one or more
a =a >> 4; equal to a >>= 4 of the relational operators.
a = a| b ; equal to a |= b Boolean Logical operators
operate on Boolean values result is Boolean.
if(c==1 & e++ < 100) d = 100;
Here, using a single & ensures that the increment
operation will be applied to e
whether c is equal to 1 or not.(causing run-time
exception when c=0)

The Assignment Operator:


var= expression;
Type of var is compatible with type of expression
To create a chain of Assignments (set a group of
variables to a common value)
int x,y,z;
X=y=z=100; // set x, y, z to 100
The ? operator (Ternary t)hree way:
Replacement for if-then-else statements
expression1 ? expression2 : expression3
any expression valuates to a Boolean value. If
expression1 is true, then expression2 is evaluated;
otherwise, expression3 is evaluated. The result of
the ? operation is that of the expression evaluated.
Both expression2 and expression3 are required to
return the same (or compatible)type, which can’t be
void.

ratio = denom == 0 ? 0 : num / denom


The result produced by the ? operator is then
assigned to ratio.

absolute value of 10 is 10
absolute value of -10 is 10
Operator Precedence
Short-Circuit Logical Operator (conditional
Operators in the same row are equal precedence
AND,conditional OR
&&,||
Not bother to evaluate the right-hand operand when
the outcome can be determined by left hand
operand alone. It is useful when right hand depends
left hand for proper operation
if (denom != 0 && num / denom > 10)
when denom is zero-no run-time exception because
of &&, when &- both sides evaluated causing run-
time exception when denom=0.
Boolean logic use short-circuit AND and OR.
Bitwise operation -& single character version.
Using Parentheses:
a >> b + 3 rewritten as a >> (b + 3)[ first adds 3 to b
and then shifts a right by that result]
(a >> b) + 3 [first shift a right by b positions and then
add 3 to that result]
a | 4 + c >> b & 7
(a | (((4 + c) >> b) & 7)) //easy to read
parentheses (redundant or not) do not degrade the
performance of your program. Adding parentheses
to reduce ambiguity does not negatively affect your
program.

Das könnte Ihnen auch gefallen