Download Java Programming

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

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

Document related concepts
no text concepts found
Transcript
Java Programming
CIS 421
Web-Based Java Programming
Course Text

Text:
Computing with Java:
Programs, Objects, Graphics
Alternate 2nd Edition
Art Gittleman
Scott Jones Publishers
ISBN: 1-57676-074-X
Prerequsites, Tests, Public area

Prerequisites:
Cis237, Mat 126, and QPA of 2.90

Tests: Two midterms, one final.
Public directory:
/export/home/public/spiegel/cis421

Turnin script


Programming Projects are submitted electronically
using turnin script
See turnin handout


Must setup turnin once
To invoke:

turnin421 <file1> <file2> ....




each item submitted separated by whitespace
no limit to # items
You should receive a confirmation e-mail containing an
ls –l listing of files submitted (can include files previously
submitted)
Files submitted may be overwritten by subsequent turnin of files
with same name
Overview of Java
Java Features
 How Java Works
 Applications vs Applets
 Program-Driven vs Event Driven
 Graphical User Interfaces (GUI)

Java Features
Simple, Object-Oriented, Familiar
 Robust and Secure
 Architecture Neutral and Portable
 High Performance
 Interpreted, Threaded, and Dynamic

Simple, OO, Familiar



Its simplicity comes from the fact that there are
no pointers in Java. Therefore, the
programmer does not have to manage pointers
and the resultant problems that pointers bring.
All programs in Java are based on objects
Java uses the familiar syntax and fundamental
control structures of C/C++
Robust and Secure


Robust programs run without crashing due to
programming errors, erroneous input, or
failure of external devices. Java has many
checks at compile-time and provides run-time
exception handling to deal with unexpected
events.
Security, especially across the internet, requires
careful measures, which are implemented in
Java
Architecture Neutral and
Portable
Java programs run on a variety of
processors using various operating
systems
 Portability depends not only on
architecture but also on implementation.
Java specifies the language carefully to
reduce implementation dependencies.

High Performance
Java versions continually increase
performance capabilities.
 In network applications, communication
delays usually far exceed performance
delays.

Interpreted, Threaded, and
Dynamic
Interpreted, not compiled.
 Threaded – capable of multi_tasking and
concurrent processing, even across the
internet
 Dynamic linking to library code as it
needs it.
 Java is ideally suited for general,
interactive, and network programming

How Java Works
Java
Source
Code
Java
Compiler
Java
Byte
Code
Java Interpreter
For Processor 2
Java Interpreter
For Processor 1
Setting up Unix for Java


Make a directory called java in your home directory.
Put this java directory in your CLASSPATH in .cshrc
with the command (this is one long line)
setenv CLASSPATH
“.:$home/java:whatever_is_there_already“



this allows packages starting from your java subdirectory
Put the directory /usr/j2sdk1.4.0_03/bin in your path
variable in the .login file.
Give the commands source .login and source .cshrc to
make the changes effective.

These changes are applied automatically on subsequent logins
Testing Changes
acad > java -version
java version "1.4.0_03"
Java(TM) 2 Runtime Environment,
Standard Edition (build 1.4.0_03-b04)
Java HotSpot(TM) Client VM (build
1.4.0_03-b04, mixed mode)
acad >
Applications vs Applets
Applications run like programs written
in other languages. They usually are
standalone, running at the command line.
 Applets are usually small applications
that are embedded in a web-page and run
on the internet in a web browser.

Building an Application




Edit the source file either in your favorite editor,
or IDE, and save it as <file>.java
The file name must be the same as the one and
only public class name in the file
Compile by giving the command
javac <file>.java
at the command line of a DOS or Unix window.
Run the program by giving the command
java <file>
hello.java
//
//
//
//
//
//
//
//
File:
Compiler:
Compile:
Executable:
Execute:
hello.java
Sun javac
javac hello.java
hello.class
java hello
Purpose:
Prints usual first
program
hello.java
public class hello
{
public static void main(String[] args)
{
int x = 3;
System.out.println(“Hello, World!” +
“\nx = “ + (x) );
}
}
Application Programs
The file name must be the same as the
one and only public class in the file.
 This public class must have a static
method called main (Why static??)

public static void main( String[] args)
{
local declarations and statements
}
Exercise

Setup your Unix account to compile and
run a Java Application program. Test it
with the HelloWorld program hello.java.
 The
example is in the java/hello subdirectory
in the instructor’s public area
Basic Java Programming

The C/C++ component








Comments – C/C++ style
Identifiers and keywords
Types, variables, expressions
Control structures
Functions
System output
Console input
Packages
Identifiers and Keywords



A Java identifier must start with a letter, followed by 0 or
more letters and/or digits. Java is case-sensitive.
Keywords cannot be used as user-identifiers. See text for a
list of keywords.
Style – recommended & preferred; consistency is a must!





Class names begin with a capital letter
Variable names begin with a lower case letter
Function names begin with a verb which is lowercased.
Constants are all upper case.
Multiple word names are lower case except for the beginning of each
word component.
Examples
Request would be a class name
 myRequest would be a variable name
 getRequest() would be a function
(method) name
 THE_REQUEST would be a constant.
 The Java standard style convention
should be followed in our programming.

Standard Types
char – ascii or unicode
 boolean – true or false
 Numerical types – various sizes of
numbers

Numerical Types
Standard numerical types in Java are
type
size least value greatest value
________________________________________
byte
8
-128
127
short
16
-32768
32767
int
32
-2147483648
2147483647
long
64
-263
263-1
float*
32
~ -3.4 x 1038
~ 3.4 x 1038
double*
64
~ -1.7 x 10308
~ 1.7 x 10308
* 7 and 15 digit accuracy respectively

Variables and Expressions
Java follows the syntax of C/C++ for
expressions and assignment.
 The operators for the standard types are
the same as those for C/C++
 Remember that = is assignment and
== is equal relational operator.
 You should NOT use = in a cascading
manner.

Control Structures





The control structures are the same as C/C++
if
switch
for
while
do – while
Note: unlike C/C++, where the expression can
evaluate to int, the test expression MUST be of
type boolean
Functions (Methods)



In Java there are no independent functions
A function (method) is always a member
function of some class.
The syntax is very similar.
modifier(s) resulttype name( <params>)
{
local declarations
and statements
}
// the modifier is public, private, or protected, and can also
be prefaced static
System Output



Output is generated by using streams. The
stream classes are defined in the standard Java
package java.io.
The class System in the package java.lang
contains three different streams for use in Java
programs:
System.in
the keyboard
System.out
the screen
System.err
the screen
System.out.println( any string)
Examples of Output
To print an object, the object should have
overloaded the method toString that is
inherited from the Class Object.
 Standard types have this method.
 System.out.println(“The value of x = “ + x );
 The + is the concatenation operator for strings.

System Input



System input is quite complicated, so many
authors provide a package of IO functions for
the standard types.
We prefer to avoid these. A class InputReader is
in the public java directory for text input.
More fundamental IO will be discussed later

Java is made for GUIs, particularly components
such as TextFields, Menus. etc;
Examples of input
helloYou.java
 helloInt.java


java/hello directory in public area
Things that are different
String concatenation is the operator +
 It takes two operands which are
“stringable”, that is any operand that is a
string or has the toString method
overloaded for that type.
 System.out.println(76 + “trombones”);

Packages
Java organizes code into packages which
correspond to directories in the file system.
 Each Java class is contained in a package.
 The default package is . (the current
directory)
 The System class is found in java.lang
 The Applet class is found in java.applet

Comments
When calling methods of the same class, we
do not need to use the class name as a
prefix.
 When calling methods of another class, we
use the class name or object of that class as
a prefix.

Objects
Still declared as a class
 No separate sections

 Each

declaration specified with access
public, private, protected
static declarations part of class, but not
object
 non-static declarations part of instantiated
objects only
