levo

Defining Data: Types, Variables and Classes

Type casting and conversion

  • 3 things to know
  • AS Unit 1

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.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

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

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A null is the of one.
absencepresencetype

Blank Blitz makes facts automatic

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

AA null means no value has been recorded.
BZero and an empty string are values.
CA null is the absence of a value.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

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.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

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