0% found this document useful (0 votes)
3 views32 pages

21 - Java Platform

The document outlines an in-class group activity for a course on Object-Oriented Programming, where students will be divided into groups to discuss and present key concepts from provided slides. It covers the importance of platform and portability in programming, particularly focusing on Java's capabilities for source code, CPU, and OS/GUI portability. Additionally, it highlights the advantages and downsides of Java's portability compared to C and C++.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views32 pages

21 - Java Platform

The document outlines an in-class group activity for a course on Object-Oriented Programming, where students will be divided into groups to discuss and present key concepts from provided slides. It covers the importance of platform and portability in programming, particularly focusing on Java's capabilities for source code, CPU, and OS/GUI portability. Additionally, it highlights the advantages and downsides of Java's portability compared to C and C++.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object-Oriented Programming

CSC 2306
Instructor: Dr. Oumaima Hourrane
Email: [Link]@[Link]
School of Science and Engineering
In-Class Group Activity

Understand key concepts from the slides through discussion and peer teaching.

How It Works:
Group Assignments:
1. You’ll be divided into 4 groups.
● Group 1: Slides 3–8 + Slide 30
2. Each group will review a specific set of slides.
● Group 2: Slides 9–15
3. Discuss together to understand the content.
● Group 3: Slides 16–23
4. Prepare a quick summary or example to share with the class.
● Group 4: Slides 24–29
5. Each group will present what they understood.

You will have 20 minutes to prepare, and each group will have 8-10 minutes to present.

OOP - CSC 2306 2


The Java
Unit 10 Platform & Portability

3
What is a platform?

A platform is a foundational environment or framework that supports the running and


development of software applications.

It typically encompasses the underlying hardware, the operating system, and sometimes
additional layers, like virtual machines or frameworks, that provide consistent services,
libraries, and standards.

Operating
Windows OS Linux MacOS
System
Platform
Processor Intel CPU AMD Mac CPU

OOP - CSC 2306 4


Portability

Portable software allows developers to support more platforms (platform


independence), which leads to a larger base of potential customers.

OOP - CSC 2306 5


Portability – Definition

- Portability refers to the ability to run a program on different machines.

- Running a given program on different machines can require different amounts


of work (for example, no work whatsoever, recompiling, or making small
changes to the source code).

- When people refer to applications as portable, they usually mean the


applications run on different types of machines with no changes (such as
recompilation or tweaking the source code).

OOP - CSC 2306 6


Why Portability?

- Portable software makes it possible for developers to run their programs on


many different types of devices and operating systems, which means they
can reach more customers.

- However, if C or C++ programs are not created with portability in mind right
from the start, it becomes much harder to actually get them working on
different machines.

OOP - CSC 2306 7


Java is portable

- Java supports platform independence, one of the primary needs of internet-based


programming.

OOP - CSC 2306 8


Java portability

Java makes code portable in three main ways:

1. Source code portability: You can write Java code once and easily reuse it across
different platforms without changing the code itself.
2. CPU architecture portability: Java’s bytecode can run on many different types of
processors without having to be rewritten or recompiled specifically for each type.
3. OS/GUI portability: Java programs can work across various operating systems and
graphical user interfaces, maintaining a consistent look and functionality.

OOP - CSC 2306 9


Source code
Unit 10
portability

10
Source code portability

- C and C++ semantics are loose.

OOP - CSC 2306 11


Source code portability

- C and C++ semantics are loose.

- Even though the rules for how C and C++ code is written (its syntax) are clear,
the rules for how that code actually behaves (its semantics) are not as strict.
- Because of this, the same piece of C or C++ code can produce different
results on different computers, operating systems, or compilers, and can
even behave differently on the same system when compiled with
different settings.

OOP - CSC 2306 12


Source code portability in Java

- Java defines more behavior (semantics) than C and C++.

OOP - CSC 2306 13


Source code portability in Java

- Java defines more behavior (semantics) than C and C++.


- Java sets clearer rules for how programs should behave compared to C and C++.
- Java’s automatic garbage collection ensures that memory is not freed until it’s
truly no longer in use.
- there are no uninitialized variables,
- integers are always 32-bit,
- and floating-point math follows the IEEE754 standard.

- By having these consistent rules, Java programs act more predictably no matter where
they run or who made the Java implementation.

OOP - CSC 2306 14


Compile and Run java programs (command line)
- All source code has the .java extension
- First phase of translation:
- Compile java code using the compiler javac
- For most languages, compilation produces machine code
- Java compilation produces bytecode (.class)
- Intermediate code readable by the Virtual Machine JVM
- Portable across different platforms (that have a JVM implemented)
- Second phase of translation:
- To use the java program to run the application with an instance of the Java Virtual
Machine of your platform

OOP - CSC 2306 15


Compile and Run java programs (command line)

OOP - CSC 2306 16


CPU
Unit 10
portability

17
CPU portability

- Object code, which consists of the low-level, binary instructions that a CPU executes,
typically only works on one type or "family" of processors (for example, Intel x86 chips).

- This means that an object file created for one processor family won’t run on a different
kind of CPU.

OOP - CSC 2306 18


CPU portability

- This means that an object file created for one processor family won’t run on a different
kind of CPU.

- Because the binary instructions in object code are specific to a certain processor type
(architecture), object code created on one kind of machine will only run on that same
kind of machine.

OOP - CSC 2306 19


Java CPU portability

- The current Java compilers are different.


- Instead of producing machine code for a specific type of CPU, they produce
“bytecode,” which is code meant for a hypothetical machine that doesn’t actually
exist.
- The bytecode generated is run by the Java Virtual Machine and not the physical
processor.
- Because the JVM provides a consistent environment on each platform, the same
compiled Java program can run on different types of computers and operating systems
without needing to be changed.
- This is what makes Java “architecture-neutral.” Each platform has its own JVM,
but the bytecode always runs the same way, allowing a single program to work
everywhere.

OOP - CSC 2306 20


Java has 2 stages of translation –
compilation of source code into bytecode

Development Phase I
Environment Compilation

OOP - CSC 2306 21


Java has 2 stages of translation –
compilation of source code into bytecode

Development Phase I
Environment Compilation

Production Phase II
Environment Interpretation

OOP - CSC 2306 22


Development vs. Production environments

What is Java Runtime Environment (JRE)?


- It is the software that actually runs your Java programs. It provides the tools
needed to load and run Java code, and it uses a special memory area called the
heap to create and manage Java objects.
- The JRE is also involved when you use tools like the Java Debugger (JDB) to
find and fix problems in your code.

What is Java Development Kit (JDK)?


- It includes everything in the JRE plus additional tools for building and testing
Java applications and applets. Because it depends on the underlying operating
system, you need a version of the JDK that is specifically made for your
computer’s platform (like macOS, Unix, or Windows).

OOP - CSC 2306 23


Packaging and distributing Java applications

Bundling the application into an executable JAR file.


- A JAR file is like a zip folder that can include many files and folders. However, JAR
files have extra features that make them ideal for sharing Java programs, such as
the ability to add digital signatures and use enhanced compression.
- This makes it easier and more secure to distribute Java applications.

Packaging: Create the JAR file in the development environment:


- In netbeans, a JAR file containing your project is created inside the
PROJECT_HOME/dist folder every time you compile your code.

In the production environment:


- You should have the JRE (Java Runtime Environment) installed
- Navigate to the project folder and double-click the jar file.
OOP - CSC 2306 24
OS and GUI
Unit 10
portability

25
OS and GUI portability

● After eliminating the semantic problems in C and C++ and the CPU porting
problems, programmers still must deal with the different operating system and
different GUI API calls.

OOP - CSC 2306 26


OS and GUI portability

● After eliminating the semantic problems in C and C++ and the CPU porting
problems, programmers still must deal with the different operating system and
different GUI API calls.
● Just like the JVM presents a virtual CPU, the Java libraries present a virtual
OS/GUI.
○ Every Java implementation provides libraries implementing this virtual
OS/GUI.
○ This means Java programs that use these libraries, such as Swing for the
graphics, can easily run on different types of computers without needing
changes.
OOP - CSC 2306 27
[Link] versus [Link]

AWT (Abstract windows toolkit)


- one of the original Java libraries for
creating graphical user interfaces
(GUIs).
- AWT is a thin layer of bytecode on top
of the OS.
- Native Heavyweight components (not
portable), i.e., platform dependent.
- Package is under [Link]

OOP - CSC 2306 28


[Link] versus [Link]

AWT (Abstract windows toolkit) Swing


- one of the original Java libraries for - Swing components are made in
creating graphical user interfaces purely java
(GUIs). - Swing components are portable or
- AWT is a thin layer of bytecode on top lightweight as they are platform
of the OS. independent.
- Native Heavyweight components (not - Swing bytecode is much larger.
portable), i.e., platform dependent. - Swing also has very much richer
- Package is under [Link] functionality.
- Package is under [Link]
OOP - CSC 2306 29
Why does Swing belong to javax package?

- Originally, everything that was part of the standard API was part of the java
package.
- Everything that was NOT part of the standard API was released under the
package name javax.
- In javax, the “x” stands for extension.
- Over time the extensions that were released as javax, become integral to the
Java API.
- However, moving the extension from the javax package to the java
package would be too cumbersome and would end up breaking a bunch
of existing software.
- Hence, eventually it was decided that the javax packages would become
part of the standard API.

OOP - CSC 2306 30


Downside of Java portability

- Unfortunately, the features that make Java so portable have a downside. Java
assumes a:
- 32-bit machine and
- IEEE754 floating-point math.

- Machines that don’t fit this model, like 8-bit microcontrollers cannot run Java.
- For this reason, we should expect C and C++ to be used on more
platforms than the Java language.

OOP - CSC 2306 31


Downside of Java portability

References
- Java’s three types of portability
- About the Java technology
- The Java platform components

OOP - CSC 2306 32

You might also like