eh2745 –lecture2 - kth · 2017-03-21 · eh2745 –lecture2 introductionto programmingin java the...

16
21/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 – Lecture 2 Introduction to Programming in Java The lecture slides are heavily based on the MIT OpenCourseware course 6.092 – Introduction to Java

Upload: others

Post on 01-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

1

KTH ROYAL INSTITUTEOF TECHNOLOGY

EH2745 – Lecture 2

Introduction to Programming in Java

The lecture slides are heavily based on the MIT OpenCourseware course 6.092 – Introduction to Java

Page 2: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

2

Outline

Intro to JavaTypes and variablesOperatorsMethods

A computer (slightly simplified)

The CPU exectues machine codeinstructions stored in memory

The memory is used both to store the program and the data needed

Communication with the real world through I/O units (keyboard, monitor, speaker, harddrive, ethernet,…)

Page 3: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

3

Machine code (abstracted to human level)

MOV A,128 Move the value 128 into register AMOV B, 127 Move the value 127 into register BADD A,B Overwrite value of A with A+B

Takes a lot of programming at that level to achieve this

Man meets machine

• Programming languages (there are several) are all the result of mans attempt to create a language which is sufficiently specific so that computer can understand it, yetunderstandable by humans.

• Different trade offs between flexibility, efficiency, ease of use, hardware performance result in different programming languages.

Page 4: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

4

What’s the thing with Java?

“Most popular” language – debatable these dayswith Python a strong contender

Runs on a “virtual machine” (JVM)Allows platform independence – used to be a big thing

More complex than some (eg. Python)

Simpler than others (eg. C++)We do not have to worry so much about the internals of the computer (e.g. Memory allocation)

From woman to machine

A person ”programs” the computer by writing Java Source codeThis language is ”sort of OK” for the human, kind of strange with [ } and ; apparently necessary.

The Java compiler converts the source code into Java Byte codeChecking for errors and converting the source code halfway into machinecode.

The Java byte code can run on any Java Virtual MachineThe Java VM is specific for different computer Operating systems (MacOS, Linux, Windows, …)

Page 5: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

5

Java source code

Outline

Intro to JavaTypes and variablesOperatorsMethods

Page 6: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

6

What here follows is the rules of the Java language that we people have to learn to make

computers do their thing

Types of Data in Java

Page 7: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

7

The eight basic types of types

boolean, True or Falsechar, 16-bit unicode charactersIntegers

– byte– short– int– long

Floating-point types (real numbers)– float– double

Note!Types do not startwith a capital letter

Variables in Java

A variable is a named location in memorythat stores data of a specific type.

Page 8: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

8

Assigning values to variables

To assign a variable a value use the ”=” signEither at declaration

Or when needed in the program

Output from the program

System.out.println – calls the method println in the Class System and sends the string within ”” to the concole.

Page 9: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

9

Putting it all together

What appears on the console?

Outline

Intro to JavaTypes and variablesOperatorsMethods

Page 10: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

10

Operators

• Symbols that perform simple computations• Assignment: =• Addition: +• Subtraction: -• Multiplication: *• Division: /

Math example

Page 11: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

11

A note on division!

Division (“/”) operates differently on variables of integer and on floating point type

Java is strongly typed

Java checks already at compilation time that you are usingtypes in the rightway. Eclipse helps you further by providingwarnings.

Generates an error at compile time, i.e.g when sourcecode is converted to bytecode.

Page 12: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

12

Type casting

Converting type by explicitly assigning values and types.

Outline

Intro to JavaTypes and variablesOperatorsMethods

Page 13: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

13

Methods in Java

Methods are in Java what in other programminglanguagesare called functions or procedures, or sub-routines or….

Calling a method is done by calling its name (!?)

NewLine example

Page 14: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

14

Calling methods with parameters

If you want to send input to a method – add it as arguments

Squares method

Page 15: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

15

Return values from methods

Returns a value of type double

Returns nothing = void

Using libraries of methods

Math.sin(x)Math.cos(Math.PI / 2)Math.pow(2, 3)Math.log(Math.log(x + y))

Etc… etc....

Page 16: EH2745 –Lecture2 - KTH · 2017-03-21 · EH2745 –Lecture2 Introductionto Programmingin Java The lectureslidesareheavilybasedon the MIT OpenCoursewarecourse6.092 –Introductionto

21/03/2017

16

Type conversion by method

int to String:String five = 5; // ERROR!

String five = Integer.toString (5);

String to int:int foo = “18”; // ERROR!

int foo = Integer.parseInt (“18”);