Download presentation
Presentation is loading. Please wait.
Published byLionel Rose Modified over 6 years ago
1
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored in the computer is by declaring a variable. Now that you know what Java’s built-in types are, you need to learn how to use those types as variables.
2
Variables A variable is a name for a location in memory
A variable is just a name that we use to hold a value and whose value can change. In Java, before we can use a variable, we have to declare it. That means we need to tell Java what name we want to use and what type that name should have. When you declare a variable, the computer sets aside a space for it in the working memory of the computer, the RAM, while the program is running. Once the space is declared, the programmer can assign a value to the variable, change the value, or retrieve the value at any time in the program.
3
Declaring Variables A variable must be declared by specifying the variable's name and the type of information that it will hold data type variable name int total; We declare variables that with a statement that includes a type followed by one or more names we want to associate with that type. A variable must be declared by specifying the variable's name and the type of information that it will hold. The programmer choses the variable name following the rules for identifiers and avoiding using names that are keywords (the compiler will produce a syntax error if you try to use a keyword as a variable name). You can declare more than one variable at a time as long as they are of the same data type, just separate them with commas. You should also notice that the declarations always ends with a semi-colon. If you forget the semi-colon, BlueJ will give you a syntax error. int count, temp, result; Multiple variables can be created in one declaration
4
A variable can be given an initial value in the declaration
When a variable is referenced in a program, its current value is used. int sum = 0; int base = 32, max = 149; double avg = 3.25; char symbol = ‘b’; boolean playAgain = false; Once we’ve declared our variables, we can use them to store information—as long as the information is of the right type. For instance, if our data type is an int, then we must assign a whole number to the variable. If our data type is char, then we must assignment a single letter or character to the variable. If our data type is a Boolean, we would assign a value of true or false.
5
Assigning a Value An assignment statement changes the value of a variable The assignment operator is the = symbol (does not mean equality) The expression on the right is evaluated and the result is stored in the variable on the left The value that was in total is overwritten You can assign only a value to variables which is consistent with the variables declared type total = 55; We store information in a variable using Java’s assignment statement (a variable name) followed by the assignment operator (the value we want o assign to the variable). Java’s assignment operator is the equal sign. When we assign a value to a variable, we refer to this action as initializing. We can assign a value in the declaration or at a later time. After you declare a variable name, you can use it anywhere within the same set of curly brackets you declared it in. In programming, we call this the scope of a name. Java won’t compile a program that uses a variable name outside those brackets because that would be outside the name’s scope.
6
final int MIN_HEIGHT = 69;
Constants A constant is an identifier that is similar to a variable except that it holds one value while the program is active The compiler will issue an error if you try to change the value of a constant during execution In Java, we use the final modifier to declare a constant final int MIN_HEIGHT = 69; We’ve talk a little bit about constants alright. A constant in Java is a name whose value can’t change. We don’t use as many constants in program as we do variables. The main difference is declaring constant in a program is that we use the final keyword to tell Java that once the constants value is set, it can’t change.
7
Constants: give names to otherwise unclear literal values facilitate updates of values used throughout a program prevent inadvertent attempts to change a value
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.