Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java...
Transcript of Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java...
SEEM 3460
Contents
!
!
!
!
What is JavaJava Translation Compile Java on Linux Some Examples
1
SEEM 3460 2
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.
Java
SEEM 3460 3
A Java source file contains at least a class
A Java application always contains a method called main
Java Program Structure
public class HelloWorld {
public static void main(String []args){
System.out.println("Hello World");
}
}
4
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
5
Java Translation
Java source
code
Java
bytecode
Java
compiler
Bytecode
interpreter
machine codefor targetmachine 1
Bytecode
interpreter
machine codefor targetmachine 2
6
Java Translation
SEEM 3460 7
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 8
//************************************************// 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 9
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.inobject
wget http://www.se.cuhk.edu.hk/~seem3460/tutorial/lab/Echo.java
SEEM 3460 10
//***********************************************************// 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 11
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 12
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 13
//*************************************************************// 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 14
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
wget http://www.se.cuhk.edu.hk/~seem3460/tutorial/lab/GasMileage.java
SEEM 3460 15
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
Development Environments