In this lesson, you will learn about variables and primitive data types. A variable is a name associated with a memory
location in the computer. Computer memory can store a value and that value can change or vary. The following video explains what a variable is and gives a couple of real word examples of variables. There are two types of variables in Java: primitive variables that hold primitive types and object variables that hold a reference to an object of a class. A reference is a way to find the object (like a UPS tracking number helps you find your package). The
primitive types presented in this chapter are: int - which store integers (numbers like 3, -76, 20393) double - which store floating point numbers (decimal numbers like 6.3 -0.9, and 60293.93032) boolean - which store Boolean values (either true or false). String is an object type and is the name of a class in Java. A string object has a sequence of characters enclosed
in a pair of double quotes - like “Hello”. You will learn more about Note Some languages use 0 to represent false and 1 to represent true, but Java uses the keywords A type is a set of values (a domain) and a set of operations on them. For example, you can do mathematical addition with ints and doubles but not with booleans and Strings.
1-3-2: What type should you use to represent the average grade for a course?
1-3-3: What type should you use to represent the number of people in a household?
1-3-4: What type should you use to hold the first name of a person?
1-3-5: What type should you use to record if it is raining or not?
1-3-6: What type should you use to represent the amount of money you have? 1-3-7: What type should you use for a shoe size like 8.5? 1-3-8: What type should you use for the number of tickets purchased? 1.3.3. Declaring Variables in Java¶A variable allows you to store a value in a named memory location. To create a variable, you must tell Java its data type and its name. Creating a variable is also called declaring a variable. The type is a keyword like int, double, or boolean, but you get to make up the name for the variable. When you create a primitive variable Java will set aside enough bits in memory for that primitive type and associate that memory location with the variable name that you used. To declare (create) a variable, you specify the type, leave at least one space, then the name for the variable and end the line with a semicolon ( Here is an example declaration of a variable called score that has type int. After declaring a variable, you can give it a value like below using an equals sign Or you can set an initial value for the variable in the variable declaration. Here is an example that shows declaring a variable and initializing it all in a single statement. The equal sign here Figure 1: Storing variables in memory¶ Note The equal sign Run the following code to see what is printed. Then, change the values and run it again. Click the When you are printing the value of a variable, never put double quotes Note Avoid putting a variable inside quotes 1-3-10: Click on all of the variable declarations in the following code.Variable declarations start with a type and then a name. public class Test2 { public static void main(String[] args) { int numLives; numLives = 0; System.out.println(numLives); double health; health = 8.5; System.out.println(health); boolean powerUp; powerUp = true; System.out.println(powerUp); } } 1-3-11: Click on all of the variable initializations (first time the variable is set to a value) in the following code.Variables are initialized using assignment name = value; Initialization occurs once per variable. public class Test2 { public static void main(String[] args) { int numLives; numLives = 0; System.out.println(numLives); double health = 8.5; System.out.println(health); boolean powerUp = true; System.out.println(powerUp); numLives = 5; System.out.println(numLives); powerUp = false; System.out.println(powerUp); } } 1-3-12: Click on all of the statements that both declare and initialize a variable in one statement.Variables are initialized using name = value; public class Test2 { public static void main(String[] args) { int numLives; numLives = 0; System.out.println(numLives); double health = 8.5; System.out.println(health); boolean powerUp = true; System.out.println(powerUp); } }Check Your Understanding - Mixed up Code Problems The following code declares and initializes variables for storing a number of visits, a person’s temperature, and if the person has insurance or not. It also includes extra blocks that are not needed in a correct solution. Drag the needed blocks from the left area into the correct order (declaring numVisits, temp, and hasInsurance in that order) in the right area. Check your solution. int numVisits = 5; --- Int numVisits = 5; #paired --- double temp = 101.2; --- Double temp = 101.2; #paired --- boolean hasInsurance = false; --- Boolean hasInsurance = false; #pairedCheck Your Understanding 1-3-14: Fill in the following: [blank] age = [blank]; to declare age to be an int and set its value to 5. 1-3-15: Fill in the following: Declare a double variable named gpa. 1-3-16: Fill in the following: Declare in int named studentCount and initialize it to 46. Follow the textbook style of using one space before and after the equal sign. 1-3-17: Fill in the following: Declare in boolean variable isRaining and initialize it to true. 1.3.4. String Concatenation¶You often need to print a message that mixes text with a variable value. You can use the string concatenation operator Run the following code to see what is printed. If you want spaces between words and variables when printing, you must put the space within the quoted string. For example, notice the space in the string “Hi ” in the last print statement. If you forget to add spaces, you will get smushed output like “HiJose” instead of “Hi Jose”.
1-3-19: Assume variable declaration Add a print statement to concatenate the string literal
“Favorite color is ” with the value stored in the Also note that the variable has to be on the left side of the This is an example of assignment dyslexia, when the coder has put the value on the left and the declaration on the right side. Try to fix the following code to compile and run. 1.3.5. Naming Variables¶While you can name your variable almost anything, there are some rules. A variable name should start with an alphabetic character (like a, b, c, etc.) and can include letters, numbers, and underscores You can’t use any of the
keywords or reserved words as variable names in Java ( The name of the variable should describe the data it holds. A name like The convention in Java and many programming languages is to always start a variable name with a lower case letter and then uppercase the first letter of each additional word. Variable names can not include spaces so uppercasing the first letter of each additional word
makes it easier to read the name. Uppercasing the first letter of each additional word is called camel case. Another option is to use underscore Note
Java is case sensitive so 1-3-23: What is the camel case variable name for a variable that represents a shoe size? 1-3-24: What is the camel case variable name for a variable that represents the top score? 1.3.6. Debugging Challenge : Weather Report¶Debug the following code. Can you find the all the bugs and get the code to run? 1.3.7. Summary¶
You have attempted of activities on this page Which is a named location in memory that is used to hold the value that may be modified by the program?A variable is a named memory location which temporarily stores data that can change while the program is running. A constant is a named memory location which temporarily stores data that remains the same throughout the execution of the program.
Is a named location in the memory that is used to hold the value?A buffer is a named location in the memory which stores data temporarily. A buffer is a memory location that is used to store data for a short period of time. It is used so that data can be retrieved much quicker and there is no delay when the data is being retrieved by the processor.
What is a name given to memory location?Memory locations that are used in the fashion are known as variables. Be careful not to confuse variables and identifiers. Identifiers are only the names given to variables, but variables are the actual memory locations used to store data.
Is a named memory location used to hold a value that Cannot be changed during the script execution?Constant is a named memory location used to hold a value that CANNOT be changed during the script execution. If a user tries to change a Constant Value, the Script execution ends up with an error. Constants are declared the same way the variables are declared.
|