[JAVA] Basic writing of various programming languages (self-memo)

If I was doing various programming language learning sites at super speed, I would like to summarize how to write each language. For the time being, only super basic. .. ..

What is written here is a compilation of research by the author who is a super beginner in programming.

I would be grateful if you could point out any mistakes.

List of programming languages handled here

It is difficult to clearly describe the difference in specifications for each language, so it is for reference only.

output

hello_world.c


printf("Hello World");
printf("%d",num);  //Integer output
printf("%f",data);  //Real number output
printf("%c",mozi);  //Character output
printf("%s",str);  //String output

hello_world.py


print("Hello World")
print(num)
print(str)

#Single quote "', And double quotes ""Either is acceptable(Because it is troublesome, I said ""」)

hello_world.java


System.out.println("Hello World"); //The second character from the back of println is l(Elle)

hello_world.js


console.log("Hello World");
console.log(num);
console.log(str);

hello_world.rb


puts "Hello World"
puts "#{str}"  #「#{variable}Can replace the value with

hello_world.cs


Console.WriteLine("Hello World");  //With line breaks
Console.Write("Hello World");  //No line breaks
Console.WriteLine("{0}",num);

comment

comment.c_&_comment.java_&_comment.js_&_comment.cs


//It's a comment

comment.py_&_comment.rb


#It's a comment

Variable declaration (initialization)

box.c


int num = 0;
char mozi = 'a'; //Only one letter
char str[] = "abc";  //String

box.py_&_box.rb


num = 0  #Numerical value
str = "abc"  #String

box.java


int num = 0;  //integer
double num = 3.14;  //Real number
String str = "abc";  //String
boolean bool = true; //Boolean value

box.js


var num = 0;
var str = "abc";

box.cs


int num = 0;
string str = "abc";
bool a = true;
var x = 10;
var y = "abc";

##String concatenation

#### **`strlink.c`**
```c

include <string.h>
char str1[80] = "DRAGON";
char str2[] = "QUEST";
strcat(str1,str2);
printf("%s",str1);

Postscript: It is necessary to create extra elements of the array for the amount to be concatenated.(ex:str1[80]) Thank you for pointing out, shiracamus!

strlink.py


str1 = "DRAGON"
str2 = "QUEST"
print(str1 + str2)

strlink.java


String str1 = "DRAGON";
String str2 = "QUEST";
System.out.println(str1 + str2);

strlink.js


var str1 = "DRAGON";
var str2 = "QUEST";
console.log(str1 + str2);

strlink.rb


str1 = "DRAGON"
str2 = "QUEST"
puts str1 + str2

##Type conversion(cast)

convert.c


 itoa (num, str, 10); // Decimal // Number (int num) to string (str)
 atoi (str); // Number from string

convert.py


 str (num) # From numeric type to string type
 int (str) #string to numeric

Java has automatic type conversion and manual type conversion

convert.java


 System.out.println (month + "month" + date + "day"); // int type month variable and date variable are automatically converted to String type

cast.java


int num = 13;
 System.out.println ((double) num); // Manual type conversion Output result is "13.0"

convert.rb


 puts 1.to_s + "month" + 1.to_s + "day" #to_s to convert numbers to strings

 num = "13" # This is a string object because it is enclosed in double quotes
 Convert strings to numbers with puts num.to_i #to_i

convert.cs


int num = int.Parse(str);
string str = (string)123;

##if statement

if.c_&_if.java_&_if.js


if(num == 100 && score > 1000){
 //処理文
}
 else if(num < 100 || score != 1000){
 //処理文
}
else{
 //処理文
}

if.py


if num == 100 and score > 1000:
 #Processing statement
elif num > 100 or score != 1000:
 #Processing statement
else:
 #Processing statement

if.rb


if num == 100 && score > 1000
 #Processing statement
 elsif num > 100 || score != 1000
 #Processing statement
else
 #Processing statement
end
# (Processing statement if conditional expression) is also possible

##switch statement

switch.c_&_switch.java


switch(num){
 case 0:
 //処理文
   break;
 case 1:
 //処理文
   break;
 default:
 //処理文
   break;
}

#case statement

case.rb


case x
 when 1
 #Processing statement
 when 2
 #Processing statement
 else
 #Processing statement
end

##input

input.c


 scanf ("% d", & num); // Integer input
 scanf ("% lf", & data); // Real number input
 scanf ("% c", mozi); // Enter characters
 scanf ("% s", str); // String input

input.py


 count = input () #variable type irrelevant

input.java


 import java.util.Scanner; // call external library

class Main{
 public static void main(String[] args){
 Scanner scanner = new Scanner (System.in); // Initialize Scanner
 String name = scanner.next (); // Waiting for input from the console
 }
}

input.rb


 The input received by count = gets #gets is a string

##Array & List

array.c


int a[] = {1, 2, 3, 4};
int a[3][4];

image

The image is http://www.cc.kyoto-su.ac.jp/~yamada/programming/array.From html

array.java


String names[] = {"apple","banana"};

array.js


var names = ["apple","banana"];
 console.log (names.length); // Get the number of elements in the array

array.rb


names = ["apple","banana"]
 puts names.size # Get the number of elements in the array
 names.push ("grape") # Add element to end of array

list.py


lists = ["apple","banana",200,300]
 lists.append ("pizza") #Add element to end of list

##dictionary(dict) (Associative array) (hash)

dict.py_&_dict.js


 fruits = {"apple": 200, "banana": 300} // Variable = {"key": value, "key": value}

hash.rb


 fruits = {"apple" => "200", "banana" => "300"} #variable = "key" => "value"}
 puts fruits ["apple"] # Retrieving values
 fruits ["grape"] = "400" #Add data

##symbol A feature in Ruby. Although it looks like a string in the code, it is faster than a string object for comparison, search, and reference. Often used for hash keys.

symbol.rb


fruits = {:apple => "200", :banana => "300"}
puts fruits[:apple]

#each statement A method that iterates over an array or hash in order from the first data

each.rb


 fruits.each do |key,value|  #Array.each do |variable|
 puts "#{key}:#{value}"
end

##for statement

for.c_&_for.js


for(int i = 0; i < 10; i++){
 //処理文
}

for.java


for(int i = 0; i < 10; i++){
 System.out.println(i);
}

String names = {"apple","banana"};
 for (int i = 0; i <names.length; i ++) {// Count the number of elements in the array with the length method
 System.out.println("eat" + names[i]);
}

exfor.java


String names = {"apple","banana"};
 for (String name: names) {// The element itself of the array names is assigned to the variable name
 System.out.println(name);
}

for.py


for i in range(0,10):
 print(i)

for.rb


 for i in 0..9 do # (do can be omitted)
 puts i
end

##while statement

while.c_&_while.java_&_while.js


int x = 1;
while(x <= 100){
 //処理文
 x++;
}

while.py


x = 1
while x <= 100:
 #Processing statement
 x += 1

while.rb


i = 1
 while i <= 100 do #do can be omitted
 #Processing statement
 i += 1
end

##Method(function)

func.c


include <stdio.h>
int number(int num){
 printf ("The number is% d \ n", num);
  num += 1;
  return num;
}

int main()
{
  int data = number(1);
  printf("%d\n",data);
  return 0;
}

func.py


# Scope of variable name From here (range where variables can be used)
def hello(name): 
  print("hello" + name)
  name = "hello"
  return name  
# Scope so far

hi = hello("world")
print(hi)

func.java


class Main{
 public static void main(String[] args){
  String hi; 
  hi = hello("world");
  System.out.println(hi);
 }

 public static String hello(String name){
  System.out.println("Hello" + name);
  name = "hello";
  return name;
 }
}

func.js


function number(num){
  console.log(num);
  num += 1;
  return num;
}

var data = number(1);
console.log(data);

func.rb


def number(num)
 puts num
 num += 1
 return num
end

data = number(1)
puts(data)

In Ruby, each type of object has its own method(↓ is an example)

func1.rb


 puts "Hello" .length # Output the number of characters in the string object
 puts 3.14.round # Output rounded numbers
 puts "Hello" .reverse # Invert characters and output

##Library(module)(header file)

module.c


include <~.h>

module.py_&_module.java


import ~

##What I noticed / notes -It seems that the specifications are quite different between Python 2 and Python 3. -In Python you can use either single or double quotes(I think)

When you can overload: When the number of arguments is different, when the argument types are different, when the order of the arguments is different

When you can't overload: When only the name of the argument is different, when only the return value is different

I will add more and more!

Recommended Posts

Basic writing of various programming languages (self-memo)
Basic level performance evaluation of programming languages
Features of programming languages [Memo]
The popularity of programming languages
Write classes (or class-like) in various programming languages
Network programming (basic)
Transition animation of the most popular programming languages (#programming languages #popular)
Basic Python writing
Numerical representation of days of the week in various languages
[Note] Beginning of programming
Recruitment of programming masters
Basic operation of pandas
Basic usage of Jinja2
Basic operation of Pandas
Basic usage of SQLAlchemy
HMAC in various languages
Basic knowledge of Python
Various processing of Python
Basic processing of librosa
A memo of writing a basic function in Python using recursion
[For beginners] A word summary of popular programming languages (2018 version)