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! 🍫🥚🥛
This exercise is part of the course
Introduction to Java
Exercise instructions
- Declare a
String
variable calledingredients
without assigning it any value. - Assign
ingredients
the value of"chocolate, eggs, milk, flour, sugar, vanilla"
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample 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);
}
}