Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java...

16
SEEM 3460 Tutorial: Java Basic Introduction, Compiling on Linux FU Zihao [email protected] 2019.11

Transcript of Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java...

Page 1: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

SEEM 3460

Tutorial:

Java – Basic Introduction,

Compiling on Linux

FU [email protected]

2019.11

Page 2: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

SEEM 3460

Contents

!

!

!

!

What is JavaJava Translation Compile Java on Linux Some Examples

1

Page 3: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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

Page 4: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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");

}

}

Page 5: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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

Page 6: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

5

Java Translation

Java source

code

Java

bytecode

Java

compiler

Bytecode

interpreter

machine codefor targetmachine 1

Bytecode

interpreter

machine codefor targetmachine 2

Page 7: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

6

Java Translation

Page 8: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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

Page 9: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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.");}

}

Page 10: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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

Page 11: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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 + "\"");}

}

Page 12: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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”

Page 13: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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

Page 14: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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);}

}

Page 15: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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

Page 16: Tutorial: Java Basic Introduction, Compiling on Linuxseem3460/tutorial/java/Java... · Java –Basic Introduction, Compiling on Linux FU Zihao zhfu@se.cuhk.edu.hk 2019.11. SEEM 3460

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