24
6.092: Introduction to Java 1: Types, Variables, Operators

6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Embed Size (px)

Citation preview

Page 1: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

6.092: Introduction to Java 1: Types, Variables, Operators

Page 2: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Goal

Learn enough Java to do somethinguseful

Examples: • Simulate a natural/engineering process • Manipulate PDFs • Draw pretty graphics

Page 3: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Assignments • View and submit via Stellar • Due at 3 PM the next day (24 hours) • Collaborate with others • Write your own code • Must submit first assignment

Must submit a “reasonable” attempt for 6/7assignments to pass

Page 4: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

The Computer

Central Processing Unit

(CPU)

Input/Output (IO) Devices

Memory

Page 5: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

CPU Instructions

z = x + y Read location x Read location y Add Write to location z

Page 6: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Programming Languages

• Easier to understand than CPU instructions

• Needs to be translated for the CPU to understand it

Page 7: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Java

• “Most popular” language • Runs on a “virtual machine” (JVM) • More complex than some (eg. Python) • Simpler than others (eg. C++)

Page 8: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Compiling Java

Source Code (.java)

Byte Code (.class)

javac java

Page 9: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

First Program class Hello {

public static void main(String[] arguments) { // Program execution begins here

System.out.println("Hello world."); }

}

Page 10: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Program Structure class CLASSNAME {

public static void main(String[] arguments) { STATEMENTS

} }

Page 11: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Output

System.out.println(some String) outputs tothe console

Example: System.out.println(“output”);

Page 12: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Second Program class Hello2 {

public static void main(String[] arguments) { System.out.println("Hello world."); // Print once System.out.println("Line number 2"); // Again!

} }

Page 13: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Types

Kinds of values that can be stored and manipulated.

boolean: Truth value (true or false). int: Integer (0, 1, -47). double: Real number (3.14, 1.0, -2.1). String: Text (“hello”, “example”).

Page 14: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Variables

Named location that stores a value of one particular type.

Form: TYPE NAME;

Example: String foo;

Page 15: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Assignment

Use = to give variables a value.

Example: String foo; foo = “IAP 6.092”;

Page 16: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Assignment

Can be combined with a variable declaration.

Example: double badPi = 3.14; boolean isJanuary = true;

Page 17: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

class Hello3 { public static void main(String[] arguments) {

String foo = "IAP 6.092"; System.out.println(foo); foo = "Something else"; System.out.println(foo);

} }

Page 18: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Operators Symbols that perform simple computations

Assignment: = Addition: + Subtraction: - Multiplication: * Division: /

Page 19: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Order of Operations Follows standard math rules:

1. Parentheses 2. Multiplication and division 3. Addition and subtraction

Page 20: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

class DoMath { public static void main(String[] arguments) {

double score = 1.0 + 2.0 * 3.0; System.out.println(score); score = score / 2.0; System.out.println(score);

} }

Page 21: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

class DoMath2 { public static void main(String[] arguments) {

double score = 1.0 + 2.0 * 3.0; System.out.println(score); double copy = score; copy = copy / 2.0; System.out.println(copy); System.out.println(score);

} }

Page 22: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

String Concatenation (+) String text = "hello" + " world"; text = text + " number " + 5; // text = "hello world number 5"

Page 23: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

Assignment: GravityCalculator

Compute the position of a falling object:

x(t) = 0.5 × at2 + vit + xi

Page 24: 6.092 Lecture 1: Types, Variables, Operators · Python) • Simpler than others (eg. C++) Compiling Java Source Code (.java) Byte Code (.class) javac java . First Program class Hello

MIT OpenCourseWarehttp://ocw.mit.edu

6.092 Introduction to Programming in Java January (IAP) 2010

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.