0% found this document useful (0 votes)
13 views8 pages

Assignment 1 Slides Type Casting

The document provides an overview of type casting in Java, explaining both widening and narrowing casting techniques. It includes instructions for activities related to type casting, emphasizing that students can work in groups but must submit individual work by April 26, 2025. Additionally, it offers examples and real-life applications of type casting, encouraging students to write and submit their own programs as part of their assignments.

Uploaded by

mandla.sithole
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)
13 views8 pages

Assignment 1 Slides Type Casting

The document provides an overview of type casting in Java, explaining both widening and narrowing casting techniques. It includes instructions for activities related to type casting, emphasizing that students can work in groups but must submit individual work by April 26, 2025. Additionally, it offers examples and real-life applications of type casting, encouraging students to write and submit their own programs as part of their assignments.

Uploaded by

mandla.sithole
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/ 8

For your

Type Casting
Attention.
Before doing the activities About Type Casting Activities
today, watch the video on the
next side (Video also available Work through the following activities during
Saturday Session in Manzini – April 26, 2025.
in this module page).
You can work alone or in groups of 2 or three
Please watch the video again students as you study and do examples.
after doing all activities here.
However, every student submit his or her own work.

All activities should be completed by Saturday,


April 26, 2025, at 1500 Hours
Java
Type Casting
Type Casting
Type casting is when you Type casting is when you assign a value of
assign a value of one primitive one primitive data type to another type.
data type to another type.
In Java, there are two types of casting:
In Java, there are two types of
casting:  Widening Casting (automatically)

 Narrowing Casting (manually)


Java
Widening Casting
Type Casting
Widening casting is done Widening Casting (automatically)
automatically when passing a - converting a smaller type to a larger type
smaller size type to a larger size. Example:
size type:

byte -> short -> char -> int -> long ->

-> float -> double


Java
Widening Casting
Type Casting
Widening casting is done public class Main
{
automatically when passing a public static void main(String[] args)
smaller size type to a larger {
size type: int myInt = 9;
double myDouble = myInt; // int to double
System.out.println(myInt); // Outputs 9
Example 1 System.out.println(myDouble); // Outputs 9.0
}
}

Try it Yourself ! Write the program in Example 1


and submit it as part of Assignment 1.
Java
Narrowing Casting
Type Casting
Narrowing casting must be Narrowing Casting (manually) - converting a
done manually by placing the larger type to a smaller size type.
type in parentheses () in front Examples:
of the value:

double -> float -> long -> int ->

char -> short -> byte


Java
Narrowing Casting
Type Casting
public class Main
Narrowing casting must be { public static void main(String[] args)
done manually by placing the {
type in parentheses () in front double myDouble = 9.78d;
int myInt = (int) myDouble;
of the value: // Manual casting: double to int

Example 2 System.out.println(myDouble); // Outputs 9.78


System.out.println(myInt); // Outputs 9
}

}
Try it Yourself ! Write the program in Example 2
and submit it as part of Assignment 1.
Java
Real-Life Example
Type Casting
Here's a real-life example of
type casting where we create a Try it Yourself ! Write the program in Exercise 1
program to calculate the and submit it as part of Assignment 1.
percentage of a user's score in In this activity …
relation to the maximum score
in a game.
Exercise 1 We use type casting to make sure that the
result is a floating-point value, rather
than an integer:
Real-Life Example
// Set the maximum possible score in the game to 500
int maxScore = 500;

// The actual score of the user


int userScore = 423;

/* Calculate the percentage of the user's score in relation to the


maximum available score. Convert userScore to float to make sure
That the division is accurate */

float percentage = (float) userScore / maxScore * 100.0f;

System.out.println("User's percentage is " + percentage);

You might also like