lecture 2: variables and expressions yoni fridman 6/29/01 6/29/01

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

Post on 21-Dec-2015

220 views

Category:

Documents


4 download

TRANSCRIPT

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

Lecture 2: Variables and Lecture 2: Variables and ExpressionsExpressions

Lecture 2: Variables and Lecture 2: Variables and ExpressionsExpressions

Yoni FridmanYoni Fridman

6/29/016/29/01

Yoni FridmanYoni Fridman

6/29/016/29/01

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

OutlineOutlineOutlineOutline

Getting started with Visual J++Getting started with Visual J++ A simple programA simple program

Using variablesUsing variables Declaring variablesDeclaring variables Initializing variablesInitializing variables Changing the value of a variableChanging the value of a variable

TypesTypes IdentifiersIdentifiers Expressions and the assignment operatorExpressions and the assignment operator ConstantsConstants

Getting started with Visual J++Getting started with Visual J++ A simple programA simple program

Using variablesUsing variables Declaring variablesDeclaring variables Initializing variablesInitializing variables Changing the value of a variableChanging the value of a variable

TypesTypes IdentifiersIdentifiers Expressions and the assignment operatorExpressions and the assignment operator ConstantsConstants

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

Getting Started With Visual J++Getting Started With Visual J++Getting Started With Visual J++Getting Started With Visual J++

Let’s start up Visual J++ and write a simple Let’s start up Visual J++ and write a simple program.program.

<Code Example><Code Example>

Let’s start up Visual J++ and write a simple Let’s start up Visual J++ and write a simple program.program.

<Code Example><Code Example>

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

Declaring VariablesDeclaring VariablesDeclaring VariablesDeclaring Variables

Every variable must be Every variable must be declareddeclared before it’s used. before it’s used. A variable declaration includes three parts:A variable declaration includes three parts:

The type of the variable.The type of the variable. The name of the variable.The name of the variable. A semicolon.A semicolon. (And possibly a fourth part – a comment)(And possibly a fourth part – a comment)

Examples:Examples: int a; // Declare an integer variable named “a.”int a; // Declare an integer variable named “a.” int a, b, c; // Declare 3 integer variables int a, b, c; // Declare 3 integer variables – – a, b, and c.a, b, and c.

<Code Example><Code Example>

Every variable must be Every variable must be declareddeclared before it’s used. before it’s used. A variable declaration includes three parts:A variable declaration includes three parts:

The type of the variable.The type of the variable. The name of the variable.The name of the variable. A semicolon.A semicolon. (And possibly a fourth part – a comment)(And possibly a fourth part – a comment)

Examples:Examples: int a; // Declare an integer variable named “a.”int a; // Declare an integer variable named “a.” int a, b, c; // Declare 3 integer variables int a, b, c; // Declare 3 integer variables – – a, b, and c.a, b, and c.

<Code Example><Code Example>

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

Initializing VariablesInitializing VariablesInitializing VariablesInitializing Variables

Next, we must Next, we must initializeinitialize the variable – give it a the variable – give it a value.value. Example: Example: a = 1; // Initialize the variable a to 1.a = 1; // Initialize the variable a to 1.

Shortcut: Declare and initialize on the same line.Shortcut: Declare and initialize on the same line. Examples:Examples:

int a = 1; // Declare a, and then initialize a to 1.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).int a = 1, b, c = 2; // Initialize a to 1, c to 2 (b uninitialized).

<Code Example><Code Example>

Next, we must Next, we must initializeinitialize the variable – give it a the variable – give it a value.value. Example: Example: a = 1; // Initialize the variable a to 1.a = 1; // Initialize the variable a to 1.

Shortcut: Declare and initialize on the same line.Shortcut: Declare and initialize on the same line. Examples:Examples:

int a = 1; // Declare a, and then initialize a to 1.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).int a = 1, b, c = 2; // Initialize a to 1, c to 2 (b uninitialized).

<Code Example><Code Example>

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

Changing the Value of a VariableChanging the Value of a VariableChanging the Value of a VariableChanging the Value of a Variable

After a variable has been initialized, its value can After a variable has been initialized, its value can be changed whenever you want.be changed whenever you want.

Example:Example: a = 1; // The value of variable a is 1.a = 1; // The value of variable a is 1. a = 2; // Now, the value of variable a is 2.a = 2; // Now, the value of variable a is 2.

Don’t redeclare a variable:Don’t redeclare a variable: int a = 1; // Declare and initialize a.int a = 1; // Declare and initialize a. int a = 2; // This gives an error.int a = 2; // This gives an error.

<Code Example><Code Example>

After a variable has been initialized, its value can After a variable has been initialized, its value can be changed whenever you want.be changed whenever you want.

Example:Example: a = 1; // The value of variable a is 1.a = 1; // The value of variable a is 1. a = 2; // Now, the value of variable a is 2.a = 2; // Now, the value of variable a is 2.

Don’t redeclare a variable:Don’t redeclare a variable: int a = 1; // Declare and initialize a.int a = 1; // Declare and initialize a. int a = 2; // This gives an error.int a = 2; // This gives an error.

<Code Example><Code Example>

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

TypesTypesTypesTypes

Every variable must have a Every variable must have a typetype.. The type indicates what kind of data the variable The type indicates what kind of data the variable

can store.can store. Java has many types, for example:Java has many types, for example:

intint – stores an – stores an integerinteger number, e.g., 0, 72000, -3. number, e.g., 0, 72000, -3. doubledouble – stores a – stores a floating pointfloating point number, e.g., 0.0, 72.4, -3.14. number, e.g., 0.0, 72.4, -3.14. booleanboolean – stores either – stores either truetrue or or falsefalse..

All variables are declared the same way:All variables are declared the same way: double x = 1.5, y = -3.2;double x = 1.5, y = -3.2; boolean z = true;boolean z = true;

Every variable must have a Every variable must have a typetype.. The type indicates what kind of data the variable The type indicates what kind of data the variable

can store.can store. Java has many types, for example:Java has many types, for example:

intint – stores an – stores an integerinteger number, e.g., 0, 72000, -3. number, e.g., 0, 72000, -3. doubledouble – stores a – stores a floating pointfloating point number, e.g., 0.0, 72.4, -3.14. number, e.g., 0.0, 72.4, -3.14. booleanboolean – stores either – stores either truetrue or or falsefalse..

All variables are declared the same way:All variables are declared the same way: double x = 1.5, y = -3.2;double x = 1.5, y = -3.2; boolean z = true;boolean z = true;

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

IdentifiersIdentifiersIdentifiersIdentifiers

The name given to a variable is known as the The name given to a variable is known as the variable’s variable’s identifieridentifier..

Identifiers must follow a set of rules:Identifiers must follow a set of rules:1.1. They may contain letters, digits, _, and $.They may contain letters, digits, _, and $.

2.2. They may not begin with a digit.They may not begin with a digit.

3.3. Identifiers are case-sensitive – X and x are different identifiers Identifiers are case-sensitive – X and x are different identifiers (they represent different variables).(they represent different variables).

1.1. What’s wrong with these identifiers?What’s wrong with these identifiers?1.1. hi!hi!

2.2. a<ba<b

3.3. 12months12months

The name given to a variable is known as the The name given to a variable is known as the variable’s variable’s identifieridentifier..

Identifiers must follow a set of rules:Identifiers must follow a set of rules:1.1. They may contain letters, digits, _, and $.They may contain letters, digits, _, and $.

2.2. They may not begin with a digit.They may not begin with a digit.

3.3. Identifiers are case-sensitive – X and x are different identifiers Identifiers are case-sensitive – X and x are different identifiers (they represent different variables).(they represent different variables).

1.1. What’s wrong with these identifiers?What’s wrong with these identifiers?1.1. hi!hi!

2.2. a<ba<b

3.3. 12months12months

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

IdentifiersIdentifiersIdentifiersIdentifiers

Use descriptive names for identifiers.Use descriptive names for identifiers. A variable that stores my age should be called “age” or “myAge,” A variable that stores my age should be called “age” or “myAge,”

not “a.” This makes the program easier to understand.not “a.” This makes the program easier to understand.

Convention: Capitalize the first letter of all but Convention: Capitalize the first letter of all but the first word.the first word. Example: myAge (not myage, MyAge, or MYAGE).Example: myAge (not myage, MyAge, or MYAGE).

KeywordsKeywords are words reserved by Java that can’t be are words reserved by Java that can’t be used as identifiers.used as identifiers. See page 45 of the textbook.See page 45 of the textbook. Also null, true, and false.Also null, true, and false.

Use descriptive names for identifiers.Use descriptive names for identifiers. A variable that stores my age should be called “age” or “myAge,” A variable that stores my age should be called “age” or “myAge,”

not “a.” This makes the program easier to understand.not “a.” This makes the program easier to understand.

Convention: Capitalize the first letter of all but Convention: Capitalize the first letter of all but the first word.the first word. Example: myAge (not myage, MyAge, or MYAGE).Example: myAge (not myage, MyAge, or MYAGE).

KeywordsKeywords are words reserved by Java that can’t be are words reserved by Java that can’t be used as identifiers.used as identifiers. See page 45 of the textbook.See page 45 of the textbook. Also null, true, and false.Also null, true, and false.

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

ExpressionsExpressionsExpressionsExpressions

ExpressionsExpressions are composed of are composed of operatorsoperators and and operandsoperands..

Operators: +, -, *, /, % (remainder), - (unary).Operators: +, -, *, /, % (remainder), - (unary). Operands: A number or a variable.Operands: A number or a variable. Trick questions:Trick questions:

What’s the value of 1.0 / 2.0?What’s the value of 1.0 / 2.0? What’s the value of 1 / 2?What’s the value of 1 / 2?

Precedence and associativity follow the rules of Precedence and associativity follow the rules of algebra.algebra. Use parentheses if you’re not sure, or for clarity.Use parentheses if you’re not sure, or for clarity.

ExpressionsExpressions are composed of are composed of operatorsoperators and and operandsoperands..

Operators: +, -, *, /, % (remainder), - (unary).Operators: +, -, *, /, % (remainder), - (unary). Operands: A number or a variable.Operands: A number or a variable. Trick questions:Trick questions:

What’s the value of 1.0 / 2.0?What’s the value of 1.0 / 2.0? What’s the value of 1 / 2?What’s the value of 1 / 2?

Precedence and associativity follow the rules of Precedence and associativity follow the rules of algebra.algebra. Use parentheses if you’re not sure, or for clarity.Use parentheses if you’re not sure, or for clarity.

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

The Assignment OperatorThe Assignment OperatorThe Assignment OperatorThe Assignment Operator

The The assignment operatorassignment operator (=) saves the result of an (=) saves the result of an expression in a variable.expression in a variable.

Example:Example: a = 2 + 2;a = 2 + 2; b = a + 4;b = a + 4; b = b + 1;b = b + 1; What will the value of b be now?What will the value of b be now?

IMPORTANT: = is assignment, not equality.IMPORTANT: = is assignment, not equality. Shortcut operators: +=, -=, *=, /=, %=.Shortcut operators: +=, -=, *=, /=, %=.

Example: Example: a = a + 1a = a + 1 is the same as is the same as a += 1a += 1..

<Code Example><Code Example>

The The assignment operatorassignment operator (=) saves the result of an (=) saves the result of an expression in a variable.expression in a variable.

Example:Example: a = 2 + 2;a = 2 + 2; b = a + 4;b = a + 4; b = b + 1;b = b + 1; What will the value of b be now?What will the value of b be now?

IMPORTANT: = is assignment, not equality.IMPORTANT: = is assignment, not equality. Shortcut operators: +=, -=, *=, /=, %=.Shortcut operators: +=, -=, *=, /=, %=.

Example: Example: a = a + 1a = a + 1 is the same as is the same as a += 1a += 1..

<Code Example><Code Example>

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

ConstantsConstantsConstantsConstants

Turn a variable into a constant by writing “final.”Turn a variable into a constant by writing “final.” Unlike variables, the value of a constant cannot be Unlike variables, the value of a constant cannot be

changed once it’s initialized.changed once it’s initialized. Example:Example:

final int myAge = 24; // Declare myAge to be a constant.final int myAge = 24; // Declare myAge to be a constant. myAge = 25; // This won’t work, since myAge is a constant.myAge = 25; // This won’t work, since myAge is a constant.

Convention: For a constant’s identifier, use all Convention: For a constant’s identifier, use all caps, and separate words with an underscore.caps, and separate words with an underscore. Example: MY_AGE instead of myAge.Example: MY_AGE instead of myAge.

<Code Example><Code Example>

Turn a variable into a constant by writing “final.”Turn a variable into a constant by writing “final.” Unlike variables, the value of a constant cannot be Unlike variables, the value of a constant cannot be

changed once it’s initialized.changed once it’s initialized. Example:Example:

final int myAge = 24; // Declare myAge to be a constant.final int myAge = 24; // Declare myAge to be a constant. myAge = 25; // This won’t work, since myAge is a constant.myAge = 25; // This won’t work, since myAge is a constant.

Convention: For a constant’s identifier, use all Convention: For a constant’s identifier, use all caps, and separate words with an underscore.caps, and separate words with an underscore. Example: MY_AGE instead of myAge.Example: MY_AGE instead of myAge.

<Code Example><Code Example>

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

ConstantsConstantsConstantsConstants

Constants are never required, but they have Constants are never required, but they have several advantages:several advantages:

1.1. Programs are easier to read (PI rather than 3.14159265359).Programs are easier to read (PI rather than 3.14159265359).

2.2. Programs are easier to modify.Programs are easier to modify.

3.3. Inconsistencies are less likely.Inconsistencies are less likely.

Use constants! It may seem like more work, but Use constants! It may seem like more work, but it will save time in the long run.it will save time in the long run.

Constants are never required, but they have Constants are never required, but they have several advantages:several advantages:

1.1. Programs are easier to read (PI rather than 3.14159265359).Programs are easier to read (PI rather than 3.14159265359).

2.2. Programs are easier to modify.Programs are easier to modify.

3.3. Inconsistencies are less likely.Inconsistencies are less likely.

Use constants! It may seem like more work, but Use constants! It may seem like more work, but it will save time in the long run.it will save time in the long run.

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

HomeworkHomeworkHomeworkHomework

Read: 2.9, 2.10 (to bottom of p. 60), 3.9 (to top of Read: 2.9, 2.10 (to bottom of p. 60), 3.9 (to top of p. 114), p. 114), 2.122.12..

Remember: Both parts of HW1 due Monday at Remember: Both parts of HW1 due Monday at the beginning of class. Email me with any the beginning of class. Email me with any questions.questions.

Read: 2.9, 2.10 (to bottom of p. 60), 3.9 (to top of Read: 2.9, 2.10 (to bottom of p. 60), 3.9 (to top of p. 114), p. 114), 2.122.12..

Remember: Both parts of HW1 due Monday at Remember: Both parts of HW1 due Monday at the beginning of class. Email me with any the beginning of class. Email me with any questions.questions.