Five languages basic grammar comparison (C #, Java, Python, Ruby, Kotlin)

Five languages basic grammar comparison
(C #, Java, Python, Ruby, Kotlin)

table of contents

-[Console output](#console output)

--[Variable Declaration](#Variable Declaration)

-[Conditional branch](# Conditional branch)

-[Repeat processing](# Iterative processing)

-Collection

Console output

C#

//System is optional
System.Console.Write("Hello ");
Console.Write("World");
>>> Hello World

Console.WriteLine("Hello ");
Console.WriteLine("Workd");
>>> Hello
    World

Java

System.out.print("Hello ");
System.out.print("World");
>>> Hello World

System.out.println("Hello ");
System.out.println("World");
>>> Hello
    World

Python


print("Hello")
print("World")
>>> Hello
    World

print("Hello ", end="")
print("World")

>>> Hello World

Ruby


print 'Hello '
print 'World'
>>> Hello World

puts 'Hello'
puts 'World'

>>> Hello 
    World

Kotlin

print("Hello ");
print("World");
>>> Hello World

println("Hello ");
println("Workd");
>>> Hello
    World



Variable declaration

C#

String stra = "a";
string strb = "b";
int num = 1;
Boolean tr = true;
bool fa = false;

Java

String str = "a";
int num = 1
boolean bool = true

Python No type declaration required

str = "a" #String
num = 1 #int
tr = True #Boolean

Ruby
No type declaration required
Boolean type does not exist

str = "a" #String
num = 1 #int

Kotlin

val str = "a" //String
//Variables declared with val cannot be changed

var num = 1 //int
//Variables declared with var can be changed
    num = 2

var tr = True //Boolean



Conditional branch

C#

///if
if(Condition A){
Process A
}
else if(Condition B){
Process B
}
else{
//If the above conditions are not met
Process C
}


//switch
switch (variable){
case Condition A:
Process A
            break;

case Condition B:
Process B
            break;

    default:
            //If the above conditions are not met
Process C
            break;
  }

Java

//if
if(Condition A){
Process A
}
else if(Condition B){
Process B
}
else{
    //If the above conditions are not met
Process C
}


//switch
switch (variable){
case Condition A:
Process A
            break;

case Condition B:
Process B
            break;

    default:
            //If the above conditions are not met
Process C
            break;
  }


Python

if condition A:
Process A

elif condition B:
Process B

else:
Process C
#if
if condition A then
Process A

elsif condition B then
Process B

else then
    //If the above conditions are not met
Process C
end


#case
case variable
when condition A then
Process A
when condition B then
Process B
else
  //If the above conditions are not met
Process C

end

Kotlin

//if
if(Condition A){
Process A
}
else if(Condition B){
Process B
}
else{
    //If the above conditions are not met
Process C
}

//when
when (variable) {
Condition A-> {
Process A
    }
Condition B-> {
Process B
    }
    else -> {
Process C
    }

//when can be judged by directly entering an expression such as variable == condition.
when {
Conditional expression A-> {
Process A
    }
Conditional expression B-> {
Process B
    }
    else -> {
Process C
    }
}



Iterative processing

Example 1: Output numbers from 1 to 100

C#

for (int i = 1; i <= 100; i++){
    Console.WriteLine(i);
}

Java

for (int i = 1; i <= 100; i++){
    System.out.println(i);
}

Python

for i in range(1, 101):
    print(i)        

Ruby

#3 types
puts [*1..100]

100.times{|n| println n+1}

1.upto(100){|n| p n}

Kotlin

for (i in 1..100) println(i)



Example 2: Output all the contents of the array

C#

int[] array = new int[10]{1, 2, 4, 8, 16, 32, 64, 128, 256, 512};

foreach(int n in array)
{
  Console.WriteLine(n);
}


Java

int[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};

for (int n : array) {
    System.out.println(n);
}

Python

array = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512];

for n in array:
    print(n)   

Ruby

array = Array[1, 2, 4, 8, 16, 32, 64, 128, 256, 512];

for n in array do
    puts(n)   
end

Kotlin

val arr = arrayOf(1, 2, 4, 8, 16, 32, 64, 128, 256, 512)
for (n in arr) {
  println(n)
}



collection

Array

C#

int[] ary = {1,10,100};


Java

int[] ary = {1,10,100};


Python

#In Python, array cannot be used without importing
import array
 
ary = array.array('i',[1, 10, 100]) 
        #array.array(Mold,[Value 1,Value 2,Value 3])   

Ruby

array = [1,10,100]
//Ruby array can be changed
array.push(200)
array[2] = 50

p array
>> [1,10,50,200]

Kotlin

// [1, 2, 3]make
val list = arrayOf(1, 2, 3)

//Create a size 3 array filled with nulls
var arr: Array<Int?> = arrayOfNulls(3)



list

C#

//Declaration
 var list = new List<string>();
//var object name= new List<Mold>(Element count);

//add to
      list.Add("Tokyo");
      list.Add("Osaka");
      list.Add("Nagoya");

//Delete
      list.Remove("Osaka");

//output
      Console.WriteLine("[{0}]", string.Join(", ", list));
      Console.WriteLine(list[1]);

//Output result
>>> [Tokyo, Nagoya]
    Nagoya


Java

import java.util.ArrayList;
import java.util.List;

//Declaration
List<String> list = new ArrayList<String>();
//List<Types of things to put in the list>Object name=type of new List<Types of things to put in the list>(Element count)
		
//add to
	list.add("Tokyo");
	list.add("Osaka");
	list.add("Nagoya");
//Delete
	list.remove("Osaka");
//output
	System.out.println(list);
	System.out.println(list.get(1));

//Output result
>>> [Tokyo, Nagoya]
    Nagoya


Python

list = ["Hello", 1, "world", 2, "!"]

#add to
list.append(5)

#Delete
list.remove(1)

#output
print(list)
print(list[3])

#Output result
>>> ['Hello','world', 2, '!',5]
    !

Ruby Ruby doesn't have List because Array plays the same role as List

Kotlin

// List<Int>make. The contents of List cannot be changed.
val list = listOf(1, 2, 3, 4, 5)

// MutableList<Int>make. You can change the contents here
val mlist = mutableListOf(1, 2, 3, 4, 5)

//add to
mlist.add(6)

//Delete
mlist.remove(3)

//output
println(mlist[0])
println(mlist)
>>> 1
    [1, 2, 4, 5,6]



Management by key

C#

Dictionary<int,String> dict = new Dictionary<int,String>();
//Dictionary<Key model name,Value type name>Object name= new Dictionary<Key model name,Value type name>()

//add to
dict.Add(1,"aaa");
dict.Add(2,"bbb");
dict.Add(3"ccc");

//Delete
dict.Remove(2)

//output
Console.WriteLine(dict[3])
>>> ccc

//Output all
foreach (KeyValuePair<int, string> item in dict){
    Console.WriteLine("[{0}:{1}]", item.Key, item.Value);
}
>>> [1:aaa]    
    [2:bbb]

Java

import java.util.HashMap;
import java.util.Map;

//Declaration
Map<Integer,String> map = new HashMap<Integer,String>();
//Map<Key model name,Value type name>Object name=new Map type<Key model name,Value type name>()

//add to
	map.put(1,"aaa");
	map.put(2,"bbb");
	map.put(3,"ccc");

//Delete
	map.remove(2);

//output
	System.out.println(map.get(3));
>> ccc

//Output all
	for (int key : map.keySet()) {
        System.out.println(key + ":" + map.get(key));
    }
>>> 1:aaa
    3:ccc

Python

#Declaration (initialization)
dict = {1:"aaa", 2:"bbb", 3:"ccc", "ddd":4}
#Object name= {key:value , key;value}

#add to
dict["peach"] = 5

#Delete
dict.pop(2)

#output
print(dict[3])
print(dict["ddd"])
>>> ccc
    4

#Output all
print(dict)
>>> {1: 'aaa', 3: 'ccc', 'ddd': 4, 'peach': 5}

Ruby

#Use Hash object

hash = {1 =>"aaa", 2 =>"bbb", 3 => "ccc", "ddd" => 4}
#Object name= { key => value}

#add to
hash[5] = "eee"

#Delete
hash.delete[2]

#output
puts hash["ddd"]
>>>4

#Output all
puts hash.inspect
>>> {:1=>aaa, :3=>cccc :ddd=>4 :5=>eee}

Kotlin

//Map<Int,String>Can't be changed
val map = mapOf(1 to "aaa", 2 to "bbb")
//val object name= mapOf(key to value)

// MutableMap<Int, String>Can be made and changed
val mmap = mutableMapOf(1 to "aaa", 2 to "bbb")
mmap[1] = "ccc" 

//add to
mmap[3] = "ddd"

//Delete
mmap.remove(2)

//output
println(list[1])
>>> ccc

//Output all
for((key, value) in map)
    println("${key} to ${value}")
>>> 1 to ccc
    3 to ddd





Recommended Posts

Five languages basic grammar comparison (C #, Java, Python, Ruby, Kotlin)
Java and Python basic grammar comparison
Python, Java, C ++ speed comparison
msgpack deserialization performance comparison (C ++ / Python / Ruby)
Closure 4 language comparison (Python, JavaScript, Java, C ++)
[Basic grammar] Differences between Ruby / Python / PHP
Learn Python! Comparison with Java (basic function)
Python3 basic grammar
paiza POH ec-campaign (C # / Java / Python / Ruby) # paizahack_01
Comparison of Python and Ruby (Environment / Grammar / Literal)
Python basic grammar / algorithm
Python basic grammar (miscellaneous)
Python basic grammar note (4)
Python basic grammar note (3)
Python basic grammar memo
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Basic Python 3 grammar (some Python iterations)
Java VS PHP VS Python VS Ruby
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (3)
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
AtCoder Beginner Contest 170 A Problem "Five Variables" Explanation (C ++, Python, Java)
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
[Swift / Ruby / Python / Java] Object-oriented programming
Multi-stage selection (Go / C # / Ruby / Python)
Call popcount from Ruby / Python / C #
Ruby Python Java Case insensitive sorting
Basic grammar of Python3 system (dictionary)
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
(Java, JavaScript, Python) Comparison of string processing
Eating and comparing programming languages: Python and Ruby
[Python] I personally summarized the basic grammar.
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
Differences between Ruby and Python (basic syntax)
Basic grammar of Python3 system (included notation)
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Various Web Application Framework memory usage comparison survey (Go, Ruby, Python, Node.js, Java)