0% found this document useful (0 votes)
17 views16 pages

Group 4 Type Conversion

The document discusses type conversion in programming, specifically in Java, highlighting its importance in converting data types either implicitly or explicitly. It explains the two types of conversion: implicit (widening) and explicit (narrowing), along with examples of built-in data types and their conversions. Additionally, it addresses the implications of impossible conversions and provides a practical example of type conversion in generating pseudo-random integers.

Uploaded by

gzcristian031706
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views16 pages

Group 4 Type Conversion

The document discusses type conversion in programming, specifically in Java, highlighting its importance in converting data types either implicitly or explicitly. It explains the two types of conversion: implicit (widening) and explicit (narrowing), along with examples of built-in data types and their conversions. Additionally, it addresses the implications of impossible conversions and provides a practical example of type conversion in generating pseudo-random integers.

Uploaded by

gzcristian031706
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

I. BASIC PROGRAMMING CONCEPTS

TYPE CONVERSION
TYPE CONVERSION
TYPE CONVERSION
PRESENTED BY: GROUP 4
GUZMAN, CRISTIANERY KHIM B. CATAN, JAMES MICHAEL E. REDULA, JAYMOEN M. OCSIANO, LESLIE KATE C.

BANOGON, ANTHONY JAMES PALASOL, LYN Y. ALEJADO, JHILES RYLL

INSTRUCTOR: EVELYN C. SILOT


CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

TYPE CONVERSION
Type Conversion is an essential aspect of
programming. It refers to the process of
converting one data type into another. This
can be done either implicitly or explicitly.

Type conversion (or typecasting) means


transfer of data from one data type to
another.
CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

DATA TYPE
a set of values and a set of operations on those values.

Commonly used built-in data types in Java:

string, for computing with sequence of characters, for input and output.
int, for computing with integers, for math calculations in programs
double, for computing with floating point numbers, typically for science
and math apps.
boolean, for computing with true and false, for decision making in
programs
CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

TYPE CHECKING
The Java compiler is your friend: it checks for type errors in your code.

When appropriate, we often convert a value from one type to another


to make types match.
CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

2 TYPES OF CONVERSION
Implicit Conversion (Widening Conversion)
Implicit conversion, also known as widening conversion, happens automatically when a smaller data type
is converted to a larger data type. This type of conversion is safe and does not result in data loss.

In this example, int is converted to long, and long is converted to double without any explicit cast.

Explicit Conversion (Narrowing Conversion)


Explicit conversion, or narrowing conversion, requires a cast because it involves converting a larger data
type to a smaller data type. This type of conversion can potentially lead to data loss.

Here, double is explicitly cast to long, and long is explicitly cast to int. The cast is necessary to inform the
compiler that you are aware of the potential data loss.
CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

2 TYPES OF CONVERSION
CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

TYPE CONVERSION WITH BUILT-IN TYPES


AUTOMATIC EXPRESSION TYPE VALUE
Convert number to string for “+” “ x : ” + 99 string “ x : 99”

Make numeric types match if no 11 * 0.25 double 2.75


loss to precision.
Home About Content Others

TYPE CONVERSION WITH BUILT-IN TYPES


FUNCTION CALL EXPRESSION TYPE VALUE
Math.round (2.7589) long “3”

Integer.parseInt(“123”) int “123”


CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

TYPE CONVERSION WITH BUILT-IN TYPES


CAST for values that
EXPRESSION TYPE VALUE
belong to multiple types.
(int) 2.71828 int “2”

Small integers can be short, int (int) Math.round(2.71828) int “3”


or long
CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

TYPE CONVERSION WITH BUILT-IN TYPES


CAST for values that
EXPRESSION TYPE VALUE
belong to multiple types.
Double values can be truncated to 11 * (int) 0.25 int “0”
int values

Pay attention to the type of your data.


CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

WHY DIFFERENT NUMERIC DATA TYPES


Tradeoff between memory use and range for integers.
Tradeoff between memory use and precision for floating points.

A conversion may be impossible.

Example: (short) 70000.


Short values must be between -2¹⁵ and 2¹⁵ - 1= 32676.
CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

WHY DIFFERENT NUMERIC DATA TYPES


REMEMBER:
CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

WHAT TO DO WITH AN IMPOSSIBLE


CONVERSION
APPROACH 1: Avoid doing it
in the first place.

APPROACH 2 (Java): Live with


a well-defined result.
It may crash. In a literal sense, the Ariane 5
rocket in 1996 blew up because of a type
conversion error. Basically, they had a cast that

APPROACH 3: CRASH. was impossible so it gave a bad value and


caused the rocket to crash.
CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

EXAMPLE OF TYPE CONVERSION PUT TO GOOD USE:


PSEUDO-RANDOM INTEGERS
It means that it generates a number sequence that exhibits the properties of a random number in terms
of distribution, but which is mathematically generated and deterministic in its output for any particular seed
value.

You can see this by creating a program that uses such a function to generate a short sequence, and
observing that each time you run it it generates the same sequence. This option surprises the
unsuspecting, but is also quite a useful property in testing code. When a less predictable sequence is
required, one normally initialised the PRNG with a seed value that is itself unpredictable, often derived from
the current system time, since it is not normally predictable when a program will be started.

System method Math.random() returns a pseudo-random double value in [0,1).


CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

EXAMPLE OF TYPE CONVERSION PUT TO GOOD USE:


PSEUDO-RANDOM INTEGERS
PROBLEM : Given N, generate a pseudo-random integer between 0 and N-1.
CSC 104 - COMPUTER PROGRAMMING II Home About Content Others

THANK YOU
THANK YOU
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy