Title: Kickstart Intro to Java Part I
1Kickstart Intro to JavaPart I
- COMP346/5461 - Operating Systems
- Revision 1.6
- February 9, 2004
2Topics
- 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
3Me, Myself, and I
- Name Serguei Mokhov, for simplicity just
Serguei, no Sirs please!!! -) - E-mail mokhov_at_cs - maybe the best way to reach
me. Questions are welcome (but please allow some
time to reply - Im only one and youre so many
-) ). - My Course web page for COMP346/546
http//www.cs.concordia.ca/mokhov/comp346/
4Setting Up the Environment
- Please, refer to the separate set of slides for
Java version and setting up environment.
5Why 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 arent buzzwords,
its just an incomplete summary of the features
of the language.
6Java vs. C/C
- Javas 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.
7Java 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.
8Java vs. C/C (3)
- Java DOESNT 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
9Java 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.
10Java Program Structure (2)
- main()
- Declarationpublic 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 well see
in a moment. - Note, unlike in C/C, theres no return value
that you have to explicitly pass back using the
return ltintgt statement. Use System.exit(ltintgt)
instead.
11Examples
- 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! Its me
again!)
12Examples (2)
ShowArguments.java public class ShowArguments
public static void main(String argv)
for(int i 0 i lt argv.length i)
System.out.println(Arg i
argvi)
A public property of an array object
Simple string concatenation with
13Compiling and Running a Java Application
- Command line compiler javacjavac
HelloWorld.java - To run the compiled code you have to invoke JVM
to interpret itjava HelloWorld(Note no
extension this time, just the name of the main
class!)
14On-line Tutorial
- Suns Tutorial on Javahttp//java.sun.com/docs/b
ooks/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 -)
15Next Tutorial
- Arrays
- Exception Handling
- Basics of Inheritance in Java
- Threads and Scheduling
- More Examples
- Whatever I forgot to mention above and your
questions.
16Links and References
- Official Java site http//java.sun.com
- Java in a Nutshell, Second Edition by David
Flanagan, (C) 1997 OReily Associates, Inc.
ISBN 1-56592-262-X - Past semesters stuff from Paul and Tony on Dr.
Aiman Hannas web sitehttp//www.aimanhanna.com/
concordia/comp346/ - Manual pages for ssh, vim, xemacs, pico