Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSindya Darman
1,141 PointsWhat is the difference between declaring a variable and initializing a variable?
any simple way to understand it?
3 Answers
Jon Kussmann
Courses Plus Student 7,254 PointsHi Sindya,
When you declare a variable, you give it a name (name/age) and a type (String/int):
String name;
int age;
Initializing a variable is when you give it a value.
int favoriteNumber = 3;
Kathryn Ann
10,071 PointsHi Sindya,
Declaring a variable reserves a name and some space in memory for a variable of the specified type, but doesn't give it a value. Initializing gives the variable a value. Depending on how you're going to use the variable, you can declare it without initializing it and then initialize it later, or declare and initialize at the same time. For example:
int value1; // declares integer variable
value1 = 6; // initializes integer variable that's already been declared
int value2 = 5; // declares and initializes integer variable
Sindya Darman
1,141 PointsThank you Kathryn
Hans Eisenman
1,315 PointsThanks for these answers. This was helpful for me. the C# Basics course is great but somehow kind of breezed over this point of declaring being the step where you're just reserving the name and memory. That note here helped me a lot.
Sindya Darman
1,141 PointsSindya Darman
1,141 PointsThank you Jon