Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Java
When C# was introduced in the early 2000s, it was accused of being "just an imitation of Java". (Java is 5 years older than C#.) The two languages diverged much more since that time, but the similarities are still quite obvious. Therefore, this section will be much shorter than the previous one.
Checking the sample code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
public class Main {
public static void main(String args[]) {
// Scanner in = new Scanner(System.in);
// String m = in.nextLine();
String m = "CG";
String[] c = {"00", "0"};
StringBuilder b = new StringBuilder();
for (char w : m.toCharArray())
b.append(String.format("%1$" + 7 + "s", Integer.toBinaryString((int)w)).replace(' ', '0'));
StringBuilder a = new StringBuilder();
a.append(c[b.charAt(0) - '0'] + " 0");
for (int i = 1; i < b.length(); i++)
if (b.charAt(i) == b.charAt(i - 1))
a.append("0");
else
a.append(" " + c[b.charAt(i) - '0'] + " 0");
// System.err.println("Debug messages...");
System.out.println(a.toString());
}
}
Enter to Rename, Shift+Enter to Preview
Looking at the syntax
We can see similar features as in the C# sample solution, like:
- classes, a static method as main, a strong and static type system, arrays, type casting, iterators, loops, conditionals, etc. The syntax is slightly different, but I will not go into detail for these features.
Some differences:
- Java's
String
is immutable, soStringBuilder
class is used to append characters to the solution character by character. It is converted to a string only at the last step by calling itstoString()
method. - In C#, even the primitive types are objects. Java features wrapper classes for the same role. (e.g.
Integer
forint
). - Of course, there are many more differences, which are not visible from such a short sample code, and are beyond the scope of this short introductory article.
Other characteristics
- Both Java and C# are 'managed' languages. Java compiles to Java bytecode, which is then run on a JVM (Java Virtual Machine). Later on, we will see some other languages relying on the same JVM as its runtime environment.
- By using JIT, their speeds fall in the same range.
- Java is very popular in the enterprise application development, due to its "write once, run anywhere" philosophy.
- Besides its Standard Edition (SE), Java has additional editions, such as Enterprise Edition (Java EE) which provides additional facilities for developing and running large, multi-tiered, reliable, and secure enterprise applications. Interestingly, in the past few years Java EE has lost some grounds, because cloud-native or microservice-based applications are more written in plain Java, using other frameworks such as Spring.
Resources to check
Coming next...
After checking 2 compiled, managed languages with a strong type system, let's explore some languages with a very different approach: weakly typed and interpreted! We start by looking at PHP!
Suggested playgrounds
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content