Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in

Sequence

Why int x = 10.5 is rejected

Core exercise

In the file, the line int x = 10.5; is commented out. Uncomment it and run: read the compiler's message. Then fix it in two ways: valeurExacte() must return 10.5 without losing anything, and valeurTronquee() must return 10 by explicitly requesting the conversion.

A workshop goes here: you write and run code in it. It requires JavaScript.

Starting code

Exercices.java

public class Exercices {

    public static void main(String[] args) {
        // Decommentez la ligne suivante et lancez pour lire l'erreur :
        // int x = 10.5;

        System.out.println(valeurExacte());
        System.out.println(valeurTronquee());
    }

    public static double valeurExacte() {
        // TODO : renvoyer 10.5 sans perte
        return 0;
    }

    public static int valeurTronquee() {
        // TODO : renvoyer 10.5 converti en int
        return 0;
    }
}

The core exercise is validated in the workshop, which requires JavaScript.

Hint 1

10.5 is a double. Java refuses to store it in an int on its own, because information would be lost: it requires you to ask for it with (int).