LoslegenKostenlos loslegen

Declare now, assign later

Sometimes you need to create a variable before you know what value it will hold. Java gives you this flexibility, which is particularly useful when the value depends on logic or calculations that come later in your code. Let's practice this pattern!

Diese Übung ist Teil des Kurses

Introduction to Java

Kurs anzeigen

Anleitung zur Übung

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

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

class DeclareThenAssign {
    public static void main(String[] args) {
    
    	// Declare a String variable called ingredients
        ____ ____;
        
        // Assign ingredients value
        ____ = "chocolate, eggs, milk, flour, sugar, vanilla";
        
        System.out.println(ingredients);
    }
}
Code bearbeiten und ausführen