Pre-introduction notes for JavaScript experienced learners of Java

An acquaintance with JavaScript experience said that he wanted to learn Java, so I've put together a simple pre-introduction note.

Assumed reader

It is intended for anyone who has learned or written JavaScript code that works in a web browser or in a NodeJS environment.

To learn properly, I think it is effective to read the Java Primer and to refer to and imitate a lot of good code. However, if you have knowledge of JavaScript at this point, it may be easier to understand if you learn after checking the difference with Java. I thought, I tried to summarize this memo.

If you find this memo too easy or too verbose, I recommend the great summary site Introduction to Java Code.

Development environment

It is assumed that we will use some kind of integrated development environment in this era. For the time being, I'm assuming the old-fashioned [Eclipse (Integrated Development Environment)](https://ja.wikipedia.org/wiki/Eclipse_(%E7%B5%B1%E5%90%88%E9%96%) 8B% E7% 99% BA% E7% 92% B0% E5% A2% 83))). There are many introductory books and articles, but what about the following articles, for example?

However, if you want to learn by yourself in advance, if you write work code, it is better to listen to the work environment and match the development environment and Java version.

Try it more easily

If you just want to experiment with Java code, it's easy and convenient to use an online service.

Also, if you have a Docker environment, you can try it without installing the Java development environment by running Docker Image including the Java environment. I also this year material using docker-lambda and [VSCode + Codewind material (NodeJS but Java is also possible)](https://qiita.com/ I have posted yamachan360 / items / 47fcdd3b1a116b7c2412), so please refer to it if you are interested.

Difference that becomes a point

Compiler language

Java is the compiler language and JavaScript is the scripting language. This is certainly the case, but I think you don't have to worry too much about it for the following reasons.

In the case of Java, I think that knowledge that an intermediate file called * .class is automatically created at runtime is sufficient.

More recent Java allows you to run * .java files as-is (it compiles secretly behind the scenes), so you might be wondering if it's a compiled language anymore? is. It's okay if you can understand only the following static typing, which is derived from the origin of the compiled language.

Static and dynamic typing

Java is a ** static ** typed language, unlike ** dynamic ** JavaScript. Checking the type is strict, so if you do not write it consciously, you will be surprised by frequent errors and warnings.

Experienced JavaScript-based languages that can be statically typed, such as TypeScript, can skip this section.

I personally like dynamic typing because it's easier to write. However, in large-scale situations such as managing code at work, or in situations where implementation and testing cannot be easily repeated, statically typed languages seem to be easier as a result.

The following articles are recommended.

  • Java, C, C #, VB.net, etc. are "statically typed"
  • JavaScript, Python, Ruby, etc. are "dynamically typed"

"Static" checks the structure at compile time. "Dynamic" checks the structure at runtime.

The advantage of static typing is that it causes compilation errors. Attached to this. This can significantly reduce the problems you only see when you run it. I'm not happy with a sloppy type system like C, but Java, Scala, OCaml, Haskell, etc. are quite useful.

It is not necessary to understand all the articles introduced, but I think that you only have to read them roughly and feel "Hmm" roughly. It is introduced on the assumption that it will be read again after learning has progressed.

Object-orientation

Also in JavaScript [Object Oriented](https://ja.wikipedia.org/wiki/%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF The concept of% E3% 83% 88% E6% 8C% 87% E5% 90% 91) is is, It's not essential knowledge, and you can actually write code without much awareness.

Object-orientation, on the other hand, is a fundamental part of Java and has a more complex mechanism.

For the time being, it seems good to understand inheritance, interface as an entrance. In particular, the ** interface ** is something that JavaScript doesn't have, and in a quiet place, it's taken care of.

An interface defines only the signatures (names, arguments, return types) of the methods needed to achieve a particular function. It does not have the processing itself.

Simply put, it omits the part of the code that is executed from the class definition. In other words, although it defines the external specifications of the class, the actual processing contents are not described. In C language, it is equivalent to a header file (* .h).

In addition, Java also has ** abstract classes ** that are somewhere between classes and interfaces, making it feel like a "partially code-free" class.

Interfaces and abstract classes cannot be used as they are, and it is necessary to inherit (implement the interface) and define a class that supplements the missing processing code. It seems difficult to say, but in fact, you don't have to worry too much at first.

For example, in Eclipse, when creating a new class, the automatic generation of methods is checked by default as shown below, so if there is a function (method) for which code implementation is not implemented, automatically create the definition part as well. I will. Even if you accidentally erase it or forget to implement it, you will get warnings and errors, so you can tell immediately. image.png

Method overload

When reading Java code, you may be confused about which function (method) with the same name is being defined. This is due to Method Overload.

In Java, you can also define multiple methods that have the same name but different argument types and sequences. This is called method overloading.

With JavaScript, you can just look at the function name to find it. In Java, if multiple functions (methods) with the same name are defined, you must search for the one that matches the number of arguments and type (including inheritance relationship).

Well, in an integrated development environment, you can check this area and move to the reference source, so it's not too much trouble. In Eclipse, for example, you can press F3 while calling a function to go to the code that defines it.

String comparison

The first thing that Java beginners get caught in is the "character code comparison" mistake. The following examples are famous.

	String str1 = "123";
	String str2 = "123";
	String str3 = "12" + "3";
	String str4 = "12";
	str4 += "3";

	System.out.println(str1 == str2); // true (1
	System.out.println(str1 == str3); // true (2
	System.out.println(str1 == str4); // false (3
	System.out.println(str1.equals(str4)); // true (4

In JavaScript, the `` = repeatedly operation on a string is a value comparison, so all of the above is true.

In Java, = incorrectly is basically a "same object" decision, and the ```equals method is used to compare values.

The tricky part here is that (1 and (2 can result in true in many environments, which can cause bugs. This is the effect of Java compile-time optimizations.

In Java, strings are immutable and their values do not change during program execution. Then, if it is a character string with the same value of "123", internally, "Oh, even if you don't prepare two seriously, you should prepare one and use it?" We will optimize (cut out). The result (1 is true.

More modern Java compilers are smarter, so when the expression "12" + "3" is included in the code, I guess, "Oh, you want to use the string" 123 "after all?" It ends up replacing the expression with the result "123". So the result is (2 is also true for the same reason as (1).

This result is the result of compiling with Java8. Later versions of the smarter Java compiler (3 may also optimize for true w

So don't forget to use the function `ʻequals`` to compare values in Java!

It's also a concept in JavaScript, but it's a good idea to check Java's wrapper class for the time being.

About language extension

Some people may have the impression that the Java language is old and fossil-like. Java is relatively aggressive in adopting the latest language specifications, and in particular, it is becoming more and more influenced by JavaScript. The following includes personal impressions.

JavaScript is also heavily influenced by Java, so it's like a child of Java. And Java has been implementing the best of JavaScript in turn. Be careful with the Java version as features are added in sequence, see Java Version History (https://ja.wikipedia.org/wiki/Java%E3%83%90%E3%83%] See BC% E3% 82% B8% E3% 83% A7% E3% 83% B3% E5% B1% A5% E6% AD% B4).

However, Java also emphasizes compatibility with the past, and it is difficult to make drastic structural reforms, so there are some parts that are quite aggressive implementations. It's good for just using the new features, but there are some parts that are quite esoteric when you step into the implementation part, so even if you care about the details, you should save the exploration after learning progresses. It is recommended.

Implementation device example

Java also has an anonymous class, which can be used as follows, for example.

Test02


import java.util.*;

public class Test02 {
	public static void main(String[] args) {
		new Thread(new Runnable() { //Here
			public void run(){
				System.out.print("Runnable.run");
			}
		}).start();
	}
}

In order to maintain compatibility with the past, that is, because of an unknown execution environment such as an anonymous class, the following class file is automatically generated at compile time. Although it is described as an anonymous class in the code, it is actually given a name (Test02 $ 1 this time) internally. image.png

As an aside, from JavaSE 8, you can use the new description lambda expression, and you can write more like JavaScript.

Test02


	List<String> list = new ArrayList<>(Arrays.asList("aa", "bb"));
	list.replaceAll(str -> str.toUpperCase());
	System.out.println(list); // [AA, BB]

Still unfamiliar expansion

As mentioned above, the Java language has been extended, but it is said that it is still unfamiliar, and there are some elements that can only be seen in the field of advanced Java development.

As with the lambda expression above, even if you write it normally in JavaScript, it may feel strange when you use it in the Java language. Even if it can be used due to the version. Read the existing code and ask your senior developers to get a feel for it. Enter the town and follow the town.

Personally, generics is often used normally, and I think that only very old code is not used.

I feel that annotation has become quite common, but I feel that there are still few examples of using it until you define your own annotation.

Stream API is a writing style that is often used in JavaScript, but I feel that it is not yet actively used in Java code. Well, I think this is more of a library design topic than a language ...

Library is important

As with any development language, the differences between languages aren't that big, and it's rather time-consuming to learn and use the library.

Java has a lot of standard libraries. First, let's refer to the official API specification as below. However, it is difficult to read the whole thing, so it is a good idea to gradually expand your understanding while checking the details when using the class.

java.lang is the standard library and is automatically loaded from the beginning without any configuration. If you want to read it properly, is it from here at first?

For JavaScript, NodeJS can usually find the functionality you need by searching the npm site (https://www.npmjs.com/). For Java, it's a good idea to look for the Maven Repository (https://mvnrepository.com/) first. Compiled library (jar) files for each version are also available.

Note that NodeJS npm downloads and configures at the same time, but in the case of Java, you need to be careful so that you can find it in the Java runtime environment. If you are running it on the command line, you need to set the classpath or specify the location where you placed it with the -cp or -classpath option. In the case of Eclipse, check the Java Build Path setting of the project, and if you can't find it, add it. image.png Some famous places such as JUnit are already prepared in the Eclipse environment, so it may be a good idea to press the Add Library button below first. image.png

Let's start learning

First, I briefly wrote down the points that seemed to be of concern. Now, start learning Java. My personal recommendation is also the Introduction to Java Code site.

If you are new to programming and want to read a gentler introductory book, Introduction to Java 3rd Edition It may be good.

On the contrary, if you are confident, after understanding on the above website etc., it may be good to study with the following books according to the version, with a view to acquiring qualifications.

Enjoy!

So far, I've summarized what you should know when learning Java for those who have experience with JavaScript. It would be great if the threshold for getting familiar with the Java language was lowered even a little.

On the contrary, I'm sorry if it's confusing. I hope you can skip the text and refer to the various links introduced!

I myself learned Java-> JavaScript, so I think there are many oversights. I think that the description is not enough, but I would like to correct and add it if there is something I noticed.

See you again!

Recommended Posts

Pre-introduction notes for JavaScript experienced learners of Java
[Java] Summary of for statements
[For beginners] Summary of java constructor
Generics of Kotlin for Java developers
Implementation of clone method for Java Record
java notes
List of download destinations for oracle java
Features of spring framework for java developers
Summary of Java environment settings for myself [mac]
Review notes for Java 1.7 and later file copies
A collection of simple questions for Java beginners
[Introduction to Java] Basics of java arithmetic (for beginners)
How to specify index of JavaScript for statement
For JAVA learning (2018-03-16-01)
Java Generics (Notes)
[Java] Array notes
2017 IDE for Java
Java and JavaScript
[Java] Study notes
Java serialization notes
[Java] Overview of Java
Java for statement
Get a list of MBean information for Java applications
[For beginners] Quickly understand the basics of Java 8 Lambda
Introduction to Java for beginners Basic knowledge of Java language ①
List of frequently used Java instructions (for beginners and beginners)
Links for each version (Japanese version) of Java SE API
How to execute WebCamCapture sample of NyARToolkit for Java
Summary of file reading method for each Java file format