Introduction to Java Tonga Institute of Higher Education.

23
Introduction to Java Tonga Institute of Higher Education

Transcript of Introduction to Java Tonga Institute of Higher Education.

Page 1: Introduction to Java Tonga Institute of Higher Education.

Introduction to Java

Tonga Institute of Higher Education

Page 2: Introduction to Java Tonga Institute of Higher Education.

Programming Basics

Program – A set of instructions that a computer uses to do something.

Programming / Develop – The act of creating or changing a program

Programmer / Developer – A person who makes a program

Run / Execute – The act of using a program

Every program was created by someone Computers use special languages Programmers use special languages to create or change a

program

Page 3: Introduction to Java Tonga Institute of Higher Education.

Machine Language

Machine language – A language understood by computers

When programs are run, machine language is used

Machine languages are almost impossible for humans to understand

Every operating system (OS) has its own machine language Windows Linux Macintosh

Hardware

Machine Language

High Level Language

C++ Java VB .NET

Page 4: Introduction to Java Tonga Institute of Higher Education.

High Level Languages

High Level Language - A programming language that is understandable by people

This enables a programmer to write programs

High level languages must be translated into machine language before running on a computer

Hardware

Machine Language

High Level Language

C++ Java VB .NET

Page 5: Introduction to Java Tonga Institute of Higher Education.

Compilers and Interpreters

There are two main ways to change programs written in a high level language to machine language:

1. Use a compiler

2. Use an interpreter

Source Code – Code written in a programming language by a developer.

Page 6: Introduction to Java Tonga Institute of Higher Education.

Compilers Compiler – A program that transforms code from

one format to another

Source code

Linux compiler

Windows compiler

Macintosh compiler

Linux code

Macintosh code

Windows code

Machine LanguageHigh Level Language

Windows

Linux

Macintosh

Page 7: Introduction to Java Tonga Institute of Higher Education.

Compilers vs. Interpreters

Advantages Programs run faster

Disadvantages Platform dependent - Programs only work on a

specific operating system. Windows Linux Macintosh

Compiling a large program may take a long time

Page 8: Introduction to Java Tonga Institute of Higher Education.

Interpreters

Interpreter – A program that translates and executes code

Usually, interpreters translate and execute source code.

Source Code Interpreter

High Level Language Code for Windows, Linux and Macintosh

Machine Language

Windows,Linux and Macintosh

Page 9: Introduction to Java Tonga Institute of Higher Education.

Interpreters vs. Compilers

Advantages Platform Independent. Don’t need to compile anything.

Disadvantages The interpreter program must be installed on the

computer that the program will run on Slower execution You should only use services available on all

platforms. Example: Windows has a cool sound library, but you can’t

use it because it won’t run on Macintosh

Page 10: Introduction to Java Tonga Institute of Higher Education.

Review

Give a short description of:Machine LanguageHigh Level Language

Give a short explanation of how an compiler is different from an interpreter

Page 11: Introduction to Java Tonga Institute of Higher Education.

Introduction to Java

Java is a programming language created by Sun Microsystems.

Java.sun.com Many programmers use Java Java is an object oriented language.

Page 12: Introduction to Java Tonga Institute of Higher Education.

Java Setup

Steps to install Java Required

1. Install the Java Software Development Kit (SDK) Find it at http://java.sun.com. Java 2 Standard Edition

Not Required but good1. Install Java Documentation

2. Install Integrated Development Environment (IDE) IDE – A program that helps you to write code

Page 13: Introduction to Java Tonga Institute of Higher Education.

Demonstration

Sun WebsiteJava DocumentationJava IDE

Page 14: Introduction to Java Tonga Institute of Higher Education.

How Does Java Work? Java uses a compiler and interpreter! You can compile the program once and run it on

each platform with an interpreter

Source code

Java Compiler

Byte Code

Windows

Macintosh

Linux

Java Interpreter

Windows

Linux

Macintosh

Page 15: Introduction to Java Tonga Institute of Higher Education.

How to Create a Java Program

1. Create a source file.

2. Use the Java compiler to create byte code.

3. Run the byte code using the Java interpreter.

Page 16: Introduction to Java Tonga Institute of Higher Education.

Create a Source File

Write Java source code and save it in a file

Use a text editor or Integrated Development Environment (IDE).

Filename extension: .java

Page 17: Introduction to Java Tonga Institute of Higher Education.

Demonstration

Create a source file

Page 18: Introduction to Java Tonga Institute of Higher Education.

Use the Java Compiler to Create Byte Code Source code is compiled by a Java compiler to byte

code. Byte Code

A file that can run on any computer that has a Java Interpreter Filename extension: .class

Compiler program: javac.exe

Java Compile

r

Javac.exe

Source Codevoid main () … ??this$0?!...

Byte Code

.java .class

Page 19: Introduction to Java Tonga Institute of Higher Education.

Use the Java Compiler to Create Byte Code Important Details

Javac.exe <filename>.java Ex: Javac HelloWorld.java

A .class file is made for every class in the file. Common Errors

Unable to find the Compiler - Something is wrong with your Java setup. Windows can’t find the Java compiler.

Is your access to Javac.exe and Java.exe setup properly? Invalid Flag – One of the inputs are wrong.

Did you include the .java extension? Are you using the right file?

Page 20: Introduction to Java Tonga Institute of Higher Education.

Demonstration

Compiling Source Code

Page 21: Introduction to Java Tonga Institute of Higher Education.

Run the Byte Code Using the Java Interpreter Java Virtual Machine (JVM) – A program which interprets Java

programs that have been compiled into byte-code and usually stored in a ".class" file.

Interpreter performs actions Creates windows Prints Etc.

Interpreter program: java.exe

Java Interpret

er

Java.exe

Byte Code??this$0?!...

.class

Page 22: Introduction to Java Tonga Institute of Higher Education.

Run the Byte Code Using the Java Interpreter Important Details

Java.exe <class name> Ex: Java HelloWorld

The class name must have the correct case!

Common Errors NoClassDefFoundError – The class can’t be found.

Is your class in the correct directory? Are you using the correct case?

Page 23: Introduction to Java Tonga Institute of Higher Education.

Demonstration

Running Byte Code