introduction to java - sharif university of...

26
Introduction to Java Computer Engineering Department Sharif University of Technology

Upload: others

Post on 18-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Introduction to Java

Computer Engineering DepartmentSharif University of Technology

Page 2: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

1st Quiz

2

Page 3: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

1st Quiz

1.What is Object?

2.What is Class?

3.What are the properties of an Object Oriented Programming? Define them!

2

Page 4: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

1st Quiz

1.What is Object?

2.What is Class?

3.What are the properties of an Object Oriented Programming? Define them!

2

Your Time is Finished

Page 5: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Programming Language

3

Page 6: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Programming Language

3

• A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer.

• PLs should support

• Data type, Sequence control, Object, Class, ...

• Java is an OOPL.

Page 7: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Interpret vs. Compile

4

Page 8: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Interpret vs. Compile

4

• Compile

• The program written in a language is translated to another language by another program named compiler.

• Interpret

• The program written in a language is executed statement by statement by another program named interpreter.

Page 9: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

5

C

C++

Pascal

Pascal Compiler for Linux

C++ Compiler for Mac

C Compiler for Windows

Pascal Compiler for Windows

Pascal Compiler for Mac

C Compiler for Mac

C Compiler for Linux

C++ Compiler for Mac

C++ Compiler for Mac

Platform and Compiler

Page 10: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Platform Independency

6

Slot 1

Slot 2

Slot 3

The

Device

Page 11: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Platform Independency

6

Slot 1

Slot 2

Slot 3

The

Device

When we have different platforms and a single device,

what can we do to support this device on all platforms?

Page 12: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Platform Independency

6

Slot 1

Slot 2

Slot 3

The

Device

When we have different platforms and a single device,

what can we do to support this device on all platforms?

Page 13: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Entering the Java world!

Edit your java files

Compile your files

Run the class file

Java file

Class file

7

Page 14: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Entering the Java world!

• A Java file contains a class.

• A class contains a main function.

• Java file compiled to byte code file (class file)

• You can run your class.

Edit your java files

Compile your files

Run the class file

Java file

Class file

7

Page 15: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

1st Exercise8

/* * This is your first java program. */

class HelloWorld{public static void main(String[] args){System.out.println(“Hello World!”);

}}

Page 16: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Edit your java file

9

Page 17: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Edit your java file

• Any editor can be used

• VIM, Gedit, Textpad, Kedit, Textedit, Notepad, ...

• It’s much more simpler if you use an IDE

• Eclipse, IDEA, Forte, NetBeans, ...

• Java files have .java extension.

9

Page 18: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Java Compilation

10

Page 19: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Java Compilation

10

• The Java Compiler (javac) is used to compiler Java sources into byte code files.

• javac is just available in the Java SDK not JRE.

• Byte code files have .class extension.

• Byte code is the assembly language for the Java Virtual Machine (JVM).

Page 20: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Compile HelloWorld

11

$javac HelloWorld.java

Page 21: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Compile HelloWorld

• Java files can be compiled with javac (Java Compiler)

• Now a file named HelloWorld.class is generated.

11

$javac HelloWorld.java

Page 22: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Running Java Classes

12

Page 23: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Running Java Classes

• Java Virtual Machine (JVM) is used for running class files.

• JVM can run class files.

• Now, there are officially supported JVMs for Linux, Windows, Mac OS X and Solaris.

• JVM is also available for FreeBSD, AIX, Darwin, ...

12

Page 24: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Run HelloWorld

13

$java HelloWorld

Page 25: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Run HelloWorld

• Use java to run the class file.

13

$java HelloWorld

Page 26: Introduction to Java - Sharif University of Technologyce.sharif.edu/.../resources/root/lectures/presentation4.pdf · 2017-02-07 · Entering the Java world! • A Java file contains

Any Questions?