Download Programming in Java CSCI-2220

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Programming
Course Overview
Introduction
www.ict.up.ac.th/thanawats/pro_lang
Course
•
•
•
•
•
Lecture – Lab session
การสอบกลางภาคและสอบปลายภาค
การสอบการเขียนโปรแกรม (ภาคปฏิบตั ิ)
การบ้าน ทดสอบย่อย
โปรเจค
Evaluation
•
•
•
•
•
•
•
การทดสอบภาคปฏิบตั ิ 1
การสอบกลางภาค
การทดสอบภาคปฏิบตั ิ 2
การสอบปลายภาค
โปรเจค
การบ้าน แบบฝึ กหัด รายงาน
จิตพิสยั
10%
25%
10%
25%
20%
5% (ให้เขียนด้วยลายมือ)
5%
Textbooks
– Beginning Java 2 SDK 1.4 Edition, by Ivor
Horton; Wrox Press; ISBN: 1861005695
– Thinking in Java, 3/e by Bruce Eckel; Prentice
Hall; ISBN: 0131002872
– วีระศักดิ์ ซึ งถาวร (2541). Java Programming
Volume 1. กรุ งเทพมหานคร : ซี เอ็ดยูเคชัน่ .
– วีระศักดิ์ ซึ งถาวร (2543). Java Programming
Volume 2. กรุ งเทพมหานคร : ซี เอ็ดยูเคชัน่ .
Topics
•
•
•
•
•
•
•
•
•
Java Basics
OOP, Classes, Inheritance, Interfaces
Exceptions
Collections
I/O Streams
Files, Directories, Serialization
Threads
GUI
*** Networking
Introduction
• What is Java?
– A high level programming language
– Similar to C++ (in some ways)
– Operating system independent
• Java Virtual Machine (JVM)
• Provides a secure operating environment that runs as a layer on top
of the operating system
• Sandbox
– Object oriented programming language
• In Java, everything is a class (well, almost)
• Unlike C++, where OOP support is built on top of the language, in
Java, OOP support is a fundamental component
Java Features
• Lots of built in pre-defined classes!
– In Java, before you write any code, check to make sure
somebody hasn’t already done it for you!
• Dynamic memory management
– Automatic garbage collection
– No pointers! (Even though every class variable is a pointer and
all objects are passed by reference)
– No memory leaks!
• Error handling built into language (Exceptions)
• Threads and synchronization primitives
• Security
Java NonFeatures
• What C++ programmers might try to do but can’t:
– Use pointers (but like we said before, everything is a pointer)
– Include files (use packages instead)
– Use global variables (although the concept of global variables
can easily be simulated)
– Operator overloading
– templates (although the new Java 5.0 has introduced support for
these)
– Multiple inheritance (interfaces are used instead)
– Destructors (nobody liked them anyways)
The JVM and Bytecode
• Unlike C++ programs, Java programs are not compiled
into machine language
• Instead they are compiled into Java bytecode
• The bytecode is then interpreted by the Java Virtual
Machine (JVM)
• This is the key to Java’s universality. Any compiled Java
bytecode can be run on any valid JVM installed on any
Operating System (on any device, not just a computer)
• With C++ and other compiled languages, programs must
be recompiled for each particular architecture that it
needs to be run on
Java Programs
• As we said before, in Java, everything is a class
• Each class is contained within its own file
• The name of each file should be exactly the same as the name of
the class that it contains along with a .java extension
– i.e. MyFirstJavaClass.java would contain the class MyFirstJavaClass
– Note: this name convention is not a suggestion, it is a requirement in
order for your Java programs to compile correctly
MyFirstJavaClass.java
public class MyFirstJavaClass {
public static void main(String[] args) {
System.out.println(“Hello World”);
}
}
Software
• Java programs can be compiled and run with a tool
called the Java SDK (Software Development Kit)
• It can be downloaded for free from
http://java.sun.com/j2se/1.4.2/download.html
• It is also already pre-installed and available on several
public RPI campus machines
• Currently Java 1.5.0 (also known as Java 5.0) is
available. We will not be using any of the additional
features in this version and therefore you should use
version 1.4.2
Java Platform
• Java Platform มีความหลากหลาย แต่สามารถแบ่งเป็ นกลุ่มใหญ่ๆได้ 3
กลุ่มดังนี้
• J2SE : Java(tm) Standard Edition
– Suitable for 'usual' Desktops
• J2EE : Java(tm) Enterprise Edition
– Suitable for Servers
• J2ME : Java(tm) Micro Edition
– Suitable for Mobile
Java Platform
SDK Details
• The following tools are available in the SDK for compiling
and running your Java programs (among others)
– javac
• Java compiler
• Generates Java bytecode from given source code
– java
• Java intepreter
• Runs a Java program from the given bytecode
– jar
• Used in creating Java Archive (jar) files
• Can be a convenient method for distributing finished Java programs
How to use the Compiler
• javac [java filenames]
– Pass an unbounded number of source code files (.java) as
command line arguments
– Will take each file and generate a .class file of the same name
(bytecode file)
• i.e. MyFirstJavaClass.java would result in a bytecode file called
MyFirstJavaClass.class
– javac *.java will compile and generate bytecode files for
every .java file in the current working directory
How to use the Interpreter
• java [classname]
– When running Java programs, you must indicate which class you
wish to run
• Note that you do not give the filename, just the class name. This is
why the naming conventions for the filenames is important
– The interpreter will then search for the bytecode file named
[classname].class in the directory or directories indicated by
something called the classpath
• The given class should have a method with the prototype:
– public static void main()
– It is this method that will automatically be run by the Java intepreter
More about the classpath
• By default the classpath is set to the current working
directory
• Depending on the software and operating system you
are using, you can indicate the classpath in a number of
ways
– You can always directly indicate the classpath by using the –
classpath or –cp switch from the command line:
• java –cp /home/kelleo/java/ MyFirstJavaClass
– Under Windows, the classpath can also typically be modified
from the Environment Variables section of the System settings in
the Control Panel
– Third party software such as Eclipse and Borland JBuilder may
also do things differently
ที่มา
• Rensselaer Polytechnic Institute: Owen Kellett