JAVA BASICS

26
JAVA BASICS JAVA BASICS

description

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 - PowerPoint PPT Presentation

Transcript of JAVA BASICS

JAVA BASICSJAVA BASICS

Why Java for this Project?Why Java for this Project?

Its open source - FREEIts open source - FREE

Java has tools that work well with rdf and Java has tools that work well with rdf and xmlxml– Jena, Jdom, SaxonJena, Jdom, Saxon

Can be run on UNIX,Windows,LINUX,etcCan be run on UNIX,Windows,LINUX,etc

GUI/Applet capabilitiesGUI/Applet capabilities

Igor’s applicationIgor’s application

WE NEED TO LEARN IT ANYWAY!WE NEED TO LEARN IT ANYWAY!

How to runHow to run

JDK – Java Development KitJDK – Java Development Kit– Basic compiler, linker, and librariesBasic compiler, linker, and libraries

IDE – Integrated Development IDE – Integrated Development EnvironmentEnvironment– NetBeans – netbeans.orgNetBeans – netbeans.org

How to runHow to run

Types of filesTypes of files– .java – source code.java – source code– .class – executable code.class – executable code– .jar (java archive) - bundle of multiple files .jar (java archive) - bundle of multiple files

Tutorial - Tutorial - http://java.sun.com/docs/books/tutorial/jar/bashttp://java.sun.com/docs/books/tutorial/jar/basics/index.htmlics/index.html

– Packages – groups of related classes in a Packages – groups of related classes in a directorydirectory

How to runHow to run

ApplicationsApplications– javac – java compilerjavac – java compiler

Commandline - javac classname.javaCommandline - javac classname.java

– java – executes applicationjava – executes applicationCommandline - java classnameCommandline - java classname

– Jikes – faster version of java commandJikes – faster version of java commandhttp://oss.software.ibm.comhttp://oss.software.ibm.com

Commandline – jikes classnameCommandline – jikes classname

How to runHow to run

Environment VariablesEnvironment Variables– Path – searches computer for executeablePath – searches computer for executeable– Classpath – searches computer for classes Classpath – searches computer for classes

that need to be usedthat need to be used

How to runHow to run

Applets – java applications that are Applets – java applications that are embedded in html and run in a browserembedded in html and run in a browser– Appletviewer – shows sample in browser Appletviewer – shows sample in browser

Commandline – appletviewer appletpage.htmlCommandline – appletviewer appletpage.html

How to runHow to run

Make – (if you are sick of javac and java)Make – (if you are sick of javac and java)– Defines which components go together to Defines which components go together to

make a program. make a program. – Defines how to "put the pieces together". Defines how to "put the pieces together". – Keeps track of dependencies among Keeps track of dependencies among

components. components. – Helps avoid doing unnecessary work and Helps avoid doing unnecessary work and

forgetting to do necessary work forgetting to do necessary work

How to runHow to run

Makefile – exampleMakefile – example

default: default:

javac Animal.javajavac Animal.java

note: there is a tabspace in front of javacnote: there is a tabspace in front of javac

Commandline – makeCommandline – make

- This compiles the file called Animal.java- This compiles the file called Animal.java

How to runHow to run

Makefile – exampleMakefile – example

Tiger.java: Tiger.java Animal.class Tiger.java: Tiger.java Animal.class

javac Tiger.javajavac Tiger.java

Animal.class must exist for tiger.java to compileAnimal.class must exist for tiger.java to compile

Commandline – make tiger.javaCommandline – make tiger.java

How to runHow to run

Makefile – exampleMakefile – example

all: Tiger Animal all: Tiger Animal

Tiger:Tiger:javac Tiger.javajavac Tiger.java

Animal:Animal:javac Animal.javajavac Animal.java

Commandline – make allCommandline – make all

““all” triggers both Tiger and Animal to compileall” triggers both Tiger and Animal to compile

How to runHow to run

ANT ANT – Similar to make in that it stores in a projectSimilar to make in that it stores in a project– Apache applicationApache application– See handoutSee handout

Java ProgrammingJava Programming

ImportImport– ““includes” a predefined java package that will be used includes” a predefined java package that will be used

in a programin a program– Statements made outside of the codeStatements made outside of the code– Package must be contained in a directory given in the Package must be contained in a directory given in the

classpathclasspath– * denotes search entire directory* denotes search entire directory– ExampleExampleImport java.applet.AppletImport java.applet.Applet;;Public class BinarySearch extends JApplet {Public class BinarySearch extends JApplet {……}}

Java ProgrammingJava Programming

Important predefined java classesImportant predefined java classes– Java.io.* - i/o files and streamsJava.io.* - i/o files and streams– Java.awt.*,java.swing.* - GUI toolsJava.awt.*,java.swing.* - GUI tools– Java.util.* - data structuresJava.util.* - data structures– Java.applet.Applet – appletsJava.applet.Applet – applets– Java.servlet.* - used for scripts, tomcat and Java.servlet.* - used for scripts, tomcat and

other interactive serversother interactive servers– Java.sql.* - used for sql handlingJava.sql.* - used for sql handling– Java.net.* - network appsJava.net.* - network apps

Java ProgrammingJava Programming

Package – used for user defined packagesPackage – used for user defined packages– Command used when wanting to include the class in Command used when wanting to include the class in

a packagea package– Used outside class declarationUsed outside class declaration– ExampleExamplepackage john.harney.example;package john.harney.example;Public class whatever {…}Public class whatever {…}

- Assuming the current directory is default, this Assuming the current directory is default, this statement will place the whatever.class in the statement will place the whatever.class in the default/john/harney/example directorydefault/john/harney/example directory

- Commandline – javac –d . Whatever.javaCommandline – javac –d . Whatever.java

Java ProgrammingJava Programming

General FormatGeneral Format/*import and package statements here*//*import and package statements here*/public/private/protected class classname{public/private/protected class classname{……Member variablesMember variablesConstructorsConstructorsMethods (member functions)Methods (member functions)……}}- File must be saved as “classname.java”File must be saved as “classname.java”- Main (like c++) is executeable for an application (public static Main (like c++) is executeable for an application (public static

void main(args))void main(args))- Class must be compiled in order to be used by another classClass must be compiled in order to be used by another class

Java ProgrammingJava Programming

Executable example (Welcome3.java)Executable example (Welcome3.java)public class Welcome3 {public class Welcome3 {

// main method begins execution of Java application// main method begins execution of Java application

public static void main( String args[] )public static void main( String args[] )

{{

System.out.println( "Welcome\nto\nJava\nProgramming!" );System.out.println( "Welcome\nto\nJava\nProgramming!" );

} // end method main} // end method main

} // end class Welcome3} // end class Welcome3

Circle.java example (Java vs C++)Circle.java example (Java vs C++)– Class HeaderClass Header

Instead of:Instead of:

class Circle : public Pointclass Circle : public Point

There is:There is:

public class Circle extends Point; public class Circle extends Point;

Java ProgrammingJava Programming

Circle.java example (Java vs C++)Circle.java example (Java vs C++)– Member variablesMember variables

Instead of:Instead of:

private:private:

double radius;double radius;

There is:There is:

private double radius; private double radius;

Java ProgrammingJava Programming

Circle.java example (Java vs C++)Circle.java example (Java vs C++)– ConstructorsConstructors

Instead of:Instead of:

Circle(int x,int y, double radiusValue);Circle(int x,int y, double radiusValue);

There is:There is:

public circle(int x,int y, double radiusValue);public circle(int x,int y, double radiusValue);

Note: “public” denotes that this may be constructed Note: “public” denotes that this may be constructed outside of the class declarationoutside of the class declaration

Java ProgrammingJava Programming

Circle.java example (Java vs C++)Circle.java example (Java vs C++)– Member functions (methods)Member functions (methods)

Instead of:Instead of:double getRadius()double getRadius()

{{return radius;return radius;

}}There is:There is:public double getRadius()public double getRadius()

{{return radius;return radius;

}}Note: “public” denotes that this may called outside of the classNote: “public” denotes that this may called outside of the class

Java ProgrammingJava Programming

Circle.java exampleCircle.java example//assume the compiler can find circle.class//assume the compiler can find circle.classImport javax.swing.JOptionPane; //allows GUI inputImport javax.swing.JOptionPane; //allows GUI inputpublic class CircleImplement {public class CircleImplement { // main method begins execution of Java application// main method begins execution of Java application public static void main( String args[] )public static void main( String args[] ) {{

Circle circ = new Circle(2,2,4.0); //constructorCircle circ = new Circle(2,2,4.0); //constructor double rad = circ.getRadius();//calls to method getRadiusdouble rad = circ.getRadius();//calls to method getRadius

JOptionPane.showMessageDialog(null,rad); //prints radiusJOptionPane.showMessageDialog(null,rad); //prints radius

} // end method main} // end method main

} // end class Welcome3} // end class Welcome3

Java ProgrammingJava Programming

Other notable differences between Java Other notable differences between Java and C++and C++– Strings are immutableStrings are immutable– No global variablesNo global variables– Memory allocation is not needed (ie no Memory allocation is not needed (ie no

pointers)pointers)– Garbage CollectionGarbage Collection– No operator overloadingNo operator overloading

JavadocJavadoc

Javadoc – gives info about a source fileJavadoc – gives info about a source file– Commandline – javadoc class.javaCommandline – javadoc class.java– Gives html documentation on the variables, Gives html documentation on the variables,

methods, inheritence, other comments, etc.methods, inheritence, other comments, etc.– Format – see handoutFormat – see handout

JDOMJDOM

JDOM – java class that enables XML JDOM – java class that enables XML construction and parsingconstruction and parsing– HandoutHandout

SaxonSaxon

Saxon - Saxon - http://saxon.sourceforge.net/saxon6.5.3/inhttp://saxon.sourceforge.net/saxon6.5.3/index.htmldex.html– Supports XSLT,XPath,XQuerySupports XSLT,XPath,XQuery– Can be used with JDOMCan be used with JDOM– CommandlineCommandline

java  classname sourcexml > destinationxmljava  classname sourcexml > destinationxml