To set an example of what I mean by "elegant and concise way", let's print a result of a simple equation x * y = z, where x, y and z are placeholders for input values and the calculated result respectively.
Ruby - main.rb
#!/usr/bin/ruby
x = 5; y = 4
puts "#{x} * #{y} = #{x * y}"
$ruby main.rb
5 * 4 = 20
Scala (as of 2.10) - Main.scala
object Main {
def main(args: Array[String]) {
val (x, y) = (5, 4)
println(s"$x * $y = ${x * y}")
}
}
$scalac Main.scala
$scala Main
5 * 4 = 20
PHP - main.php
<?php
$x = 5; $y = 4; $z = $x * $y;
echo "$x * $y = $z";
?>
$php main.php
5 * 4 = 20
The closest equivalent in Java is the use of String.format
Java - Main.java
public class Main {
public static void main(String []args){
int x = 5, y = 4;
System.out.println(String.format("%d * %d = %d", x, y, x * y));
}
}
$javac Main.java
$java Main
5 * 4 = 20
The code snippets were created and executed via codingground
No comments:
Post a Comment