I have just started learning Java and want to understand the difference from other languages.
** Click here for an overview and environment construction article **
I would like to divide what I learned in Java into several articles and output them.
-Variables and types ・ Type conversion -Variable Scope -String operation -Array operation ・ Operator ・ Conditional branch (in preparation) ・ Repeat processing (in preparation) ・ About class (in preparation) ・ Abstract class (in preparation) ・ Interface (in preparation) ・ Encapsulation (in preparation) ・ About the module (in preparation) -Exception handling ・ About lambda expression ・ About Stream API
Next time, I will check Variables and types. This time, I will roughly check two points that are different between Java and other languages.
Computers can only execute and understand machine language written with 0s and 1s. Source code written in a programming language must also be converted (translated) into machine language.
Called the interpreter language
.
A source file written in a programming language
It is executed while converting line by line so that the computer can recognize it.
Write a program ↓ (Translated into machine language) Run ↓ Write a program ↓ (Translated into machine language) Run
Hello.php
<?php
echo "Hello World";
?>
Terminal
$ php Hello.php
Hello World ← Executed and output!
Java
Called the compiler language
.
All source files written in programming language are once converted (translated) into machine language to generate another file (class file).
And the execution is done collectively by the JVM (Java Virtual Machine).
Write a program ↓ Compile (translate into machine language) Generate class file ↓ Run
Hello.java
class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Compile the source file with the javac command to create a class file.
Terminal
$ javac Hello.java
$ ls
Hello.java Hello.class ← Created!
Execute the class file with the java command and output Hello World. Do not add the `.class extension. ``
Terminal
$ java Hello
Hello World ← Output!
** Interpretered language **
-When the computer executes, the programming language written in the source file is converted to machine language and executed.
-Since the interpreted language does not need to be compiled one by one, one troublesome procedure can be omitted.
** Compiled language **
-Prepare a compiled version (class file) of the source file written in the programming language so that the machine language can be read. So you don't have to compile when your computer does the work.
-Speaking of computer processing speed, a compiler-type language in which all code is once translated into machine language is faster.
Called a dynamically typed language
.
It is not decided what kind of value will be included in the variable name, function argument or return value. (Can be declared without deciding)
test.js
let str = "Good morning";
console.log(str); //Good morning
str = 2020;
console.log(str); // 2020
Originally it was the variable name "str" for entering a character string, but you can also substitute the numerical value 2020.
test.js
//A function for adding numbers and returning a result
function sumNumber(a, b) {
return a + b;
}
console.log(sumNumber(1, 1)); // 2
console.log(sumNumber("Hello", "World")); // HelloWorld
Originally, the function name was "sumNumber", which I wanted to return the result of adding two numbers. You can also add the strings Hello and World and return them.
There is a demerit that you can use it unintentionally.
Java
Called a statically typed language
.
Explicitly declare the type in variable names, function arguments and return values.
It cannot be handled with data other than the declared type.
I will write the code in the same way as before.
Main.java
class Main {
public static void main(String[] args) {
String str = "Good morning";
System.out.println(str); //Good morning
str = 2020; // Type mismatch: cannot convert from int to String
System.out.println(str);
}
}
An error will occur if you try to substitute the numerical value 2020 for the variable name "str" that contains character type data. Correctly...
Main.java
class Main {
public static void main(String[] args) {
String str = "Good morning";
System.out.println(str); //Good morning
//Substitute the string "Good evening" for str
str = "Good evening";
System.out.println(str); //Good evening
//Substitute 2020 for the variable name "year" specified as int type
int year = 2020;
System.out.println(year); // 2020
}
}
The same is true for functions.
Main.java
class Main {
public static void main(String[] args) {
System.out.println(sumNumber(1, 1)); // 2
System.out.println(sumNumber("Hello", "World")); // The method sumNumber(int, int) in the type Main is not applicable for the arguments (String, String)
}
//Both the first argument and the second argument receive int type, and the return value also defines the int type function name "sumNumber".
public static int sumNumber(int a, int b) {
return a + b;
}
}
In JavaScript it was okay to pass either letters or numbers, but in Java I get an error. This is because even though it is a function that receives int type data, it is passing a character string.
It can be executed as the program creator intended, and there is an advantage that if an error occurs, it can be noticed before the program is executed.
I've only learned dynamically typed languages so far, so I was surprised to specify the data type every time I declare a variable. Next, we'll delve into Variables and Types (https://qiita.com/morioheisei/items/3d27e82d6cd88068dded).
Recommended Posts