introduction and review of java: part 1 - university of...

Post on 03-May-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Introduction and Review of Java:

Part 1

Basic Introduction and Installation of Tools

2

The Java Programming Language

Note: these slides are based on figures and text obtained from:

http://java.sun.com/docs/books/tutorial/index.html

You can download the Java Tutorials from the Sun Download Center.

3

The Java Programming Language

– Simple

–Object Oriented

–Distributed

–Multithreaded

–Dynamic

–Architecture neutral

–Portable

–High performance

–Robust

– Secure

• The design requirements of the Java language are driven by the nature of the computing environments in which software must be deployed • Today’s computing environment is heterogeneous,

distributed network based.

4

Java Development Process 1. Source code is written in plain text files: names ending with .java

2. The javac compiler translates (compiles) the source files into .class files. Class files contain bytecodes: the machine language of the Java Virtual

Machine (Java VM).

Class files do not contain code native to your processor.

3. The java program runs your application on the VM, by interpreting the bytecodes and converting them into the native machine code of your machine.

javac.exe java.exe

5

Architecture Neutral and Portable

Because the Java VM is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris TM Operating System (Solaris OS), Linux, or Mac OS

6

The Java Platform Microsoft Windows, Linux, Solaris OS, and Mac OS

– Harware and Software based platforms.

Java

– Is a software based platform, consisting of

– The Java Virtual Machine.

– The Java Application Programming Interface (API).

– Large collection of libraries, stored in packages.

7

JAVA Development Tools In this course we use

– Notepad ++ to create source files

– The javac compiler to compile our programs.

– The java launcher to run them.

– The API documentation as the Java API reference.

– Primitive debugging tools.

– Such as old fashion analysis and print statements.

– Netbeans, a Java based Integrated Development Environment

– Breakpoint, stepping through statements, etc.

8

Application Programming Interface

Of course, your programs may contain your custom classes and methods, but a significant part of your programs will include pre-built classes and methods offered by Java.

The API provides the core functionality of the Java programming language.

Offers a wide array of useful classes ready for use in your own applications.

To view the API documentation online

– Google: java api 7

– http://docs.oracle.com/javase/7/docs/api/

9

Development Techniques • In this course, we will use two techniques to

develop java programs:

1. Command-line interface for developing programs

– Windows: command prompt

– Notepad ++ to create source files

2. Integrated Development Environment (IDE)

– Netbeans: an integrated development environment

– You can create and edit source code (*.java files); compile and build projects; debug projects; and run programs all within the IDE.

10

Command Line Technique

11

Check List 1. The Java SE Development Kit 7

(jdk1.7.0_67)

2. A text editor

– Such as Notepad++

• Homework: download and install the above.

12

Check List 1. Note: executable files are stored in the bin directory

13

Basic Development Steps 1. Create a source file

Use Notepad++

2. Open a command window

– Compile the source file, thus creating a *.class file

javac *.java

– Run the program

java *.class

14

Creating a Source File /*

Source File: HelloWorldApp.java

The HelloWorldApp class implements an application that

simply prints "Hello World!" to standard output.

*/

class HelloWorldApp {

public static void main(String[] args) {

// Display a string.

System.out.println("Hello World!");

}

}

15

… Using Notepad++

16

Compiling a Source File (1) • Bring up a “command prompt” window. You can do this:

• Start->All Programs->Accessories->Command Prompt

• Start, and then type cmd in the search window

17

Compiling a Source File (2) • Change your current directory to the directory where your file

is located.

18

Compiling a Source File (3) • Now compile the .java file by running the command:

javac HelloWorldApp.java

19

Running a Class File (1) • Now run the .class file by running the command: java helloWorldApp

20

Common Problems • System Problems

– Example: after you type: javac SourceFileName.java

the OS gives you a response:

'javac' is not recognized as an internal or external command, ...

• This means the OS can’t find the java.exe program.

– Solution – Provide the path to javac.exe explicitly on the command line

C:\Program Files\Java\jdk1.7.0_67\bin\javac.exe

– OR, store the path in the PATH environment variable:

21

Storing a Path • Control Panel, and

locate “Advanced System Settings”

• Click on the Environment Variables button

22

Storing a Path • Ensure the path to

javac.exe is stored in the PATH environment variable for both User and System variables

• Click on PATH, then click on Edit…

• Add path (C:\Program

Files\Java\jdk1.7.0_67\bin) to current path

23

Syntax Problems

• Example:

testing.java:14: `;' expected. System.out.println("Input has " + count + " chars.")

^

1 error

24

Compiler Checking Problems • Semantic Errors, e.g.,

testing.java:13: Variable count may not have been initialized. count++

^

1 error

25

Runtime Errors • Error Messages from the JVM, e.g.,

Exception in thread “main”

java.lang.NoClassDefFoundError: HelloWorldApp

If you receive this error, java cannot find your bytecode file, HelloWorldApp.class.

26

IDE Technique

27

Check List 1. The Java SE Development Kit 7

(jdk1.7.0_67)

2. Netbeans IDE

– Download and install NetBeans IDE 8.0

Homework: download and install Netbeans

28

Basic Development Steps 1. Using Netbeans IDE:

– Create project and source code (*.java files)

– Compile and build your project

– Debug your project

– Run your project

29

See Netbeans Tutorial http://docs.oracle.com/javase/tutorial/getStarted/cupojava/netbeans.h

tml

30

Brief Intro To Java Language

31

Source Code Comments The Java programming language supports three kinds of comments:

/* text */

– The compiler ignores everything from /* to */.

/** documentation */

– The javadoc tool uses doc comments when preparing automatically generated documentation.

// text

– The compiler ignores everything from // to the end of the line.

32

The HelloWorldApp Class Definition

class HelloWorldApp {

public static void main(String[] args) {

System.out.println(“Hello World!”);

// Display the string “Hello World!”.

}

}

The most basic form of a class definition is:

class name {

. . .

}

The keyword class begins the class definition for a class named name, and the code appears between the opening and closing curly braces.

33

The main Method In Java, every application must contain a main method whose signature is:

public static void main(String[] args)

– The main method is the entry point for your application.

– The main method accepts a single argument: an array of elements of type String.

– This array is the mechanism through which the runtime system passes information to your application.

– Each string in the array is called a command-line argument.

34

Note: • When you run the helloWorldApp.class file, you won’t

add any command line arguments, and so none were passed to the main method.

• In this case the application does not require any command line arguments, but other application may.

35

Printing class HelloWorldApp {

public static void main(String[] args) {

System.out.println(“Hello World!”);

// Display the string “Hello World!”.

}

}

– System.out.println("Hello World!"); uses the System class from the core library (Java API) to print the “Hello World!” message to standard output.

top related