***





Java!!

Why not just stick to C++?

Genesis

Creating a brand new language that is going to be popular, is HARD! There need to be great reasons why programmers will care..

James Gosling (@Sun, now Oracle) had really good reasons..

'Oak' == C++ minus the 'warts'.

Oak was renamed Java, and was released in 1995.

From Day One, PORTABILITY ("write once, run anywhere") has been a huge goal.

Genesis [cont'd]

"Java™ is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high-performance, multithreaded, dynamic, buzzword-compliant, general-purpose programming language."

Lots of claims in the above blurb!

How can Java achieve all this? Answer: "JVM". Very clever idea!

A 'virtual machine' (virtual processor) is what runs Java code.

Java is NOT the first language to run on a VM: UCSD Pascal had one (~1980), and Smalltalk too (~1984)!

JVM

The JVM is an example of a neat idea called a virtual machine, which is an abstraction. The JVM can be pictured like so:

Why use a VM?

* security: bugs/problems can be contained within the VM

* memory management

* portability: TYPES ARE IDENTICAL across processors

* threads management

What does the JVM execute?

The JVM executes "bytecode", which are compiled instructions meant for the VM:

Fun with JVM

As we will find out in a bit, all sorts of interesting variations exist, on the JVM -> underlying OS -> underlying hardware idea..

HelloWorld #1

Time to get our feet wet (or hands dirty)! Make sure you have (installed these):

After you download and bring up drjava, be sure to go to Edit->Preferences, and set the 'Tools.jar Location' to your OpenJDK 8's lib/tools.jar, like so.

We're going to be using drjava, or an online interface such as repl.it, to compile and run our Java code.

Here is the classic HelloWorld program, Java version - it is written as a class called HelloWorldApp. Save it to a file (THAT MUST BE) called HelloWorldApp.java, inside a HelloWorldApp folder [always make sure that the classname, filename and foldername match]:


class HelloWorldApp {
    // say "p s v m" over and over till you like the sound of it :)
    public static void main(String[] args) {
        System.out.println("Hello, Java World!"); // output
    }
}// HelloWorldApp

Compile and run it in the IDE (drjava):

NOTE: running a Java program is done via 'java nameOfClassWithAMain' ('java' invokes the JVM) - this is what the 'run' command in the IDE actually does; compiling a program is done using 'javac'. We compile, then run [just like with C++]. Compilation produces .class files (from .java sources), those are our 'executables' - we don't/shouldn't/can't directly run them, our JVM runs them for us.

HelloWorld #1 [cont'd]

We can create a .jar (compressed, .zip equivalent) file out of our HelloWorldApp.class file (plus a 'manifest' file), and execute the .jar file just by double-clicking on it. Real-world Java executables are packaged and distributed as jar files (eg. our drjava IDE).

To create a .jar file:

The above steps should generate a .jar file, which contains our .class file(s) plus a 'manifest'. This standalone file is our 'executable'. We can double-click on it to execute it, as if it is any other app on our machine [double-clicking will simply cause the jar file's 'manifest' to be examined to discover the class that has psvm, and the JVM to be launched, with our psvm class].

When we double-click on the .jar, we get nothing! That's because the program has no UI, just command-line output. We need to run it from a shell, via


java HelloWorldApp [or java -jar HelloWorldApp.jar]

HelloWorld #2

Instead of the command line version we had, here is a GUI-based one. No need to understand the internals right now - let us compile and run:

In drjava, create a .jar as before (create a new project, then do this), and double-click on it, that [too] puts up the same little UI as shown above. In Java, due to the inclusion of the 'Swing' library in the core language, UI is an integral part!

HelloWorld #3

Now let's look at an '**applet**' version of HelloWorld. An applet is bytecode that runs entire inside a browser (whereas the first three HelloWorld versions we ran were **app**s - these run outside the browser, ie. on a JVM atop the underlying OS).

Here is the source - compile it and run [using drjava - click on 'Run']. You will see the following result (FYI, the applet gets run by drjava's 'Applet Viewer' program):

Publishing apps and applets

We'd like our applets and apps to be runnable and launchable (respectively) from standard web pages - this makes it possible to distribute/share them easily, since our users won't need to manually download a .jar file and then click on it.

Applet - from a web page

Here is a page that contains an applet version of the .jar file we just created and ran.

Doh!! Error:

Applet - from a web page [cont'd]

Applets are subject to STRICT security (since Java 7), so we need to explicitly add the containing URL to our 'exception' ("OK to run") sites on our computer, via the Java Control Panel [on your PC/Mac]:

Applet - from a web page [cont'd]

After adding the URL (that contains the html page that contains our applet) to the exception list, and reloading:



Success!

NOTE that most browsers - Chrome, Brave, Firefox... simply will NOT run applets, no matter what we do (enable them in the browser, add URLs to the OS exception list). The ONLY browser on the PC that executes applets, is IE:

Moral of the story: if you want interactive graphics etc. in the browser, use JavaScript!!

Apps and applets: execution summary

An app is runnable **locally** like a regular program, ie. outside the browser. Additionally, you can make it available **online** via a link to download the app's jar file.

An applet is usually run **online** in a browser - users need to add the page's folder/site to their exception list (but applets have fallen out of favor lately, when it comes to being executed in the browser). Additionally you can run an applet **locally** outside the browser, using an 'applet viewer' program [eg. through an IDE].

Summary, again:
apps: run locally [outside the browser], or via browser links to download/launch.
applets: run in a browser [not a popular option], or locally in an applet viewer [outside the browser].

Notes on the JVM again - so many variations and mods..

'Traditionally', a JVM is meant to run atop an OS (eg. Win, Mac, Linux) or inside a browser (FF, Chrome, Safari, Opera, IE..).

A JVM can run on "bare metal", without an underlying OS.

JVM's bytecode can be translated to another VM's bytecode, eg. that of Dalvik VM (DVM). DVM is what Android runs!

JVM 'profiles' heavily used sections of code, and compiles them into native code (for faster execution).

JVM's classfiles can be packaged into binaries for Win, Mac, Linux etc, eg. http://www.duckware.com/jexepack/index.html.

It is possible to create an interpreter (REPL shell) for a JVM - drjava does this!

New languages can be designed to directly emit JVM bytecode, ie. run inside the JVM. It is also possible to reimplement existing languages (eg. Python) using Java - that way, programs in such languages (eg. Python) run on the JVM (via their interpreters), and can have access to all Java class libraries in addition to their own (which increases the usefulness of those languages). Read this page for a list of such languages; it is of interest to note that Scala and Groovy are JVM-based languages that are used in machine learning.