Download Java Data Types

Document related concepts
no text concepts found
Transcript
1
2
Data types are very important in every
language for storing and manipulating
date. Java Provides eight simple types of
data which can be grouped into four
groups.
1. Integer
2. Floating Point Numbers
3. Characters
4. Boolean
3
Integer Types
Type
Bit Size
byte
8
short
16
int
32
long
64
Range
-27 to 27-1
-215 to 215-1
-231 to 231-1
-263 to 263-1
4
Floating Point Numbers
Type
Bit Size
float
32
double
64
Range
1.7e-308 to 1.7e+308
3.4e-038 to 3.4+038
5
Characters
Character data type is used to store
single UNICODE character. Unicode
character set is 16 bit character set. The
standard set of ASCII characters still
ranges from 0 to 255.
Type
Bit Size
Range
char
16
0 to -215-1
NOTE : char data type is unsigned type.
6
Boolean
Java has a simple type called boolean, for
logical values. It can have only one of two
possible values.
Type
Bit Size
Range
boolean
1
true or false
7
8
The variable is the basic unit of
storage in a Java program. A variable
is defined by the combination of a
type, variable name and an optional
initializer. In java all variables must be
declared before they can be used.
int a = 4;
Type
Initialize
value
Name
9
Some variable declarations examples: 1. int a = 3;
2. int x,y,z;
x = 4;
y = 6;
z = 3;
3. char c = ‘a’;
4. boolean b = true;
5. double d = 44.4, v = 43.3;
10
NOTE :
All the integer types are
considered int type by
default.
All the floating point types
are considered double by
default.
11
So if you want to store 4.4 in float
datatype you can not write this: float f = 4.4;
Instead you write
float f = 4.4f;
This way you explicitly tell the
compiler to treat 4.4 as float.
12
Identifier
Identifiers are used to name variables,
classes, interfaces, methods, and other
Java language elements.
An identifier is a sequence of characters
that starts with an underscore (_), a
dollar sign ($), or a letter (ASCII or
Unicode). Subsequent characters may
contain these characters plus the digits
0 through 9.
13
So followings are valid identifiers: _minute
$total
Sum4
Followings are invalid identifiers: &total
Minute#
4hour
14
Both uppercase and lowercase letters
can be used in an identifier. Because
java is case sensitive, therefore these
are treated as different identifiers: Total
total
TOTAL
toTal
15
Expressions
An Expressions can be thought of as a
programmatic equation. More formally,
“An expression is a sequence of
one or more operands and zero
or more operators that produce
a result. “
e.g.
X = y / 3;
A = 4 + 3 * 3/2;
16
17
abstract
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
extends
final
finally
float
for
goto
if
implements import
instanceof
int
interface
native
long
18
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
Keywords const and goto are reserved but not
used.
Words like true, false, null are not used as
variable name because they are used as literals.
19
20
Operators can be used to
combine or alter the program
values. Java contains a very rich
set of operators. There are three
types of operators: 1. Unary Operators
2. Binary Operators
3. Ternary Operators
21
Unary Operators are that operators which
require one operand to perform calculation.
-4;
5++;
Binary Operators are that operators which
require two operand to perform calculation.
4/2;
5>=7;
22
Ternary Operators are that operators
which require three operands to perform
calculation. There are only one ternary
operator in java.
?:
(5>3) ? Value1 : Value2
23
Java Unary Operators
++
--
+
!
-
~
( )
Java Binary Operators
Arithmetic Operators
*
/
%
>>
>>>
+
-
Shift Operators
<<
24
Comparison Operator
<
<=
instanceof
>
>=
==
!=
Bitwise Operators
&
^
|
Short Circuit Logical Operators
&&
||
25
Assignment Operators
=
*=
/=
%=
+=
-=
<<==
>>==
>>>==
&=
^=
|=
26
Increment and Decrement Operator
++
--
The ++ and – are java’s increment and
decrement operators. The increment
operator increases its operand by one;
the decrement operator decreases its
operand by one.
27
The expression
a = a + 1;
can written using increment operator as:
a++;
Similarly the statement
a = a – 1;
Is equal to following:
a--;
28
The operator a++ first assign the
value and then increment a by one.
These operators can be used as:
++a;
--a;
In this case the increment is
decrement is done before assignment.
29
Examples
Initial value
Final value Final value
Expression
of a
of b
of a
5
b = a++
5
6
5
b = ++a
6
6
5
b = a--
5
4
5
b = --a
4
4
30
Bitwise Inversion Operator
~
This operator performs bitwise
inversion on integral types. This
operator works by converting all
the 1 bits in a binary to 0s and all
the 0 to 1s.
31
For example binary representation :
01001101
Using the ~ operator convert into following
10110010
You may notice that all the 0 bits are
converted into 1s and all the 1 bits are
converted into 0s.
32
For a positive value the result is
always negative and increase the
value by one.
For example:
~15
~1128
~0
~8888888
returns
returns
returns
returns
-16
-1129
-1
-8888889
33
For a nagative value the result is
always positive and decrease the
value by one.
For example:
~-15
~-1128
~-1
~-88888
returns
returns
returns
returns
14
1127
0
88887
34
Boolean Complement Operator
!
The ! Operator inverts the value of
a boolean expression. So
!true results into false
!false results into true
35
Arithmetic Operators
*
/
%
+
-
Basic
arithmetic
operators
are
addition(+),
subtraction(-),
multiplication(*), and division(/). All
behave the same, as you would
expect for all numeric types.
Modulus(%) operator returns the
remainder of a division operation.
36
Comparison Operator
<
instanceof
<=
==
>
!=
>=
These are also called relational
operators.
They
determine
the
relationship that one operand has to
the other. The outcome of these
operators is always a boolean value.
37
Operator
Result
==
Equal to
!=
Not equal to
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
38
Bitwise Operator
&
^
|
The bitwise operators provides bitwise
AND, XOR and OR operations
respectively. These operators can be
apply to both integer types and
boolean types. These operators
compare each bit of first operand with
the corresponding bit of the second
39
operand and give results according to
following rules.
For AND operation 1 AND 1 results 1.
any other combination produces 0.
Op1
Op2
Op1 AND Op2
0
0
0
0
1
0
1
0
0
1
1
1
40
For OR operations, 0 OR 0 produces
0. any other combination produces 1.
Op1
Op2
Op1 OR Op2
0
0
0
0
1
1
1
0
1
1
1
1
41
For XOR operations, 1 XOR 0
produces 1, as 0 XOR 1 does, any
other combination produces 0.
Op1
Op2
Op1 XOR Op2
0
0
0
0
1
1
1
0
1
1
1
0
42
The &,^ and | behave in the same
way when applied to boolean.
However instead of calculating the
result on a bit-by-bit basis, the
boolean values are treated as
single
bits,
with
true
corresponding to 1 bit and false to
a 0 bit.
43
For AND operation true AND true
results true. any other combination
produces true.
Op1
Op2
Op1 AND Op2
false
false
false
false
true
false
true
false
false
true
true
true
44
For OR operations, false OR false
produces
false.
any
other
combination produces true.
Op1
Op2
Op1 OR Op2
false
false
false
false
true
true
true
false
true
true
true
true
45
For XOR operations, true XOR false
produces true, as false XOR true
does, any other combination produces
false.
Op1
Op2
Op1 XOR Op2
false
false
false
false
true
true
true
false
true
true
true
false
46
Short Circuit Logical Operators
&&
||
The short circuit logical operators &&
and || provide logical AND and OR
operations on boolean types similar to
the & and | operators.
However they have a valuable
additional feature.
47
The ability to “short circuit” a
calculation if the result is definitely
known.
1.For an AND operation, if first
operand is false, the result is false
without checking the other operand.
2.For an OR operation, if first operand
is true, the result is true, without
checking the other operand.
48
Example
int a = 5;
boolean b = ( (a>8) && (a==5) );
The first expression (a>8) returns
false so the second expression
(a==5) never executes and false is
stored in b.
49
Example
int a = 5;
boolean b = ( (a>3) | | (a==2) );
The first expression (a>3) returns
true so the second expression
(a==5) never executes and true is
stored in b.
50
Implicit Type Casting
When one type of data is assigned
to the other type of variable, an
automatic type conversion occurs if
left hand type is larger than the
right hand type. This conversion is
sometimes
called
widening
conversion since you are convert
small value to large value.
51
byte a = 5;
int b = a;
long c = b;
double d = c;
All the above are implicit type
casting examples. The right hand
side is smaller than the left hand
side.
52
Explicit Type Casting
When you want to assign a larger
value to a smaller value, you can
not do it without explicitly casting it
using the cast operator. This
conversion is sometimes called
narrowing conversion since you are
explicitly
making
the
value
narrower to fit into the small size.
53
The syntax for explicit casting is:
target variable = (target type) value;
NOTE:
When you explicitly cast large value
to small value some information may
be lost. For example, when floating
point value is assigned to an integer
the fractional part is lost.
54
int a = 5;
byte b = a;
The second line will cause error.
You can cast the second line like
this using cast operator.
byte b = (byte) a;
55
56
In Java, strings are handled by a
special class called String. Even
literal strings are managed internally
by an instantiation of a String class.
This method of handling strings is
very different from languages like C
and C++, where strings are
represented simply as an array of
characters.
57
Java does not have a string primitive
type, each use of a string literal causes
an object of the String class to be
created behind the scenes.
Following are a few strings declarations.
String message;
message = “Hello This is Java“;
String name = “Mr. Habib";
58