――It was this assignment from this week ――I ended up studying Java ――Let's summarize the differences between Java and Python while my memory is new. ――By the way, let's pull out and compare JavaScript that I have studied a little.
--Variables --if statement --while statement --for statement
Python --No need to declare data type --The type is specified by the contents of the variable. --No need for a semicolon
asobi.py
py_str = "Python"
py_int = 23
py_float = 23.5
#Data type confirmation
print(type(py_str))
print(type(py_int))
print(type(py_float))
console
<class 'str'>
<class 'int'>
<class 'float'>
--You can also rewrite the data type of the variable
asobi.py
py_str = "Python"
py_int = 23
print(type(py_str))
py_str = 10
print(type(py_str))
py_cal = py_str * py_int
print(py_cal)
console
<class 'str'>
<class 'int'>
230
Java --You must declare the data type --There is also a float type, but it seems to mainly use the double type. --I couldn't find a way to check the data type directly (it seems that there is a method called instanceof, but it can't be used for int or double) --For Java data types, the site here is easy to understand. --Don't forget the semicolon
asobi.java
public class asobi {
public static void main(String[] args) {
String javaStr = "Java";
int javaInt = 23;
double javaFloat = 23.5;
}
}
JavaScript --No data type declaration is required, but var must be prepended to the variable name ――Both are number type, not int type and float type ... --I also need a semicolon
asobi.html
<html>
<script>
var jsStr = "JavaScript";
var jsInt = 23;
var jsFloat = 23.5;
//Data type confirmation
console.log(typeof(jsStr));
console.log(typeof(jsInt));
console.log(typeof(jsFloat));
</script>
</html>
console
string
number
number
Python --if, elif, else are used --Add: to the end of the condition --Indentation of the statement to be executed is required
asobi.py
age = 23
if age >= 65:
print("Retirement age")
elif age >= 20:
print("I'm an adult")
else:
print("I'm a minor")
Java --if, else if, else are used --Enclose each condition in () and the statement to be executed in {}
asobi.java
public class asobi {
public static void main(String[] args) {
int age = 23;
if (age >= 65) {
System.out.println("Retirement age");
}else if (age >= 20) {
System.out.println("I'm an adult");
}else{
System.out.println("I'm a minor");
}
}
}
JavaScript --if, else if, else are used --Enclose each condition in () and the statement to be executed in {} ――The form of the if statement itself is the same as Java.
asobi.html
<html>
<script>
var age = 23;
if (age >= 65) {
console.log("Retirement age");
}else if (age >= 20){
console.log("I'm an adult");
}else{
console.log("I'm a minor");
}
</script>
</html>
――Let's display the console in each language as follows
console
0
Is 1
2
3
4
Python --Since it is not possible to connect i, which is an int type, and a character string as it is, make i a str type.
asobi.py
i = 0
while i < 5:
print(str(i) + "is")
i += 1
Java --As usual, enclose the condition in () and the executable statement in {}. --You can use the form i ++ in increments
asobi.java
public class asobi {
public static void main(String[] args) {
int i = 0;
while (i < 5){
System.out.println(i + "is");
i++;
}
}
}
JavaScript --As usual, the form of the while statement itself is the same as Java.
asobi.html
<html>
<script>
var i = 0;
while (i < 5){
console.log(i + "is");
i++;
}
</script>
</html>
――Let's display the console in each language as follows
console
0
Is 1
2
3
4
Python --i is repeated in the range of 0 to 4 5 times
asobi.py
for i in range(5):
print(str(i) + "is")
Java --The repeat condition of the for statement is represented by (initial value; range; increase / decrease). --Variables that store initial values must declare a data type
asobi.java
public class asobi {
public static void main(String[] args) {
for (int i = 0; i < 5; i++){
System.out.println(i + "is");
}
}
}
JavaScript --As usual, the form of the for statement itself is the same as Java. --Var must be prepended to the variable name that stores the initial value
asobi.html
<html>
<script>
for (var i = 0; i < 5; i++){
console.log(i + "is");
}
</script>
</html>
――When studying multiple languages, it is easier to remember if you study while comparing ――It's said that Java and JavaScript are different from India and Indonesia, but the writing style is quite similar. ――After all Python is the easiest to write!