35
1 Introduction and Review of Java: Part 1 Basic Introduction and Installation of Tools

Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

1

Introduction and Review of Java:

Part 1

Basic Introduction and Installation of Tools

Page 2: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 3: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 4: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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

Page 5: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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

Page 6: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 7: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 8: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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/

Page 9: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 10: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

10

Command Line Technique

Page 11: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 12: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

12

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

Page 13: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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

Page 14: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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

}

}

Page 15: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

15

… Using Notepad++

Page 16: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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

Page 17: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

17

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

is located.

Page 18: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

18

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

javac HelloWorldApp.java

Page 19: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

19

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

Page 20: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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:

Page 21: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

21

Storing a Path • Control Panel, and

locate “Advanced System Settings”

• Click on the Environment Variables button

Page 22: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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

Page 23: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

23

Syntax Problems

• Example:

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

^

1 error

Page 24: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

24

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

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

^

1 error

Page 25: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 26: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

26

IDE Technique

Page 27: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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

Page 28: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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

Page 29: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

29

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

tml

Page 30: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

30

Brief Intro To Java Language

Page 31: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 32: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 33: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 34: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.

Page 35: Introduction and Review of Java: Part 1 - University of Manitobaece.eng.umanitoba.ca/undergraduate/ECE3740/Lecture Slides... · 2014-09-10 · JAVA Development Tools In this course

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.