Please enable JavaScript to use CodeHS

CodeHS Glossary


Declare a variable Java

Declaring a variable is defining it for the first time. --- The first time you define a variable, you need to give it a type and a name. ``` int numApples; ``` This line declares an `int` variable named numApples. This is telling the computer "I am hereby making a box called numApples and it will hold an integer value." --- Declaring a variable just creates the variable, it doesn't give it a value yet. For example, this creates a variable named cost that has no value yet. ``` double cost; ``` Later, you can initialize it by typing ``` cost = 2.50; ``` which sets the value of `cost` to be 2.50 --- This line declares **and** initializes a variable numOranges with a starting value of 4. ``` int numOranges = 4; ``` Declaring is different from updating the value of a variable. Here’s an example: ``` int points = 5; // declares the points variable and sets its value to 5 points = 10; // updates the value of the points variable to 10 ```