Google Dart Program Example Tutorial
Google dart is now released and its time to try google dart and see how much it offers to a programmer and whether it is suitable language to replace JavaScript or not. As a language my first impression of Google dart is great it looks so much familiar with Java my favorite programming language and this motivate me to write this hello-world example in Google Dart and execute it , run it and see how it works. Google dart has Class and Object, it has final keyword, String Class , Object class , extends keyword and many more I going to learn but it really look structure because it has google dart compiler which shows warning and error like any other structure programming language.
In this Google Dart tutorial we will see How to write Helloworld in Google Dart and How to run Google dart program using Google Dartboard Application which is a browser based application, allow you to write and run Google Dart Programs. This is the easiest and quickest way to get a feel of Google Dart language itself before trying it with HTML code. Google Dart also has compiler and html converter which allow you to convert Dart code into JavaScript and use it. We will see about how to convert Google Dart Code into JavaScript in next article. For now let's start with this step by step guide of writing Google Dart HelloWorld Example and running it.
Step by Step Guide to Write and Run Google Dart Example
I. Open http://www.dartlang.org/ in your browser.
Make sure you use Mozilla Firefox, Google Chrome or Apple Safari. Google Dartboard application won’t work on Microsoft Internet Explorer, though support is coming in may be few days or week.also if you are using Mozilla please use latest version, Google Dartboard was not working on my Mozilla Firefox 3.6 versions. Once you open this link Google Dartboard Application will be open on your browser and first example which it has is "Hello Dart" as shown in below screen.
II. Important points about Google Dart structured programming language
Now you have two options either edit the existing program or write a new program.
Before writing new program let’s see few points about google dart language.
1. Like Java google dart program will start with main () method.
2. You can declare Variable using "var" a JavaScript style or using correct type like String or int using Java Style , for example below two code are same.
String text = "NYSE is biggest Stock Market in world";
var text = "NYSE is biggest Stock Market in world";
It depends upon your choice whether you want to adopt javascript style or Java Style. Since I am a Java programmer I found Java Style more intuitive and easy to use.
3. You can assign incorrect type to variable e.g. in this example assigning "text" as int, your programe will run but Dart compiler will show warning related to incorrect type.
int text ="google dart compler will show warning but programme will run".
4. Instead of var or a formal type, you can use final keyword and your program will run successfully.
5. Each statement ends with semicolon ";" otherwise compiler will throw error "Unexpected token "IDENTIFIER expected" or "; expected"
6. For displaying value of variable we need to use interpolation or jsp expression language style "${var}" or else it will complain as "cannot resolve text".
7. We can display string as literal, exactly same as in Java. print() method is similar to Perl, java or any other language. For example
print("This is String literal");
III. Our first google dart program
main() {
String tradingSecrets = "High Frequency Trading requires extremely fast computers";
int latency = 10;
String message = "Google Dart language";
print('Hello world in ${message }');
print("This is String literal just like Java");
print( '${tradingSecrets}');
}
Output:
Hello world in Google Dart language
This is String literal just like Java
High Frequency Trading requires extremely fast computers
IV. Running your Google Dart program
Just press the Run button as shown in the above image, it will contact darn server and run your program, sometime you may get error as "The darn server failed to respond." but mostly you see out of your google dart program.
This is the first Google Dart tutorial there are more to come but this is good to start and get feeling of Google dart language. Google Dartboard application is easy to use and execute google dart program which reduce learning curve but converting google dart program to JavaScript and than running into HTML file like JavaScript requires little effort which we will see in next article.
To learn more examples see http://www.dartlang.org/docs/getting-started/index.html
Some of my older post you may find interesting