1/15
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Virtual Machine
a software emulation of a physical computer that runs programs in an isolated environment
VMs handle memory allocation and garbage collection, reducing the risk of memory-related errors
execution within a VM provides a layer of isolation
Virtual Machine Based Languages
Java (JVM), C# (CLR), Python (Cpython, Jython, IronPython), Kotlin, JS, Ruby…
instead of directly running on hardware, VM-based languages run on a software-based virtual environment
code written in VM-based languages can run on any system with the appropriate VM installed
Java
A modern, platform independent, object-oriented programming language
Developed by SUN microsystems in 1995 (James Gosling). Java is now owned by Oracle
Overarching design goal: simplicity and reuse
Originally designed for embedded devices… the first envisioned application was the Java toaster
Next it was the web language of choice (applets)
Since evolved into a programming language of choice for high reliability and good performance. Now commonplace in:
Enterprise Systems (IBM/Oracle)
Mobile Devices (Android)
Embedded Devices (SmartTVs, IoT)
Financial, Medical, and Automotive domains
Open
Java is a modern, platform independent, object-oriented programming language
Open standards allow interoperation and promote innovation
Abstract
Java is a modern, platform independent, object-oriented programming language
Java uses a virtual machine to abstract over device operating systems
Java Virtual Machine (JVM)
Abstracts over a device hardware
Processor
Memory
Input / Output
Graphical Interfaces…
Contains a virtual computer processor
Virtual Computer Processor
Executes its own machine language known as bytecode
Very simple instructions, such as add, multiply, compare (c.f. machine language)
Java programs are compiled into bytecode by the developer
The JVM interprets these into whatever the hardware understands at run time
Java bytecode is an example of an intermediate language (neither something you write in, nor something that is directly executed by a computer)
Java Machine Language - Bytecode
C++ vs Java
I ran a Bubble Sort algorithm on an array of 13 integers…
Results shown in nanoseconds (1000 nsec = 0.001 msec)
Java: 4279 nsec
C++: 666 nsec (6 times faster)
Language
based on the syntax of C and C++
public class HelloWorld
{
public static void main(String[] arg)
{
System.out.println("Hello
World");
}
}
Classes
Every Java program is made up of one or more
Java’s unit of modularity… they define objects
The class keyword defines a unique name for the class you’re writing. Curly braces define the code that is part of that class
One class per file, with filename matching as the class name
public class HelloWorld
{
}
Methods
The main method defines the start point for your program
This method always returns void in Java (they have no return value)
Parameter is an array of strings (command line arguments, like C)
Methods are like functions in C
They are blocks of code that have names, parameters and return types
public static void main(String[] args)
{
//say what you want it to do
}
Comments
A single line comment begins with //
All text on the line after the // characters are ignored
Java also supports the C style /* */ comment blocks
//la la la
Statements
Method invocation (like calling a function in C) statements end with a semicolon, as they do in C and C++
For example:
System.out is the name of an object (the console output stream)
println is the name of a method – note there’s also a print method
System.out.println("Hello World");
Code Blocks
The end of the method and end of the class (respectively)
As with C, it's very important to maintain good code indentation
In Java, methods are always defined inside classes
{
{
}
}
Compiling a Java Program
Create a text file with your favourite editor (e.g. VS Code)
Filename must match the name of the class it contains
Filename must end in .java
Open a command line prompt
shell in Unix
cmd in Windows
Change directory to the location of your file, then compile your program into bytecode:
javac HelloWorld.java
Now start a Java virtual machine that interprets your program:
java HelloWorld