Skip to main content

more options

Part 3. Types and expressions    Part 5. Objects and classes

Module 1, part 4. Variables, declarations, and assignments

Introduction

This part 4 introduces you to variables, the Java declaration (of a variable), and the assignment statement. We also talk about strong versus weak typing —Java is strongly typed, while Matlab and C are weakly typed.

 

Contents

No. Topic Discussion
1. The variable and its declaration
(blecture doc ppt.pdf)
Java's concept of variable is similar to that in most other languages: a named box with a value in it. In Java, a variable must be declared before it is used, and it has a type.
2. The assignment statement
(blecture doc ppt.pdf)
Execution of an assignment statement stores a value in a variable. Learn also about the initializing declaration and narrower and wider types.
  Reading for parts 1 and 2: Gries/Gries, Section 1.2, pp. 26–29.
3. Promoting and casting
(PLive lectures 6-2.3..4)
Reading: Gries/Gries, Section 1.3.1,pp. 19–20.
In several circumstances, Java will automatically promote a value to a wider type. In addition, you, the programmer can explicitly cast a value from one type to another. This lecture explains how and why this is done.
4. char as an integral type.
Reading: Gries/Gries, Sec. 6.5, pp. 224–225. PLive lectures 6-5–2..4.
You are not responsible for this material, but reading it will help you understand how characters are represented.
5. Conditional expression (html) The conditional expression evaluates one of two subexpressions based on a condition (a boolean expression). You will use it in assignment 1.
6. Safety and strong typing (html) This little essay will give you some understanding of why strong typing is used in DrJava.
7. Lab 01. Expressions, variables, declarations, and assignments Lab 01 gives you practice in DrJava with basic concepts of expression, variables, etc. Do this on your own, if you want to show the results to a consultant to make sure you did everything right, they will be happy to talk to you.
8. Self-help exercises. Gries/Gries,
Section 1.2.1, pp. 29–30.
It is important that you fully understand declarations and the assignment statement. These exercises will help you in this endeavor.
9. Quiz 1. Assignments This quiz will test your understanding of assignments and declarations. See a consultant in the lab at your convenience. You cannot take the test for module 1 until you have passed this (and other) quizzes.