Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables in Java x = 3;. What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of.

Similar presentations


Presentation on theme: "Variables in Java x = 3;. What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of."— Presentation transcript:

1 Variables in Java x = 3;

2 What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of time.  Similar to math class, variables hold values and can be involved in operations.  For example, a numeric variable can be multiplied by 2. Or a text variable might be joined with another text variable.

3 Properties of Variables  Variables must have a unique identifier (name).  The name must be alphabetic and not contain any spaces.  A variable can only contain one value at a time. This value can be overwritten as many times as you want.  Variables also have a specific data type. The data type indicates what kind of data the variable can hold.  Behind the scenes, the data type also indicates how much memory is required to store the data.

4 Data Types  We will be working with five primary data types: TypeMeaningExample Values int Integer in the range -2,147,483,648 to 2,147,483,647. 4, -6, 0, 10000000 doubleA decimal number.-0.5643, 3.145829, 100000.0 char A single character. This can be a letter, a number, a space, or special characters like punctuation. char values are surrounded by single quotations. ‘a’, ‘1’, ‘.’ boolean A type that can only hold the value true or false. true, false StringStrings are used to hold text such as sentences, serial numbers, etc. “Bob” “Holy cow I love milk!” “My phone number is 911!”

5 Declaring a Variable To create a variable, we must declare it as follows: ; The following are all valid variable declarations. int age; // Creates an int variable called age. String name; // Creates a String variable called name. double PI; // Creates a double variable called PI. None of the above variables hold any values! They simply represents boxes of memory that been reserved and named.

6 Assigning a Value to a Variable To assign values to a variable, simply apply algebra! After a variable has been declared, use the following to assign values: = ; Note that the type of the variable must match the type of the value. We cannot assign a String value to an int variable!

7 Assigning a Value to a Variable - Example int age; // Creates an int variable called age. String name; // Creates a String variable called name. double PI; // Creates a double variable called PI. age = 16; name = “Mitchell”; PI = 3.14; Note that only String values are contained in quotations.

8 Printing the Value of a Variable To print the value of a variable to the screen, use the System.out.print() statement using the variable’s name as an argument. age = 5; System.out.println(age);// Prints 5. You can also print both text and variables to the screen using the “+” symbol. age = 5; System.out.println(“My age is “ + age);


Download ppt "Variables in Java x = 3;. What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of."

Similar presentations


Ads by Google