Oop 3
Oop 3
Learning Objectives
At th e e n d o f th e l e s s o n t h e s t u d e n t s s h o u l d b e a bl e to :
• e xp l a in h o w m e m o r y i s a l l oca te d f o r d i f fe re nt d a ta ty p e s a n d
th e i m p a ct o n p e r f o r ma nce ;
By t e
Short
Th e s h o r t d a ta ty p e ca n s to r e w h o l e n u m b e r s f r o m - 3 2 7 6 8 t o 3 2 7 6 7 .
In t
The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807 . This is used when int
is not large enough to store the value. Note that you should end the value with an "L“.
You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.
The float and double data types can store fractional numbers. Note that you should end the value with an "f" for floats and
"d" for doubles.
The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision
of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer
to use double for most calculations.
Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate the power of 10.
• Example that
demonstrates
using different
numeric data types
and performing
arithmetic
operations.
• This example
shows how to
declare various
numeric data
types, perform
simple arithmetic
operations, and
print the results.
JAVA BOOLEAN
DATA TYPES
Boolean Types
Very of ten in programming, you will need a data type that can only have
one of two values, like:
• YES / NO
• ON / OFF
• TRUE / FALSE
For this, Java has a boolean data type, which can only take the values
true or false.
This example
shows how to
declare
boolean
variables, use
logical
operators
(&&, ||, !),
and perform
conditional
checks to
determine
outcomes
based on
boolean
expressions.
Java Characters
Characters and Strings
The char data type is used to store • The String data type is used to store a
a single character. The character sequence of characters (text). String
values must be surrounded by double
must be surrounded by single
quotes.
quotes, like 'A' or 'c ’.
• The String type is so much used and
integrated in Java, that some call it
"the special ninth type".
• Type casting is when you assign a value of one primitive data type to
another type.
byte -> short -> char -> int -> long -> f loat -> double
• Type casting is when you assign a value of one primitive data type to
another type.
double -> f loat -> long -> int -> char -> short -> byte
The return value of a comparison is either true or false. These values are known as
Boolean values, and you will learn more about them in the Booleans and If..Else
chapter.
Java Logical Operators
You can also test for true or false values with logical operators.
Java Strings
Strings are used for storing text.
Java Strings
• String Length
• A S t r i ng i n J a va i s a c t u a l l y a n o b j e c t , w h i c h c o n t a i n m e t h o d s t h a t c a n p e r f o r m c e r t a i n
o p e ra t i o ns o n s t r i n g s .
• String Length
• A S t r i ng i n J a va i s a c t u a l l y a n o b j e c t , w h i c h c o n t a i n m e t h o d s t h a t c a n p e r f o r m c e r t a i n
o p e ra t i o ns o n s t r i n g s .
• F i n d i n g a C h a ra c t er i n a S t r i n g
• T h e i n d e x Of () m e t h o d r e t u rn s t h e i n d e x ( t h e p o s i t i o n) o f t h e f i r s t o c c u r r e n c e o f a s p e c i f i e d t e x t
i n a s t r i n g ( i n c l udi ng w h i te sp a c e )
• J a va c o u n t s p o s i t i o n s f r o m ze r o .
Yo u a l r e a d y k n o w t h a t Java s u p p o r ts t h e u s u a l l o g i ca l co n d i t i o n s f r o m m a t h e m a t i cs:
• Le s s t h a n : a < b
• Le s s t h a n o r e q u a l t o : a < = b
• G r e ater t h a n : a > b
• G r e ater t h a n o r e q u a l t o : a > = b
• Equal to a == b
REYCHELLE ANN
M. ANTONIO
INSTRUCTOR