Java, Scala and Koltlin, general-purpose application building environments

Web Technology Digital Transformation Artificial Intelligence Natural Language Processing Semantic Web Deep Learning Online Learning Reinforcement Learning  Chatbot and Q&A User Interface Knowledge Information Processing Reasoning Machine Learning Programming Navigation of this blog

About JVM

Java, which runs on the JVM, was released by Sun Microsystems in 1996, and when Sun Microsystems merged with Oracle in 2010, the copyright was transferred to Oracle, which has continued to develop the language ever since. Since then, it has continued to be developed by Oracle and has become the most widely used language for web applications in the client/server model.

Until the JVM appeared, programming languages were converted into instructions that the CPU could understand and execute through a compiler, as shown on the left in the figure below. When a complex language was built using this method, it was difficult to support hardware variations. The JVM, on the other hand, enables more flexible and advanced programming by forming a compiled file called Java bytecode once and then translating it into a machine language that the host can understand.

This JVM environment allows Clojure to maintain a high degree of compatibility with Scala, JRuby, and Java, which all run on the same JVM.

About Java

Java is an Object Oriented Programming (OOP) language as previously described in “Object Oriented Languages” and others. According to “Clojure for Brave and True,” a useful introduction to Clojure, the object is likened to a dumb android, capable of only two things: responding to commands and holding data. It can only do two things: respond to commands and store data.

When we consider the factory that manufactures the android, the set of commands that the android understands and the set of data that the android retains correspond, in OOP terms, to the factory as a class, the android as an object, and the commands as methods.

For example, suppose we have a factory (class) called ScrayClown that generates an android (object) corresponding to a command (method) called makBallonArt. The android keeps track of the number of balloons it has and updates that number whenever the number of balloons changes. To report the number of balloons, use ballonCount, and to receive the number of balloons, use recieveBalloons. Their descriptions are as follows.

ScaryClown bellyRubsTheClown = new ScaryClown(); 
bellyRubsTheClown.balloonCount();
// => 0
bellyRubsTheClown.receiveBalloons(2); 
bellyRubsTheClown.balloonCount();
// => 2
bellyRubsTheClown.makeBalloonArt();
// => "Belly Rubs makes a balloon shaped like a clown, because Belly Rubs
// => is trying to scare you and nothing is scarier than clowns."

This example shows how to create a new object bellyRubsTheClown using the ScaryClown class. It also shows how to call this object’s methods (balloonCount, receiveBalloons, makeBalloonArt, etc.).

OOP can also send commands to the factory. In OOP terms, this means that the class also has methods. For example, the built-in class Math has many class methods, including Math.abs, which returns the absolute value of a number.

Math.abs(-50)
// => 50

Now, create PitatePhrases.java in the directory named facebook and write the following

public class PiratePhrases
{    
   public static void main(String[] args)
   {
      System.out.println("Shiver me timbers!!!");
   }
}

This simple program, when run, displays the phrase “Shiver me timbers!!!” phrase in the terminal (just like a pirate saying “Hello, world!”). It consists of a class called PiratePhrases and a static method called main that belongs to that class. Static methods are essentially class methods.

If we type “javac PitatePhrases.java” in the terminal (assuming the JVM environment is set up), a class file named PiratePhrases.class will be generated, and if we type “java PitatePhrases”, we will get “Shiver me timbers!!!” and you can confirm that you have made it work (run) correctly.

What we have done here is to compile PitatePhrases.java using a compiler called javac to create a class file named PitatePhrases. This class file is packed with a large amount of Java bytecode.

cafe babe 0000 003d 001d 0a00 0200 0307
0004 0c00 0500 0601 0010 6a61 7661 2f6c
616e 672f 4f62 6a65 6374 0100 063c 696e
6974 3e01 0003 2829 5609 0008 0009 0700

When you run java PiratePhrases, the JVM first looks at the classpath to find a class named PiratePhrases. The classpath will be a list of filesystem paths that the JVM will look for to find the file that defines the class. By default, the classpath contains the directory you are in when you run java. java allows only one public class per file, and the file name must match the class name. Thus, java looks for the bytecode of the PiratePhrases class in PiratePhrases.class, and when it finds it, it executes the main method of that class. java is similar to the C language in that if you say “run something and use this class as an entry point ” will always execute the main method of that class.

How to use multi-file programs and Java libraries

First, packages in Java provide code organization. Specifically, packages contain classes, and package names correspond to filesystem directories. If a file has the line package com.shapemaster, then the directory com/shapemaster exists somewhere in the classpath.

import Java allows classes to be imported. This basically means that classes can be referenced without namespace prefixes. In other words, if com.shapemaster has a class called Square, you can write import com.shapemaster.Square; or import com.shapemaster.*; at the beginning of the .java file, and your code will refer to com. Square instead of shapemaster.Square.

Here is an example of using package and import. Here we create a package called pirate_phrases and create two classes, Greetings and Farewells. First, go to the phasebook and create a directory named pirate_phrases in that directory. pirate_phrases needs to be created because Java package names correspond to directories in the file system. Next, create Greetings.java in the pirate_phrases directory.

phrasebook
  |____pirate_phrases
           |_____Farewells.java
           |_____Greetings.java
  |_____PirateConversation.java
  |_____PirtaePhrases.java

Now, import Farewells.java and Greetings.java files in the pirate_phrases folder as follows.

package pirate_phrases;

public class Farewells
{
    public static void goodbye()
    {
        System.out.println("A fair turn of the tide ter ye thar, ye magnificent sea friend!!");
    }
}
package pirate_phrases;

public class Greetings
{
    public static void hello()
    {
        System.out.println("Shiver me timbers!!!");
    }
}

Furthermore, the PirateConversation.java file that imports them and makes them work is as follows.

import pirate_phrases.*;

public class PirateConversation
{
    public static void main(String[] args)
    {
        Greetings greetings = new Greetings();
        greetings.hello();

        Farewells farewells = new Farewells();
        farewells.goodbye();
    }
}

The first line, import pirate_phrases.*; imports all classes in the pirate_phrases package, including Greetings and Farewells classes.

Compiling the class file with “javac PirateConversation.java” and running it with “java PirateConversation” produces “Shiver me timbers! A fair turn of the tide ter ye thar, ye magnificent sea friend! and you can see the two classes in action.

JAR file

The JAR file allows you to bundle all .class files into one file. Go to the aforementioned phasebook directory and execute the following

jar cvfe conversation.jar PirateConversation PirateConversation.class

A compressed file named conversation.jar is created, and the contents can be viewed with “jar tf conversation.jar” and run with “java -jar conversation.jar”.

Scala

Scala is a programming language that runs on the Java platform and is a relatively new language that was introduced around 2003. Scala is a relatively new programming language that was introduced around 2003 and has been put to practical use by companies such as Twitter and Foursquare overseas, and is being adopted by an increasing number of companies in Japan as well.

Scala was designed and developed by Professor Martin Odersky of the Swiss Federal Institute of Technology, who was also involved in the design of Generics, a Java language. The motivation for its development was to

  • General-purpose languages are scalable, meaning that the same concepts can be used to describe both small and large programs.
  • Scalability can be achieved by integrating and generalizing the programming concepts of functional languages and object-oriented languages.

The official Scala website introduces Scala as follows: “Scala combines object-oriented and functional programming in one concise, high-level language. Scala combines object-oriented and functional programming in one concise, high-level language. You can build high-performance systems with easy access.”

Comparing Java source code with Scala source code, the following can be written very shortly

//Javaのコード
public class Person
{
  private  String firstName;
  private  String lastName;
  String getFirstName() { return firstName; }
  void setFirstName(String firstName) { this.firstName = firstName; }
  String getLastName() { return  lastName; }
  void setLastName(String lastName) { this.lastName = lastName; }
  int hashCode() ....
  boolean equals(Object o) { ....  }
}

//Scalaのコード
case class Person(firstName:String, lastName:String)
  

Koltlin

Koltlin is a language that runs on the JVM developed by JETBRAINS, which sells InteiliJ, a development environment for various languages. The features that are considered useful were selected from the features that are desired in Java but have not yet been realized to maintain compatibility. We also adopted functions and simple notation (sugar-coated syntax) from the scripting language Groovy, which is similar to Java in that it runs on the Java Virtual Machine, and Scala, which has strong elements of functional programming language. In addition, some parts of the code are influenced by C#, such as the generics syntax.

An example code is shown below.

package org.kotlinlang.play         
l
fun main() {                        
    println("Hello, World!")        
}

Here are the details

Technology Topics

  • Overview of Code as Data and Examples of Algorithms and Implementations

“Code as Data” refers to a concept or approach that treats the code of a program itself as data, and is a method that allows programs to be manipulated, analyzed, transformed, and processed as data structures. Normally, a program receives an input, executes a specific procedure or algorithm on it, and outputs the result. In “Code as Data,” on the other hand, the program itself is treated as data and manipulated by other programs. This allows programs to be handled more flexibly, dynamically, and abstractly.

In order to program, it is necessary to create a development environment for each language. This section describes how to set up specific development environments for Python, Clojure, C, Java, R, LISP, Prolog, Javascript, and PHP, as described in this blog. Each language has its own platform to facilitate development, which makes it possible to easily set up the environment, but this section focuses on the simplest case.

A statically typed language is one in which the type of an expression is determined at compile time. First, in statically typed languages, variables have types: if you declare a: in Java, a is of type int. And a+1 is a variable a of type int plus 1 of type int, so the result is of type int. If a+0.5, the result will be of type double, because in Java, int is expanded to double and then added. Thus, in statically typed languages, the type of each expression, including subexpressions, is determined at compile time. Of course, the types of method arguments and return values are also determined. Otherwise, the form of the expression would not be determined.

File input/output functions are the most basic and indispensable functions when programming. Since file input/output functions are procedural instructions, each language has its own way of implementing them. Concrete implementations of file input/output in various languages are described below.

Among programming languages, the basic functionality is one element of the three functions of structured languages (1) sequential progression, (2) conditional branching, and (3) repetition, as described in the “History of Programming Languages” section. Here, we show implementations of repetition and branching in various languages.

Database technology refers to technology for efficiently managing, storing, retrieving, and processing data, and is intended to support data persistence and manipulation in information systems and applications, and to ensure data accuracy, consistency, availability, and security.

The following sections describe implementations in various languages for actually handling these databases.

This section describes examples of how servers described in “Server Technology” can be used in various programming languages. Server technology here refers to technology related to the design, construction, and operation of server systems that receive requests from clients over a network, execute requested processes, and return responses.

Server technologies are used in a variety of systems and services, such as web applications, API servers, database servers, and mail servers. Server technology implementation methods and best practices differ depending on the programming language and framework.

    Clojure is a Lisp that runs on top of the JVM and can be integrated with Java at various levels, allowing full use of Java assets.

    PlantUML will be a tool that can automatically draw various open source data models and is based on graphviz, an open source drawing tool provided by AT&T Laboratories. It will be a component for quickly creating various diagrams such as those shown below.

    There are various ways to use plantUML. (1) using Jar files, (2) using brew on Mac, (3) Web services, and (4) planting in applications.

    At Clojure/Conj2018, one of the Clojure conferences, I saw Tyler Hobbs give a talk on generative art called “CODE GOES IN, ART COMES OUT”.

    According to wiki, generative art is

    Generative art refers to works of art that are algorithmically generated, synthesized, or constructed by computer software algorithms or mathematical/mechanical/random autonomous processes. By taking advantage of the computational freedom and computational speed of computers, and by implementing theories derived from natural science, many works are made to express themselves in a unified, organic manner, somewhere between artificial and natural.

    Generative art is an art form that uses natural scientific systems as its main creative method. The difference between generative art and other art forms is that generative art requires the design and creation of mechanisms that operate autonomously. Works of art with systems may implement scientific theories such as complex systems and information theory.

    In this article, we will discuss how to build a development environment for Apache Spark and how to build and run an application. Spark provides APIs for a variety of programming languages. In this article, we will discuss how to build an application using Scala, the language in which Apache Spark is written, under the assumption that Spark-clint is used. First, a Spark application needs to have its source code compiled and packaged into a JAR file. For this purpose, we will use sbt, a tool to manage the build process of an application development project, including compilation of source code written in Scala and Java, management of library dependencies, and packaging.

    • About Scale (Guide to Scala)

    コメント

    タイトルとURLをコピーしました