Top Banner
6.092: Introduction to Java Lecture 1: Types, Variables, Methods
36

6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Jun 02, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

6.092: Introduction to Java

Lecture 1: Types, Variables, Methods

Page 2: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Outline

• Intro to Java

• Types and variables

• Operators

• Methods

Page 3: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

The Computer

Central Processing Unit

(CPU)

Input/Output (IO) Devices

Memory

Page 4: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

CPU Instructions

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

Page 5: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Programming Languages

• Easier to understand than CPU instructions

• Needs to be translated for the CPU to understand it

Page 6: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Java

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

Page 7: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Compiling Java

Source Code (.java)

Byte Code (.class)

javac java

Page 8: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Program Structure class CLASSNAME {

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

} }

Page 9: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Outline

• Intro to Java

• Types and variables

• Operators

• Methods

Page 10: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

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 11: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Variables

Named location that stores a value of one particular type.

Form: TYPE NAME;

Example: String foo;

Page 12: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Assignment

Use = to give variables a value.

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

Page 13: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Assignment

Can be combined with a variable declaration.

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

Page 14: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Output

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

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

Page 15: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

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 16: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Outline

• Intro to Java

• Types and variables

• Operators

• Methods

Page 17: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Operators Symbols that perform simple computations

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

Page 18: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

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 19: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Division

Division (“/”) operates differently on integers and on doubles!

Example: double a = 5.0/2.0; // a = 2.5 int b = 4/2; // b = 2 int c = 5/2; // c = 2 double d = 5/2; // d = 2.0

Page 20: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Mismatched Types

Java verifies that types always match:

String five = 5; // ERROR!

test.java.2: incompatible types found: int required: java.lang.String String five = 5;

Page 21: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Conversion by casting int a = 2; // a = 2double a = 2; // a = 2.0 (Implicit)

int a = 18.7; // ERRORint a = (int)18.7; // a = 18

double a = 2/3; // a = 0.0double a = (double)2/3; // a = 0.6666…

Page 22: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Outline

• Intro to Java

• Types and variables

• Operators

• Methods

Page 23: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Methods

{

}

public static void main(String[] arguments)

System.out.println(“hi”);

Page 24: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Adding Methods

public static void NAME() { STATEMENTS

}

To call a method:

NAME();

Page 25: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

class NewLine { public static void newLine() {

System.out.println("");}

public static void threeLines() {newLine(); newLine(); newLine();

}

public static void main(String[] arguments) { System.out.println("Line 1"); threeLines(); System.out.println("Line 2");

} }

Page 26: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

class NewLine { public static void newLine() {

System.out.println("");}

public static void main(String[] arguments) { System.out.println("Line 1"); threeLines(); System.out.println("Line 2");

public static void threeLines() {newLine(); newLine(); newLine();

}

} }

Page 27: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

public static void main(String[] arguments) { System.out.println("Line 1"); threeLines(); System.out.println("Line 2");

public static void threeLines() {newLine(); newLine(); newLine();

}

class NewLine { public static void

""newLine() {

System.out.println( ); }

} }

Page 28: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Parameters

public static void NAME(TYPE NAME) { STATEMENTS

}

To call:

NAME(EXPRESSION);

Page 29: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

class Square { public static void printSquare(int x) {

System.out.println(x*x); }

public static void main(String[] arguments) { int value = 2; printSquare(value); printSquare(3); printSquare(value*2);

} }

Page 30: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

class Square2 { public static void printSquare(int x) {

System.out.println(x*x); }

public static void main(String[] arguments) { printSquare("hello"); printSquare(5.5);

} }

What’s wrong here?

Page 31: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Multiple Parameters

[…] NAME(TYPE NAME, TYPE NAME) { STATEMENTS

}

To call:

NAME(arg1, arg2);

Page 32: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

class Multiply { public static void times (double a, double b) {

System.out.println(a * b); }

public static void main(String[] arguments) { times (2, 2); times (3, 4);

} }

Page 33: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Return Values

public static TYPE NAME() { STATEMENTS return EXPRESSION;

}

void means “no type”

Page 34: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

class Square4 { public static double square(double x) {

return x*x; }

public static void main(String[] arguments) { System.out.println(square(5)); System.out.println(square(2));

} }

Page 35: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Mathematical FunctionsMath.sin(x)

Math.cos(Math.PI / 2)

Math.pow(2, 3)

Math.log(Math.log(x + y))

Page 36: 6.092: Introduction to Java Lecture 1: Types, Variables ...€¦ · 6.092: Introduction to Java Lecture 1: Types, Variables, Methods . Outline • Intro to Java • Types and variables

Conversion by method

int to String: String five = 5; // ERROR!String five = Integer.toString (5);String five = “” + 5; // five = “5”

String to int: int foo = “18”; // ERROR! int foo = Integer.parseInt (“18”);