Switching from Java to D is not a big step. There are a few differences because D is compiled for native code. As both languages are somehow influenced by or inherited from C or C++ syntax, there won't be a big problem :-)
Let me show you some D code that works and is very similar to java code:
class Foo
{
int myInteger;
this()
{
myInteger = 1;
}
int bar()
{
for (ubyte i=0; i<2; i++) {
myInteger *= 2;
}
return myInteger;
}
}
void main()
{
Foo myFoo = new Foo();
writefln("%d",myFoo.bar());
writefln("%d",myFoo.bar());
writefln("%d",myFoo.bar());
} |
Output:
Difference: In D, we don't have to use a class name that is same name as the file and we import std.stdio to write to the display:
import std.stdio;
int main(char[][] args)
{
char[] world() {
return "world!";
}
writefln("Hello "~world());
return 0;
} |
Difference: The main function must return an int to the system.
Difference: In D "+" and "~" are not the same! The first one (plus) is only for numbers (int b=5+3, int a=2+b) and the concatenation ("~") is only for arrays (which strings are):
char[] str = "Paradise";
char[] text = "Welcome to "~str~"."; |
See also:
Language Reference, An advanced tutorial: File read and write, Java to D
|