Download Tutorial_Java_Intro_Compile

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
Tutorial:
Java – Basic Introduction,
Compiling on Linux
SEEM 3460
1
Java


Java was created by Sun Microsystems, Inc. The
latest version is Java 8.
"write once, run anywhere“ (WORA)
compiled Java code can run on all platforms that
support Java without the need for recompilation.
Java virtual machine (JVM)


one of the most popular languages, particularly
for web applications, 9 million developers.
The language derives much of
its syntax from C and C++, but it has
fewer low-level facilities than either of them.
SEEM 3460
2
Java Program Structure


A Java source file contains at least a class
A Java application always contains a method
called main
public class HelloWorld {
public static void main(String []args){
System.out.println("Hello World");
}
}
SEEM 3460
3
Java Translation





The Java compiler translates Java source code
into a special representation called bytecode
Java bytecode is not the machine language for
any traditional CPU
Another software tool, called an interpreter,
translates bytecode into machine language and
executes it
Therefore the Java compiler is not tied to any
particular machine
Java is considered to be architecture-neutral
4
Java Translation
Java source
code
Java
compiler
Bytecode
interpreter
machine code
for target
machine 1
Bytecode
interpreter
machine code
for target
machine 2
Java
bytecode
5
Java Translation
6
Compiling and Running Java on
Unix

We can compile a Java program under Unix by:
cuse93> javac Countdown.java


If the compilation is successful, a bytecode file
called Countdown.class will be generated.
To invoke Java bytecode interpreter, we can:
cuse93> java Countdown
Three… Two… One.. Zero… Liftoff!
Houston, we have a problem.
wget http://www.se.cuhk.edu.hk/~seem3460/tutorial/lab/Countdown.java
SEEM 3460
7
//************************************************
// Countdown.java
//
// Demonstrates the difference between print and println.
//************************************************
public class Countdown
{
//----------------------------------------------------------------// Prints two lines of output representing a rocket countdown.
//----------------------------------------------------------------public static void main (String[] args)
{
System.out.print ("Three... ");
System.out.print ("Two... ");
System.out.print ("One... ");
System.out.print ("Zero... ");
System.out.println ("Liftoff!"); // appears on first output line
}
}
System.out.println ("Houston, we have a problem.");
SEEM 3460
8
Interactive Programs




Programs generally need input on which to
operate
The built-in Scanner class provides convenient
methods for reading input values of various
types
A Scanner object (called scan) can be set up to
read input from various sources, including the
user typing values on the keyboard
Keyboard input is represented by the System.in
object
wget http://www.se.cuhk.edu.hk/~seem3460/tutorial/lab/Echo.java
SEEM 3460
9
//***********************************************************
// Echo.java
//
// Demonstrates the use of the nextLine method of the Scanner class
// to read a string from the user.
//***********************************************************
import java.util.Scanner;
public class Echo
{
//----------------------------------------------------------------// Reads a character string from the user and prints it.
//----------------------------------------------------------------public static void main (String[] args)
{
String message;
Scanner scan = new Scanner (System.in);
System.out.println ("Enter a line of text:");
message = scan.nextLine();
}
}
System.out.println ("You entered: \"" + message + "\"");
SEEM 3460
10
Echo.java - Sample Execution

The following is a sample execution of
Echo.class
cuse93> java Echo
Enter a line of text:
This is a line
You entered: “This is a line”
SEEM 3460
11
Input Tokens





Unless specified otherwise, white space is used
to separate the elements (called tokens) of the
input
White space includes space characters, tabs,
new line characters
The next method of the Scanner class reads the
next input token and returns it as a string
Methods such as nextInt and nextDouble read
data of particular types
See GasMileage.java
wget http://www.se.cuhk.edu.hk/~seem3460/tutorial/lab/GasMileage.java
SEEM 3460
12
//*************************************************************
// GasMileage.java
//
// Demonstrates the use of the Scanner class to read numeric data.
//*************************************************************
import java.util.Scanner;
public class GasMileage {
//----------------------------------------------------------------// Calculates fuel efficiency based on values entered by the
// user.
//----------------------------------------------------------------public static void main (String[] args) {
int miles;
double gallons, mpg;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter the number of miles: ");
miles = scan.nextInt();
System.out.print ("Enter the gallons of fuel used: ");
gallons = scan.nextDouble();
mpg = miles / gallons;
}
}
System.out.println ("Miles Per Gallon: " + mpg);
SEEM 3460
13
GasMileage.java - Sample
Execution

The following is a sample execution of
GasMileage.class
cuse93> java GasMileage
Enter the number of miles: 34
Enter the gallons of fuel used: 17
Miles Per Gallon: 2.0
SEEM 3460
14
Development Environments

There are many programs that support the
development of Java software, including:








Sun Java Development Kit (JDK)
Sun NetBeans
IBM Eclipse
Borland JBuilder
MetroWerks CodeWarrior
BlueJ
jGRASP
Though the details of these environments differ,
the basic compilation and execution process is
essentially the same
SEEM 3460
15