0% found this document useful (0 votes)
8 views

Oop 3

Uploaded by

Jae
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Oop 3

Uploaded by

Jae
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

JAVA DATA TYPES

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 :

• i d e n tify a n d d e s cr i b e th e e i g h t p r i m itive d a ta ty p e s i n j ava ;

• e xp l a in th e ke y d i f fer en ces b e tw e en p r i m itive a n d r e f e r en ce


d a ta ty p e s i n j ava ;

• u n d e r s tan d th a t s tr i n g i s a r e f e r e nce ty p e i n j ava a n d


e xp l o r e b a sic s tr i n g o p eratio ns ;

• 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 ;

• r e co g n ize a n d h a n d l e p o ten tial ove r f low o r u n d e r f lo w


s i tu a tio ns w h e n p e r f o rmin g o p e ra tion s o n n u m e r ic d a ta
ty p e s .
JAVA
NUMBERS
Numbers

Primitive number types are divided into two groups:


• Integer types stores whole numbers, positive or negative
(such as 123 or -456), without decimals. Valid types are
byte, short, int and long. Which type you should use,
depends on the numeric value.
• Floating point types represents numbers with a fractional
part, containing one or more decimals. There are two types:
float and double.
Integer Types

By t e

Th e byte da ta typ e ca n s tor e w hol e nu mb er s f ro m - 128 to 12 7 . This ca n b e u se d in stea d


o f i nt o r other i nte ge r typ es t o s ave mem or y wh en yo u a re ce r tain th at the valu e will be
w i th in - 1 2 8 a n d 1 2 7 .

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 int da ta typ e ca n s to re w h ole nu mb er s f rom - 2 1 4 74 8 36 48 t o 2 14 74 83 6 47 . In


g en e ral, an d in o ur tu to rial, the i nt da ta typ e i s the p re fe rr ed da ta typ e whe n w e cre ate
va r i able s w i th a n u m e r ic va l ue .
Integer Types
Long

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“.

Floating Point Types

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.

Use float or double?

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.

Boolean values are mostly used for conditional testing.


Example using boolean variables.

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".

• A String in Java is actually a non -


primitive data type, because it refers
to an object. The String object has
methods that are used to per form
cer tain operations on strings
Java Type Casting

• Type casting is when you assign a value of one primitive data type to
another type.

• Widening Casting (automatically) - converting a smaller type to a


larger type size

byte -> short -> char -> int -> long -> f loat -> double

• Widening casting is done automatically when passing a smaller size


type to a larger size type
Java Type Casting

• Type casting is when you assign a value of one primitive data type to
another type.

• Narrowing Casting (manually) - converting a larger type to a


smaller size type

double -> f loat -> long -> int -> char -> short -> byte

Narrowing casting must be done manually by placing the type in


parentheses () in front of the value.
Java Operators
Operators are used to perform
operations on variables and values.
Java Assignment
Operators
Assignment operators are used to
assign values to variables.
Java Comparison
Operators
Comparison operators are used to compare two values (or variables). This is
important in programm ing, because it helps us to find answers and make decisions.

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 .

• 0 is the first position in a string, 1 is the second, 2 is the third ...


Java String
Concatenation
• The + operator can be used between
strings to combine them.
• You can also use the concat() method to
concatenate two strings
Java Numbers
and Strings
• Adding Numbers and Strings
• Java uses the + operator for both addition and concatenation.
• Numbers are added. Strings are concatenated.
Java Special
Characters
Strings - Special Characters
• Because strings must be written within quotes, Java will
misunderstand this string, and generate an error.
The solution to avoid this problem, is
to use the backslash escape character.

The backslash ( \) escape character


turns special characters into string
characters.
Java Math
The Java Math class has many methods that allows
you to perform mathematical tasks on numbers.
Java If ... Else
Java Conditions and If Statements

Yo u ca n u s e t h e s e co n d i t i o n s t o p e r form d i f fe ren t a ct i o n s fo r d i f fe ren t d e ci s i o n s .

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

• Not Equal to: a != b


Java Conditions and If Statements

Java has the following conditional statements:

• Use if to specify a block of code to be executed, if a specified


condition is true

• Use else to specify a block of code to be executed, if the same


condition is false

• Use else if to specify a new condition to test, if the f irst condition is


false

• Use switch to specify many alternative blocks of code to be executed


The if Statement
Use the if statement to specify a block of Java
code to be executed if a condition is true.
The else
Statement
Use the else statement to specify a block of code to be
executed if the condition is false.
The else if
Statement
Use the else if statement to specify a
new condition if the first condition is false.
Short Hand
if...else
• There is also a short-hand if else,
which is known as the ternary operator because it consists of three operands.
• It can be used to replace multiple lines of code with a single line,
and is most often used to replace simple if else statements
THANK YOU

REYCHELLE ANN
M. ANTONIO
INSTRUCTOR

You might also like