16
May 9, 2002 Serguei A. Mokhov, mokhov @cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002Serguei A. Mokhov, [email protected] 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

Embed Size (px)

Citation preview

Page 1: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

1

Kickstart Intro to JavaPart I

COMP346/5461 - Operating Systems

Revision 1.6

February 9, 2004

Page 2: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

2

Topics• Me, Myself, and I• Why Java 1.2.*?• Setting Up the Environment• Buzz about Java

– Java vs. C++– Basic Java Syntax– Compiling and Running Java Programs– Example(s)

• Next Tutorial• References

Page 3: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

3

Me, Myself, and I

• Name: Serguei Mokhov, for simplicity just Serguei, no “Sir”s please!!! :-)

• E-mail: mokhov@cs - maybe the best way to reach me. Questions are welcome (but please allow some time to reply - I’m only one and you’re so many :-) ).

• My Course web page for COMP346/546: http://www.cs.concordia.ca/~mokhov/comp346/

Page 4: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

4

Setting Up the Environment

• Please, refer to the separate set of slides for Java version and setting up environment.

Page 5: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

5

Why is Java sooo co0OL???

• “Dear Serguei, you promised an Intro to Java!!”

• Here it goes: Java is a quite simple, OO, distributed, interpreted, robust and secure, platform and architecture independent, multithreaded and dynamic language.

• A bunch of buzzwords? No, they aren’t buzzwords, it’s just an incomplete summary of the features of the language.

Page 6: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

6

Java vs. C/C++

• Java’s syntax is very similar to that of C/C++; thus, it is quite easy to learn for C/C++ programmers.

• However, there are some conceptual differences behind this syntactical similarity, which you should pay close attention to.

Page 7: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

7

Java vs. C/C++ (2)

• Java– is pure OO language unlike C/C++.– has everything as an object with and exception

of few primitive data types (int, float, etc.).– uses two-byte (16 bit) Unicode characters.– has well defined and sometimes mandatory to

use the Exception Handling Mechanism (will be covered later).

– has automatic garbage collection.

Page 8: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

8

Java vs. C/C++ (3)• Java DOESN’T have

– Multiple inheritance (well, it somewhat does through interfaces and, but this is not a true inheritance).

– Templates. Vast majority of objects inherit from the Object class or its descendants (implicitly or explicitly), so any object can be cast to the Object class.

– Pointers, only references. All objects manipulated by reference by default in Java, not like in C++. So, there is no & in the syntax for function parameters.

– Operator overloading– Some others…

Page 9: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

9

Java Program Structure• Since Java is a pure OO language even the main() function has

to be defined in a class.• An instance of the class, which defines main(), will be the main

thread when run.• There should be only one public class with main in a file and the

file name must be the same (including capitalization and spelling) as the main class’ name plus the .java extension.

• In general: one (any) public class per file. It is possible to have more than one class defined within one .java file, but only one of them should be public and the file name should correspond to that, public, class name.

• When a java program compiled with no errors with javac, a JVM object code is produced and stored in .class files. One file per class.

Page 10: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

10

Java Program Structure (2)• main()

– Declaration:public static void main(String argv[]){ … }

– The argv is a list of arguments passed via command line, just like in C/C++.

– There is no argc in Java for a reason we’ll see in a moment.

– Note, unlike in C/C++, there’s no return value that you have to explicitly pass back using the return <int>; statement. Use System.exit(<int>); instead.

Page 11: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

11

Examples

• Immortal Hello World Application

Note, a java file must be named as HelloWorld.java

public class HelloWorld

{

public static void main(String argv[])

{

System.out.println(“Hello dear World! It’s me again!”);

}}

Page 12: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

12

Examples (2)

• Command Line Arguments

ShowArguments.java

public class ShowArguments

{

public static void main(String argv[])

{

for(int i = 0; i < argv.length; i++)

System.out.println(“Arg[“ + i + ”]: ” + argv[i]);

}}

A public property of an array object

Simple string concatenation with “+”

Page 13: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

13

Compiling and Running a Java Application

• Command line compiler: javacjavac HelloWorld.java

• To run the compiled code you have to invoke JVM to interpret it:java HelloWorld(Note: no extension this time, just the name of the main class!)

Page 14: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

14

On-line Tutorial

• Sun’s Tutorial on Java:http://java.sun.com/docs/books/tutorial/

• Material for the course:– Getting Started– Learning the Java Language– Essential Java Classes, especially Thread and Object– Collections

• For your own pleasure and enjoyment:– Everything else :-)

Page 15: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

15

Next Tutorial

• Arrays

• Exception Handling

• Basics of Inheritance in Java

• Threads and Scheduling

• More Examples

• Whatever I forgot to mention above and your questions.

Page 16: May 9, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004

May 9, 2002 Serguei A. Mokhov, [email protected]

16

Links and References

• Official Java site: http://java.sun.com• Java in a Nutshell, Second Edition by David

Flanagan, (C) 1997 O’Reily & Associates, Inc. ISBN: 1-56592-262-X

• Past semester’s stuff from Paul and Tony on Dr. Aiman Hanna’s web site:http://www.aimanhanna.com/concordia/comp346/

• Manual pages for ssh, vim, [x]emacs, pico