Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.

Similar presentations


Presentation on theme: "Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01."— Presentation transcript:

1 Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01

2 OutlineOutline ä Getting started with Visual J++ ä A simple program ä Using variables ä Declaring variables ä Initializing variables ä Changing the value of a variable ä Types ä Identifiers ä Expressions and the assignment operator ä Constants ä Getting started with Visual J++ ä A simple program ä Using variables ä Declaring variables ä Initializing variables ä Changing the value of a variable ä Types ä Identifiers ä Expressions and the assignment operator ä Constants

3 Getting Started With Visual J++ ä Let’s start up Visual J++ and write a simple program. ä ä ä Let’s start up Visual J++ and write a simple program. ä ä

4 Declaring Variables ä Every variable must be declared before it’s used. ä A variable declaration includes three parts: 1. The type of the variable. 2. The name of the variable. 3. A semicolon. ä (And possibly a fourth part – a comment) ä Examples:  int a; // Declare an integer variable named “a.”  int a, b, c; // Declare 3 integer variables – a, b, and c. ä ä ä Every variable must be declared before it’s used. ä A variable declaration includes three parts: 1. The type of the variable. 2. The name of the variable. 3. A semicolon. ä (And possibly a fourth part – a comment) ä Examples:  int a; // Declare an integer variable named “a.”  int a, b, c; // Declare 3 integer variables – a, b, and c. ä ä

5 Initializing Variables ä Next, we must initialize the variable – give it a value.  Example: a = 1; // Initialize the variable a to 1. ä Shortcut: Declare and initialize on the same line. ä Examples: ä int a = 1; // Declare a, and then initialize a to 1. ä int a = 1, b, c = 2; // Initialize a to 1, c to 2 (b uninitialized). ä ä ä Next, we must initialize the variable – give it a value.  Example: a = 1; // Initialize the variable a to 1. ä Shortcut: Declare and initialize on the same line. ä Examples: ä int a = 1; // Declare a, and then initialize a to 1. ä int a = 1, b, c = 2; // Initialize a to 1, c to 2 (b uninitialized). ä ä

6 Changing the Value of a Variable ä After a variable has been initialized, its value can be changed whenever you want. ä Example: ä a = 1; // The value of variable a is 1. ä a = 2; // Now, the value of variable a is 2. ä Don’t redeclare a variable: ä int a = 1; // Declare and initialize a. ä int a = 2; // This gives an error. ä ä ä After a variable has been initialized, its value can be changed whenever you want. ä Example: ä a = 1; // The value of variable a is 1. ä a = 2; // Now, the value of variable a is 2. ä Don’t redeclare a variable: ä int a = 1; // Declare and initialize a. ä int a = 2; // This gives an error. ä ä

7 TypesTypes ä Every variable must have a type. ä The type indicates what kind of data the variable can store. ä Java has many types, for example:  int – stores an integer number, e.g., 0, 72000, -3.  double – stores a floating point number, e.g., 0.0, 72.4, -3.14.  boolean – stores either true or false. ä All variables are declared the same way: ä double x = 1.5, y = -3.2; ä boolean z = true; ä Every variable must have a type. ä The type indicates what kind of data the variable can store. ä Java has many types, for example:  int – stores an integer number, e.g., 0, 72000, -3.  double – stores a floating point number, e.g., 0.0, 72.4, -3.14.  boolean – stores either true or false. ä All variables are declared the same way: ä double x = 1.5, y = -3.2; ä boolean z = true;

8 IdentifiersIdentifiers ä The name given to a variable is known as the variable’s identifier. ä Identifiers must follow a set of rules: 1. They may contain letters, digits, _, and $. 2. They may not begin with a digit. 3. Identifiers are case-sensitive – X and x are different identifiers (they represent different variables). 1. What’s wrong with these identifiers? 1. hi! 2. a<b 3. 12months ä The name given to a variable is known as the variable’s identifier. ä Identifiers must follow a set of rules: 1. They may contain letters, digits, _, and $. 2. They may not begin with a digit. 3. Identifiers are case-sensitive – X and x are different identifiers (they represent different variables). 1. What’s wrong with these identifiers? 1. hi! 2. a<b 3. 12months

9 IdentifiersIdentifiers ä Use descriptive names for identifiers. ä A variable that stores my age should be called “age” or “myAge,” not “a.” This makes the program easier to understand. ä Convention: Capitalize the first letter of all but the first word. ä Example: myAge (not myage, MyAge, or MYAGE). ä Keywords are words reserved by Java that can’t be used as identifiers. ä See page 45 of the textbook. ä Also null, true, and false. ä Use descriptive names for identifiers. ä A variable that stores my age should be called “age” or “myAge,” not “a.” This makes the program easier to understand. ä Convention: Capitalize the first letter of all but the first word. ä Example: myAge (not myage, MyAge, or MYAGE). ä Keywords are words reserved by Java that can’t be used as identifiers. ä See page 45 of the textbook. ä Also null, true, and false.

10 ExpressionsExpressions ä Expressions are composed of operators and operands. ä Operators: +, -, *, /, % (remainder), - (unary). ä Operands: A number or a variable. ä Trick questions: ä What’s the value of 1.0 / 2.0? ä What’s the value of 1 / 2? ä Precedence and associativity follow the rules of algebra. ä Use parentheses if you’re not sure, or for clarity. ä Expressions are composed of operators and operands. ä Operators: +, -, *, /, % (remainder), - (unary). ä Operands: A number or a variable. ä Trick questions: ä What’s the value of 1.0 / 2.0? ä What’s the value of 1 / 2? ä Precedence and associativity follow the rules of algebra. ä Use parentheses if you’re not sure, or for clarity.

11 The Assignment Operator ä The assignment operator (=) saves the result of an expression in a variable. ä Example: ä a = 2 + 2; ä b = a + 4; ä b = b + 1; ä What will the value of b be now? ä IMPORTANT: = is assignment, not equality. ä Shortcut operators: +=, -=, *=, /=, %=.  Example: a = a + 1 is the same as a += 1. ä ä ä The assignment operator (=) saves the result of an expression in a variable. ä Example: ä a = 2 + 2; ä b = a + 4; ä b = b + 1; ä What will the value of b be now? ä IMPORTANT: = is assignment, not equality. ä Shortcut operators: +=, -=, *=, /=, %=.  Example: a = a + 1 is the same as a += 1. ä ä

12 ConstantsConstants ä Turn a variable into a constant by writing “final.” ä Unlike variables, the value of a constant cannot be changed once it’s initialized. ä Example: ä final int myAge = 24; // Declare myAge to be a constant. ä myAge = 25; // This won’t work, since myAge is a constant. ä Convention: For a constant’s identifier, use all caps, and separate words with an underscore. ä Example: MY_AGE instead of myAge. ä ä ä Turn a variable into a constant by writing “final.” ä Unlike variables, the value of a constant cannot be changed once it’s initialized. ä Example: ä final int myAge = 24; // Declare myAge to be a constant. ä myAge = 25; // This won’t work, since myAge is a constant. ä Convention: For a constant’s identifier, use all caps, and separate words with an underscore. ä Example: MY_AGE instead of myAge. ä ä

13 ConstantsConstants ä Constants are never required, but they have several advantages: 1. Programs are easier to read (PI rather than 3.14159265359). 2. Programs are easier to modify. 3. Inconsistencies are less likely. ä Use constants! It may seem like more work, but it will save time in the long run. ä Constants are never required, but they have several advantages: 1. Programs are easier to read (PI rather than 3.14159265359). 2. Programs are easier to modify. 3. Inconsistencies are less likely. ä Use constants! It may seem like more work, but it will save time in the long run.

14 HomeworkHomework ä Read: 2.9, 2.10 (to bottom of p. 60), 3.9 (to top of p. 114), 2.12. ä Remember: Both parts of HW1 due Monday at the beginning of class. Email me with any questions. ä Read: 2.9, 2.10 (to bottom of p. 60), 3.9 (to top of p. 114), 2.12. ä Remember: Both parts of HW1 due Monday at the beginning of class. Email me with any questions.


Download ppt "Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01."

Similar presentations


Ads by Google