levo

Defining Data: Types, Variables and Classes

Type casting and conversion

  • 3 things to know
  • AS Unit 1
Start revising · £5/month

Overview

Type casting and lost in a narrowing conversion.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Why does Java convert an int to a double automatically, but refuse to convert a double to an int without a cast?

Key term

How does implicit widening conversion differ from explicit narrowing conversion?

Key term

What value is stored by double d = 7 / 2;, and why?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is type casting?

    Model answer

    Type casting is the conversion of a value from one data type to another so that the value can be stored in, or combined with, a variable of the second type.

  2. 2

    How does implicit widening conversion differ from explicit narrowing conversion?

    Model answer

    A widening conversion moves a value into a larger type and is performed automatically because nothing can be lost, whereas a narrowing conversion moves a value into a smaller type and must be written by the programmer because information may be lost.

  3. 3

    Why does Java convert an int to a double automatically, but refuse to convert a double to an int without a cast?

    Model answer

    Every int value fits exactly in a double, so the widening conversion is safe, whereas a double carries a fractional part and a far larger range, so converting to an int would silently discard information.

  4. 4

    What value does x hold after int x = (int) 9.99;, and why?

    Model answer

    The value of x is 9, because a cast from a real type to an integer type truncates towards zero and discards the fractional part rather than rounding.

  5. 5

    What value is stored by double d = 7 / 2;, and why?

    Model answer

    The value stored is 3.0, because both operands are integers so integer division is performed first, giving 3, and only then is that result widened to a double.

  6. 6

    What value is stored by double d = 7 / 2.0;, and why?

    Model answer

    The value stored is 3.5, because one operand is a double, so the integer 7 is promoted to a double and real division is carried out.

Other ways to work this

The same content, four other ways.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

Software and the Development Environment
Defining Data: Types, Variables and Classes
Program Control Structures
Objects, Classes and Data Structures

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

A null is the of one.
absencepresencetype

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Defining Data: Types, Variables and Classes, on a schedule.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.