CommencerCommencer gratuitement

Declare now, assign later

In Java, variables can be declared first and assigned a value later, giving your code flexibility. In this exercise, you'll declare a String variable and then assign it a list of ingredients. Let's get cooking! 🍫🥚🥛

Cet exercice fait partie du cours

Introduction to Java

Afficher le cours

Instructions

  • Declare a String variable called ingredients without assigning it any value.
  • Assign ingredients the value of "chocolate, eggs, milk, flour, sugar, vanilla".

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

class DeclareThenAssign {
    public static void main(String[] args) {
    
    	// Declare String variable called ingredients
        ____ ____;
        
        // Assign ingredients value
        ____ = "chocolate, eggs, milk, flour, sugar, vanilla";
        
        System.out.println(ingredients);
    }
}
Modifier et exécuter le code