At work, I had the opportunity to teach Java (Servlet) to those who have been working in C for a long time, and I was made to think about the features of Java and the parts that are difficult to understand, so here is an introductory article on Java. I will summarize it.
This article is not for "programming beginners", but for "people who normally work in C language". Therefore, I will not talk about programming in general (such as "What are variables?"). Also, omit the contents that appear immediately when you google, such as Java grammar. Only link to good reference sites.
I hope this article helps you understand the characteristics and confusing parts of the Java language.
First, let's build the environment. It's not uncommon for a clean machine to be unable to even compile or even run Java. First, let's take a look at the elements needed to write and run Java.
The first source of confusion when it comes to "downloading and installing Java" is the JDK and JRE. You can download only the JRE separately, or if you download the JDK, there is a JRE directory in it, which is also a cause of difficulty.
So, let's take a brief look here.
JDK (Java Development Kit) As the name implies, it is a Java development kit. Used when coding. Includes compiler, debugger, performance analysis tools and more. It's a good idea to remember that you need it before the source code is compiled. (Strictly speaking, the JDK refers to the one including the JRE described later, but in the sense of memorability, it is "until the source code is compiled")
JRE (Java Runtime Environment) As the name implies, it is a Java execution environment. Java absorbs environmental differences such as the OS of the machine on which it is executed by using the JVM described later. The JRE contains files and so on to run this JVM. Keep in mind that you need a JRE to run the compiled Java.
So it's no wonder you know what the JDK and JRE stand for. If you keep this in mind, the environment construction should go smoothly.
When I open the Oracle Java Page to download the JDK, items such as Java SE, Java EE, and Java FX are lined up. , I'm wondering which one to choose.
I would like to say, "Java is Java!", But in fact, even if you say Java in a word, the type you need depends on the purpose. This section describes these differences.
Java SE (Java Standard Edition) A JDK that contains the most basic Java APIs. Use this when developing applications that run on the console, Window applications, etc.
Java EE (Java Enterprise Edition) EE is SE with API for developing Servlet programs. Select this if you want to develop a server application with Servlet or JSP.
Java FX Actually, I haven't touched JavaFX at all, so I can't explain the details, but Oracle's [JavaFX Overview](http://docs.oracle.com/javase/8/javafx/get-started-tutorial/ According to jfx-overview.htm#JFXST7840), it is a group of packages for developing rich GUI applications using graphics and audio media.
It seems to use this if you want to develop a GUI application that mainly uses images, animations, and sounds.
So, once you know "EE if you do Servlet, SE if you don't", it seems good.
So far, the following articles are easy to understand about the differences per JDK / JRE, SE / EE / FX.
Differences from the meaning of Java JDK, JRE, SE, EE, etc.
~~ For some reason Java, for example, Java version 1.8 is called "Java 8". It's easy to misunderstand that the major Java version is "8" at first, but in reality the major Java version is "1" and the minor version is "8". ~~
(Corrected on 2016/08/12) In Java, there are "version number" and "version string" as words to indicate the version. For example, in the case of Java SE version 8, the version number is "Java SE 8" and the corresponding version string is "1.8". I am. At first, it's easy to get confused by the proper use of this notation, but it's good to remember that there are two notations for the same version.
See the links below for more information. Java Platform, Standard Edition 8 Name and Version Oracle
(Corrected so far. Thank you for pointing out @ budougumi0617!)
That is, "Java 1.8" and "Java 8" refer to the same thing. Especially in the IDE, this notation is mixed and confusing, so be careful.
Unlike C language, Java does not become an executable file as it is even if the source code is compiled. Whether it's a console program, Servlet, or Android, you need a VM (virtual machine) called JVM to run Java. You don't have to think too hard about a virtual machine. The execution itself simply executes the java command with the compiled class as an argument.
For example, the flow of compiling and executing a source file called HelloWorld.java is as follows.
#Compiling source files
$ javac HelloWorld.java
#View files in the directory (.class file is generated)
$ ls
HelloWorld.java HelloWorld.class
#Execution (.class is not attached)
$ java HelloWorld
Hello World!
C language becomes machine language when compiled, but Java only outputs a file in .class format that can be read by the JVM. You can think of a .class file as a simple conversion of a .java file, which is a text file, into a binary file.
The reason for taking such a procedure is that the JVM fills in the differences in the execution environment such as the OS, as explained earlier in the JRE section. The C language needs to be compiled for the environment in which the person who wrote the source code wants to run it, but Java does not have to. Compile the source code without worrying about the execution environment, and execute it on the JVM installed by each machine according to each situation. Programmers don't have to worry about which machine their program runs on. For this reason, Java is said to be a platform-independent language.
By the way, all languages called "JVM languages" such as Scala and Kotlin have the characteristic that they become .class files when compiled and are executed in this JVM. Since the final execution environment is the JVM, it has the advantage of being easy to mix with Java (it is easy to reuse the source code once written in Java).
The most commonly used Java development is Eclipse. There are other options such as IntelliJ and NetBeans, but IntelliJ is paid, NetBeans is a little minor than Eclipse, and the amount of plugins and information is small, so it is better to start with Eclipse for the time being.
Eclipse is open source, so anyone can download it for free from the official website. If you are using Windows, Pleiades Japanese localization & various convenient plug-ins & JDK6,7,8 & Tomcat6,7,8 are included, " All in One package is available. It will take some time to download, but if you drop this for the time being, you will have everything you need for Java & Servlet development, so using All in One is also a good option. Please use it properly according to the circumstances of the development PC.
By the way, if you have time to study, we recommend that you write a simple program using only a text editor without using an IDE and execute it on the command line. You can understand Java syntax, compilation-> execution flow and mechanism well.
Let's write the code as soon as the environment is ready. The basic grammar and writing method are left to Java Tutorials, etc., and here we will mainly explain the differences from the customs and ideas of C language. ..
In Java you always have to create a class.
Even if it only displays "Hello World!", Unlike C language, where you only need to write the int main () function,
Create a HelloWorld.java file, define the class with class HelloWorld (make sure the class name is the same as the .java file name), define the main method with public static void main (String args []), and finally Write System.out.println ("Hello World!"); In and execute it, and Hello World is finally completed.
As is often said, Java is an object-oriented language. With the exception of primitive types such as int and char, everything is defined as a class, with distinct roles and behaviors.
As a result, the reusability of the source code once written is improved, the development efficiency is improved by multiple people, and the open source library is easy to create and incorporate.
Therefore, as far as I know, the amount of description required for HelloWorld is overwhelmingly large compared to other languages, but the larger the system, the greater the merit of classification. More on the class later.
When you're researching Java, the words "class", "instance", and "object" come up frequently and can be confusing. This is a very important factor in understanding Java, so I will briefly summarize it here.
A class is just a programmatic definition. Declare it as class Food {} and define member variables and methods in it. If you dare to compare it to the grammar of C language, is it an image that the structure has a function?
In object-oriented programming, it is desirable that one class is one thing that humans can recognize. Food class, FoodListView class, CsvFileAccessor class, etc., one object in reality, one screen on the application, programmatic Collect what may be one of the roles as one class.
Just because it's convenient, be careful not to combine the two roles into one, like FoodAndListView (a class that defines values for food and holds and displays that list). If you put them together, it will be a class that cannot be reused when a request that requires only one of the concepts, such as "I want to send Food class data to the server", comes out.
In Java, division of roles / reusability is often prioritized over program execution efficiency.
Instances are created (memory space is reserved) when a new class is created during program execution.
For the member variables defined in the class, a separate memory area is allocated for each instance, so for example, "Create two instances by newing the Food class twice, and set the name variable of one instance to" Ramen. You can create instances with different values using the Food class as a template, such as "set" and "set" Curry "in the name variable of the other instance".
Be a little careful when using the word "object". Because the word "object" in Java talks means "instance", "class", or an object in a more vague object-oriented sense. And in some cases, the intention is ambiguous.
When talking about Java programs themselves, there is no problem if you understand the difference between "class" and "instance", so I think it's a good idea to remember those two first. (In this article as well, I am conscious of not using the word "object" other than the term "object-oriented".)
As explained so far, Java has different characteristics and ideas from C language. In other words, the things that are prioritized when considering the trade-offs in coding, such as readability, execution efficiency, and extensibility, are also different from C language.
Here are some things to keep in mind when coding Java.
In addition, the article is very educational about the precautions when coding Java. Please read it together. (I wrote this article with reference to this article)
In C language, I think that it is often used to edit the argument received by the pointer in the function and return the execution result code of the function as the return value. (Items of "Output parameters" and "Return processing status" in the above article)
However, it is better not to create such a method in Java. This is because the method caller must understand by looking at the method implementation how the instance passed as an argument is changed within the method, and whether something is changed or not in the first place.
Not limited to Java, methods that directly change the instance received as an argument are called "destructive" methods and tend to be avoided. Basically, please be aware that the instance received as an argument is not changed, and the result is always returned as the return value of the method.
In C, variables had to be defined at the beginning of a function. But Java has no such restrictions. Rather, it is desirable to define it so that it can be referenced only in the minimum necessary scope immediately before use. If the scope is limited, later readers of the source code will only need to look at the minimum necessary range to find out how the variable is used, minimizing the burden.
(Added on 2016/08/12) Strictly speaking, it was the ANSI-C specification formulated in 1989 that the variable declaration had to be made at the beginning of the block, and it seems that there is no such restriction in C99 formulated in 1999. As a result, it seems that "declare variables just before use" as described as a feature of Java in this item is also considered in C language. Thank you @SaitoAtsushi for pointing this out! (Addition so far)
Java has clear naming conventions, such as method names and variable names.
The behavior of the program does not change because it violates the naming convention, but Java developers read the source code assuming that the program is written according to this naming convention.
If the description "Food" appears in the source code, it is always judged to be the class name, and if it is "food", it is judged to be the variable name. If you write it in the opposite way, it will prevent other developers from understanding the source code, which may result in bugs, so always be aware of this when coding.
People often say, "Java is easy because it doesn't have pointers," but I think it's half correct and half lie.
Certainly there is no grammatical pointer, so you can't manipulate pointers directly. However, if you create an instance and store it in a variable, what the variable indicates is a pointer to the instance, not the instance itself, and passing it as a method argument means using the pointer in C language as an argument. It's almost the same as passing.
If you code without understanding this, for example, you pass an instance to a method as shown in the following example, and when that instance is edited in the method, the instance that called the method is changed. You will not be able to understand the behavior.
Main.java
public static void main(String args[]) {
Food ramen = new Food("ramen"); // "ramen"Create a Food instance with the name
FoodPrinter.printDecoratedName(ramen); //I want to output the name beautifully
System.out.println(ramen.getName()); // "ramen"not"***** ramen *****"Is output
}
FoodPrinter.java
/**
*A method that outputs the name of the food received as an argument with a little decoration
*/
public static void printDecoratedName(Food food) {
food.setName("***** " + food.getName() + " *****"); //Decoration
System.out.println(food.getName()); // -> ***** ramen *****
}
If you understand the concept of pointers, it's a matter of course because you're just passing a reference like this, but if you write Java without understanding pointers, it's one of the mistakes you often make. ..
Because it's Java, you don't have to think about pointers, but because it's Java, you should be aware of where in the memory the variables you're dealing with are pointing (which variables are the same as the memory they're pointing to). I think that is important.
There are still some parts that I haven't written yet, but if I write one, it will end up in this and the article will not end, so I will stop here.
I myself am still studying Java, and I learned C language for about 3 months in the company training, so there is a possibility that the content may be incorrect or misleading. If you have any such points, please let us know in the comments. I hope it can be a reference article for those who are starting Java from now on, including the discussion in the comment section.
Also, I would appreciate it if you could comment on any unclear points. I would like to write another article or add to this article as needed.
Recommended Posts