0% found this document useful (0 votes)
4 views26 pages

Lesson_02

This document provides an overview of Java programming, including the distinction between platform-dependent and platform-independent programs, and the role of the Java Virtual Machine (JVM). It explains the differences between procedural and object-oriented programming, and outlines how to verify the Java development environment, compile, and run Java programs from the command line. Key features of Java, such as its platform independence and object-oriented nature, are also discussed.

Uploaded by

wweellddeerr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

Lesson_02

This document provides an overview of Java programming, including the distinction between platform-dependent and platform-independent programs, and the role of the Java Virtual Machine (JVM). It explains the differences between procedural and object-oriented programming, and outlines how to verify the Java development environment, compile, and run Java programs from the command line. Key features of Java, such as its platform independence and object-oriented nature, are also discussed.

Uploaded by

wweellddeerr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

2

What Is a Java
Program?

Copyright © 2019, 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
• Compile and run a Java program from the command line

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

• Introduction to computer programs


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

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 2-3
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

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

Solaris OS
C Compiler

Solaris OS Binary

Linux
C Compiler
Linux Binary
C Code

Microsoft Windows
C Compiler
Microsoft Windows
Binary

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

Project
Libraries
Solaris OS
C Compiler

Solaris OS Binary Solaris OS Executable

Project
Libraries

Linux
C Compiler
Linux Binary Linux Executable

Project
Libraries

Microsoft Windows
Microsoft Windows C Compiler Microsoft Windows
Binary Executable

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

Solaris OS Executable Solaris OS Workstation

Linux Executable Linux Workstation

Microsoft Windows Microsoft Windows Workstation


Executable

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

• Introduction to computer programs


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

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 2-8
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

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

Java Code

Library Java Compiler


JARs
Java
Bytecode

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


Java Programs Run In a Java Virtual Machine
JRE

Solaris OS
Java
Workstation
Bytecode
(.class file)
JRE

Linux
JRE Workstation

Java Virtual
Machine
(JVM)
Microsoft Windows
Workstation

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


Procedural Programming Languages

1 Step 1
• Many early programming languages followed a
paradigm called Procedural Programming.
• These languages use a sequential pattern of program 2 Step 2

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

– Difficult to enhance as needed

5 Step 5

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


Java Is an Object-Oriented Language

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

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


Topics

• Introduction to computer programs


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

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


Verifying the Java Development Environment

1. Download and install the Java Development Kit (JDK) from oracle.com/java.
2. Examine the environment.
3. Compile and run a Java application by using the command line.

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


Examining the Installed JDK: The Tools

PATH
points
here

Runtime

Compiler

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


Topics

• Introduction to computer programs


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

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


Compiling and Running a Java Program

is is
de e c. i s is
o
c in d
o v a l t le le y
c fi
r
e
c ed le . e y ja e su s fi ). ss db
u in fi c
r b r s e l a te v a
o a a u e a o d c u ja e.
e
s nt v so iled Th .c tec
l e c
o
Th c .j
a
h e p a by Th exe he tim
a T m ( t n
co ru

.java javac .class java

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


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 <source file>

• Example:

javac SayHello.java

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


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!

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


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

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


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?

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


JDK 11: Launch Single-File Source-Code Programs
Circle.java
Benefits: public class Test {
• Skip the compilation "ceremony". public static void main(String args[]) {
double area = Circle.findArea(7.5);
• Run a program with one quick command:
System.out.print("Area of circle=" +area);
java <source file> }
}
Requirements:
• Write the entire program as single source file. public class Circle {
public static double findArea(double radius){
• The file may contain any number of classes.
return Math.PI * radius * radius;
• The top-most class declares a main method. }
}
Use Cases:
• Experiment quickly to learn Java. java Circle.java
• Write small utility or "shebang" files. Area of circle=176.714…

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


Exercise 2-2

• In a terminal window:
• Look for Circle.java in:
/labs/02-GettingStarted/Exercises/Exercise2
• Run the file: java Circle.java
• Did you see the output?
• Do you see any bytecode file produced?

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


Quiz Q
Which of the following is correct? (Choose all that apply.)
a. javac OrderClass
b. java OrderClass
c. javac OrderClass.java
d. java OrderClass.java

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


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
• Compile and run a Java program from the command line

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 2 - 26

You might also like