1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
A variable is a name associated with a memory location in the computer, where you can store a value that can change or vary.
primitive variables:
int-number
double-decimals
boolean-(small)true or false
There are two types of variables in Java: primitive variables that hold primitive types and object or reference 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).
String is one of the object types on the exam and is the name of a class in Java. A String is written in a Java program as a sequence of characters enclosed in a pair of double quotes - like "Hello". You will learn more about String objects in Unit 2.
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 name that you used.
Computers store all values using bits (binary digits). A bit can represent two values and we usually say that the value of a bit is either 0 or 1. When you declare a variable, you have to tell Java the type of the variable because Java needs to know how many bits to use and how to represent the value. The 3 different primitive types all require different number of bits. An integer gets 32 bits of memory, a double gets 64 bits of memory and a boolean could be represented by just one bit.
here is an example declaration of a variable called score.
int score;
After declaring a variable, you can give it a value like below using an equals sign =
followed by the value.
int score; score = 4;
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.
int score = 4;
When you are printing out variables, you can use the string concatenation operator +
to add them to another string inside System.out.print. If you forget to add spaces, you will get smushed output like “HiJose” instead of “Hi Jose”.
The keyword final can be used in front of a variable declaration to make it a constant that cannot be changed. Constants are traditionally capitalized.
**final** double PI = 3.14
A variable name should start with an alphabetic character (like a, b, c, etc.) and can include letters, numbers, and underscores _. It must be all one word with no spaces.You can’t use any of the keywords or reserved words as variable names in Java (for, if, class, static, int, double, etc).
Use meaningful variable names!
Start variable names with a lower case letter and use camelCase.
Variable names are case-sensitive and spelling sensitive! Each use of the variable in the code must match the variable name in the declaration exactly.
Never put variables inside quotes (” “).
A variable is a name for a memory location where you can store a value that can change or vary.
A variable can be declared and initialized with the following code:
int score; double gpa = 3.5;
Data types can be primitive types (like int) or reference types (like String).
The three primitive data types used in this course are int (integer numbers), double (decimal numbers), and boolean (true or false).
Each variable has associated memory that is used to hold its value.
The memory associated with a variable of a primitive type holds an actual primitive value.
When a variable is declared final, its value cannot be changed once it is initialized.