Download File

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
Introduction to Java
What is Java?
• Java is an OOP (Object Oriented
Programming) language developed by
Sun Microsystems in 1990.
• It is widely used for the development of
Internet (and other distributed) software.
What is Java?
• Its features include neutrality (i.e. it doesn’t
depend on a specific type of machine),
security, robustness and multi-threading.
• It offers extensive application programmer
interfaces (APIs) including support for
graphical user interfaces (GUIs), utilities,
database access, networking etc.
What can you do with Java?
Amongst many other things, you can
• Produce stand-alone applications which run
directly on your computer.
• Produce applets on a world-wide web server
and embed them in your web pages to be run in
a browser such as Microsoft Internet Explorer.
• Produce servlets on a world-wide web server.
Unlike applets, these will run on the web server
machine; typically these are used to
communicate with web databases.
Creating simple Java applications
This involves the following steps:
• Writing the Java source code - saved (by you)
as Filename.java in a suitable folder. You can
use any editor, even NotePad, for this purpose.
• Compiling the Java source code using the
javac command in a command shell to create
Filename.class in the same folder.
• Running the program using the command java
Filename. (N.B. not
java Filename.class)
Example – Hello World
• Create a folder J:\java\Hello
• Using NotePad or EditPlus, type the following:
// this is my first java program
class Hello1
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
NB the first line is a Java comment
Example – Hello World
• Save this as Hello1.java in
J:\java\Hello
• Note that:
– Java is case sensitive. For example Hello1
is not the same as hello1.
– Spaces are significant. For example Hello 1
is not the same as Hello1.
– The file name must be the same as the class
name (class Hello1, file Hello1.java here)
A Java program consists of a number of
classes, which contain methods, which
contain statements
Here we have a single class
// this is my first java program
class Hello1
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
This class has a single method
// this is my first java program
class Hello1
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
This method has a single statement
// this is my first java program
class Hello1
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
This statement prints Hello world in the
command shell window
Compiling Hello1.java
• Open up a command shell
• Move to the folder where you saved
Hello1.java
• Type the command
javac Hello1.java
If you get error messages, go back to
Notepad and correct the typing errors,
save and try again
Running Hello1
• If / when you don’t get any error
messages, the java compiler has created
the bytecode file Hello1.class
• Type command
java Hello1
to run your application. Note that you must
NOT include the .class extension here
(See next slide)
Hello world with input
In Notepad or EditPlus, change the program:
// this is my second java program
class Hello2
{
public static void main(String[] args)
{
String name;
name = args[0];
System.out.println("Hello " + name);
}
}
and (important!) save it as Hello2.java. Compile and run it.
1 class Hello2
2 {
3
public static void main(String[] args)
4
{
5
String name;
6
name = args[0];
7
System.out.println("Hello " + name);
8
}
9 }
At line 5 we have introduced a String variable name.
Variables hold values (in this case a String like "Don" or
"Kate") within a program, and the values of the variables
can change as the program runs.
1 class Hello2
2 {
3
public static void main(String[] args)
4
{
5
String name;
6
name = args[0];
7
System.out.println("Hello " + name);
8
}
9 }
Line 6 is an assignment statement which takes the first
command line argument args[0] ("Don" or "Kate" or
whatever the user typed) and puts it into the variable name
on the left hand side.
1 class Hello2
2 {
3
public static void main(String[] args)
4
{
5
String name;
6
name = args[0];
7
System.out.println("Hello " + name);
8
}
9 }
At line 7 the expression "Hello " + name ‘glues’ (or
concatenates) String "Hello " and whatever is in the
name variable. This is then printed out in the command shell
window, so if name has the value "Don" the output is
Hello Don
Using EditPlus
• NotePad is fine for simple programs but provides
absolutely no support for Java.
• There are many Java development
environments like NetBeans for serious Java
programming but these are quite complicated.
• EditPlus provides quite a lot of help for the Java
programmer and is as easy to use as NotePad.
• See the notes for an explanation of how to use
EditPlus