Chapter one in my Java textbook is basically ten pages of basic Java information about the virtual machine and how Java is compiled and how to locate the API and etc..
The first real get down to it moment is about half way through, where they talk about the main method and then they build a simple "hello world" type program.
The program goes something like this:
public class Welcome{
public static void main(String args[]){
// displays java is da bomb in the console
System.out.println("Java is da bomb");
}
}
The top line of course defines the class, and we all know that Java programs must contain at least one class. It also notes that by convention class names start with a capital letter.
The second line of course outlines the main method. In order to run a class a program must contain a main method. The main method is where programs in Java are executed.
The third line is a comment line. Two slashes makes the compiler skip that line. It is there to communicate what the program is doing and for leaving messages in plain old English.
The fourth line is a method that tells the program to output what is in the parenthesis to the console. It is called System.out.println. Notice that the line ends in a semicolon, which in Java is the statement terminator.
Other things to note in this simple program are the curly brackets! These are for forming what is known as a programming block. Every class has a block that is used to group data and methods together. Every method also has a block that groups the code in the method together.
Soooooooooooo... how would we do this in RUBY???
Please wait while I Google "what is terms in red in ruby?"
Here is what i found....
a far as a class in ruby goes, you can define one as simply as typing in "class"
for example,
class Boogers
or
class Hockey
the naming convention is still the same for classes, you should start a class name with a capital letter.
With regards to the main method, in Ruby there is no need to define it or declare it.. without getting too deep into it (meaning take my word for it) it is there but you dont need to see it or know about it. (at least not at this point)
Long story short...Ruby will execute the code as it sees it!
Comment Line in Ruby can be done with a (#).
like this:
# this is the method that computes the radius
# displays cust name
no different than Java other than the // is now a #.
In Java we use System.out.println to output to the console, in Ruby you can simply use "puts"
like this :
puts " Ruby is the best language ever"
puts " your mother is not very attractive"
You might notice that in the above lines that there is no statement terminator!
Ruby doesnt use em.
The class block is ended with "end"
How simple is that?
Very simple.
Here is our first Java program translated to ruby.
class Welcome
# displays Ruby is way more bomb than Java in the console
puts "Ruby is way more bomb than Java"
end
That will conclude chapter 1.
I like it so far.
Now onto the excercises.
No comments:
Post a Comment