Sei sulla pagina 1di 26

JAVA BASICS

Why Java for this Project?


Its open source - FREE
Java has tools that work well with rdf and
xml
– Jena, Jdom, Saxon
Can be run on UNIX,Windows,LINUX,etc
GUI/Applet capabilities
Igor’s application
WE NEED TO LEARN IT ANYWAY!
How to run
JDK – Java Development Kit
– Basic compiler, linker, and libraries
IDE – Integrated Development
Environment
– NetBeans – netbeans.org
How to run
Types of files
– .java – source code
– .class – executable code
– .jar (java archive) - bundle of multiple files
Tutorial -
http://java.sun.com/docs/books/tutorial/jar/bas
ics/index.html
– Packages – groups of related classes in a
directory
How to run
Applications
– javac – java compiler
Commandline - javac classname.java
– java – executes application
Commandline - java classname
– Jikes – faster version of java command
http://oss.software.ibm.com
Commandline – jikes classname
How to run
Environment Variables
– Path – searches computer for executeable
– Classpath – searches computer for classes
that need to be used
How to run
Applets – java applications that are
embedded in html and run in a browser
– Appletviewer – shows sample in browser
Commandline – appletviewer appletpage.html
How to run
Make – (if you are sick of javac and java)
– Defines which components go together to
make a program.
– Defines how to "put the pieces together".
– Keeps track of dependencies among
components.
– Helps avoid doing unnecessary work and
forgetting to do necessary work
How to run
Makefile – example

default:
javac Animal.java

note: there is a tabspace in front of javac


Commandline – make
- This compiles the file called Animal.java
How to run
Makefile – example

Tiger.java: Tiger.java Animal.class


javac Tiger.java

Animal.class must exist for tiger.java to compile

Commandline – make tiger.java


How to run
Makefile – example

all: Tiger Animal

Tiger:
javac Tiger.java

Animal:
javac Animal.java

Commandline – make all

“all” triggers both Tiger and Animal to compile


How to run
ANT
– Similar to make in that it stores in a project
– Apache application
– See handout
Java Programming
Import
– “includes” a predefined java package that will be used
in a program
– Statements made outside of the code
– Package must be contained in a directory given in the
classpath
– * denotes search entire directory
– Example
Import java.applet.Applet;
Public class BinarySearch extends JApplet {
…}
Java Programming
Important predefined java classes
– Java.io.* - i/o files and streams
– Java.awt.*,java.swing.* - GUI tools
– Java.util.* - data structures
– Java.applet.Applet – applets
– Java.servlet.* - used for scripts, tomcat and
other interactive servers
– Java.sql.* - used for sql handling
– Java.net.* - network apps
Java Programming
Package – used for user defined packages
– Command used when wanting to include the class in
a package
– Used outside class declaration
– Example
package john.harney.example;
Public class whatever {…}
- Assuming the current directory is default, this
statement will place the whatever.class in the
default/john/harney/example directory
- Commandline – javac –d . Whatever.java
Java Programming
General Format
/*import and package statements here*/
public/private/protected class classname{

Member variables
Constructors
Methods (member functions)

}
- File must be saved as “classname.java”
- Main (like c++) is executeable for an application (public static
void main(args))
- Class must be compiled in order to be used by another class
Java Programming
Executable example (Welcome3.java)
public class Welcome3 {

// main method begins execution of Java application


public static void main( String args[] )
{
System.out.println( "Welcome\nto\nJava\nProgramming!" );

} // end method main

} // end class Welcome3


Circle.java example (Java vs C++)
– Class Header
Instead of:
class Circle : public Point
There is:
public class Circle extends Point;
Java Programming
Circle.java example (Java vs C++)
– Member variables
Instead of:
private:
double radius;
There is:
private double radius;
Java Programming
Circle.java example (Java vs C++)
– Constructors
Instead of:
Circle(int x,int y, double radiusValue);
There is:
public circle(int x,int y, double radiusValue);
Note: “public” denotes that this may be constructed
outside of the class declaration
Java Programming
Circle.java example (Java vs C++)
– Member functions (methods)
Instead of:
double getRadius()
{
return radius;
}
There is:
public double getRadius()
{
return radius;
}
Note: “public” denotes that this may called outside of the class
Java Programming
Circle.java example
//assume the compiler can find circle.class
Import javax.swing.JOptionPane; //allows GUI input
public class CircleImplement {
// main method begins execution of Java application
public static void main( String args[] )
{
Circle circ = new Circle(2,2,4.0); //constructor
double rad = circ.getRadius();//calls to method getRadius
JOptionPane.showMessageDialog(null,rad); //prints radius

} // end method main

} // end class Welcome3


Java Programming
Other notable differences between Java
and C++
– Strings are immutable
– No global variables
– Memory allocation is not needed (ie no
pointers)
– Garbage Collection
– No operator overloading
Javadoc
Javadoc – gives info about a source file
– Commandline – javadoc class.java
– Gives html documentation on the variables,
methods, inheritence, other comments, etc.
– Format – see handout
JDOM
JDOM – java class that enables XML
construction and parsing
– Handout
Saxon
Saxon -
http://saxon.sourceforge.net/saxon6.5.3/in
dex.html
– Supports XSLT,XPath,XQuery
– Can be used with JDOM
– Commandline
java  classname sourcexml > destinationxml

Potrebbero piacerti anche