Skip to main content
Logo image

Section 4.5 Initializing Variables

It is very important to always initialize your variables (that is, give initial values to your variables, even if you plan to change those values immediately). In the following video we discuss two ways to do so.
  • Separate declaration and initialization:
    int cash; 
    cash = 20;
    
  • Declaration and initialization in one statement:
    int cash = 20;
    

If you cannot see this codecast, please click here.

Check Your Understanding Check Your Understanding

1.

Which of the following variable declaration and initialization statements is equivalent to:
int money;
money = 20;
  • int money = 20;
  • Great job!
  • money = int 20;
  • Not quite - try again!
  • int 20;
  • Not quite - try again!
  • int money 20;
  • Not quite - try again!
Hint.
Scroll up to the top of this page for the correct syntax.

2.

Find an equivalent variable declaration and initialization statement to:
int apples;
apples = 4;
  • int apples = 4;
  • Great job!
  • apples = 4 int;
  • Not quite - try again!
  • int 4;
  • Not quite - try again!
  • nt apples 4;
  • Not quite - try again!
Hint.
Scroll up to the top of this page for the correct syntax.