What Is A Java Program?

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

2

What Is a Java Program?

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Objectives

After completing this lesson, you should be able to:


• Contrast the terms “platform-dependent” and “platform-
independent”
• Describe the purpose of the JVM
• Explain the difference between a procedural program and
an object-oriented program
• Describe the purpose of javac and java executables
• Verify the Java version on your system
• Run a Java program from the command line

2-2 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Topics

• Introduction to computer programs


• Introduction to the Java language
• Verifying the Java development environments
• Running and testing a Java program

2-3 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Purpose of a Computer Program

A computer program is a set of instructions that run on a


computer or other digital device.
• At the machine level, the program consists of binary
instructions (1s and 0s).
– Machine code
• Most programs are written in high-level code (readable).
– Must be translated to machine code

2-4 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Translating High-Level Code to Machine Code

Solaris OS
C Compiler
Solaris OS
Binary

Linux
C Compiler
Linux
C Binary
Code

Microsoft Windows
C Compiler Microsoft Windows
Binary

2-5 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Linked to Platform-Specific Libraries

Project
Libraries
Solaris OS
C Compiler
Solaris OS Solaris OS
Binary Executable

Project
Libraries

Linux
C Compiler
Linux Linux
Binary Executable

Project
Libraries

Microsoft Windows
Microsoft Windows C Compiler Microsoft Windows
Binary Executable

2-6 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Platform-Dependent Programs

Solaris OS Workstation

Solaris OS
Executable

Linux Linux Workstation


Executable

Microsoft Windows
Executable Microsoft Windows Workstation

2-7 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Topics

• Introduction to computer programs


• Introduction to the Java language
• Verifying the Java development environment
• Running and testing a Java program

2-8 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Key Features of the Java Language

Some of the features that set Java apart from most other
languages are that:
• It is platform-independent
• It is object-oriented

2-9 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Java Is Platform-Independent

Project
Libraries

Java Compiler

Java Code Java


Bytecode

2 - 10 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Java Programs Run In a Java Virtual Machine
JRE

Solaris OS
Java Workstation
Bytecode
JRE
(.class file)

Linux
Workstation

JRE
Java Virtual
Machine
(JVM)

Microsoft Windows
Workstation

2 - 11 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Procedural Programming Languages

• Many early programming languages


1 Step 1
followed a paradigm called Procedural
Programming.
• These languages use a sequential 2 Step 2

pattern of program execution.


3 Step 3
• Drawbacks to procedural programming:
– Difficult to translate real-world use cases
to a sequential pattern 4 Step 4

– Difficult to maintain programs


– Difficult to enhance as needed 5 Step 5

2 - 12 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Java Is an Object-Oriented Language

• Interaction of objects
• No prescribed sequence
• Benefits:
– Modularity
– Information hiding
– Code reuse
– Maintainability

2 - 13 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Topics

• Introduction to computer programs


• Introduction to the Java language
• Verifying the Java development environment
• Running and testing a Java program

2 - 14 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Verifying the Java Development Environment

1. Download and install the Java Development Kit (JDK) from


oracle.com/java.
2. Explore the Java Help menu.
3. Compile and run a Java application by using the command
line.

2 - 15 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Examining the Installed JDK (Linux Example):
The Tools

ime
Runt
pile
Com
r

PATH points here

2 - 16 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Examining the Installed JDK (Windows Example):
The Libraries

2 - 17 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Topics

• Introduction to computer programs


• Introduction to the Java language
• Verifying the Java development environment
• Running and testing a Java program

2 - 18 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Compiling and Running a Java Program

is a ss
e is l a is
od jav de ac. c
. e) . l e
c o fi the
r ce a . c e c jav i
a
s cod a
s
s by e.
sou in . our by t
ul byte
l
c ed tim
he ned file e s led s
re e ( he u t n
T tai Th mp
i e il T e
x
c
a ru
n
co Th f e av
co j

.java javac .class java

2 - 19 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Compiling a Program

1. Go to the directory where the source code files are stored.


2. Enter the following command for each .java file you want
to compile.

• Syntax:
javac SayHello.java

• Example:
javac SayHello.java

2 - 20 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Executing (Testing) a Program

1. Go to the directory where the class files are stored.


2. Enter the following for the class file that contains the main
method:
• Syntax:
java <classname>

• Example: Do not specify .class.


java SayHello

• Output:
Hello World!

2 - 21 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Output for a Java Program

A Java program can output data in many ways. Here are some
examples:
• To a file or database
• To the console
• To a webpage or other user interface

2 - 22 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Exercise 2-1

• From a Terminal window, enter java –version to see


the system’s Java version.
• Look for SayHello.java in:
/labs/02-GettingStarted/Exercises/Exercise1
• Compile it: javac SayHello.java
• Run the resulting class file: java SayHello

Did you see the output?

2 - 23 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Quiz

Which of the following is correct? (Choose all that apply.)


a. javac OrderClass
b. java OrderClass
c. javac OrderClass.java
d. java OrderClass.java

2 - 24 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Summary

In this lesson, you should have learned how to:


• Describe the distinction between high-level language and
machine code
• Describe what platform-independence means
• Describe how a Java program is compiled and to what
format
• Explain what it means to say that Java is an object-
oriented language
• Determine the version number of a Java install
• Use the javac tool to compile Java source code and the
java tool to run or test your program

2 - 25 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

You might also like