"Hi. My name is Laga Bilaabo. I'm an extraterrestrial and this ship's physician. I hope we'll be good friends."
"Me too."
"On my home planet, we use the advanced Pascal programming language instead of the backward Java language. Look at this side-by-side comparison of Java and Pascal code:"
Java | Pascal |
---|---|
|
|
"This is the same program written in two different languages. As you can see, it has fewer lines in Pascal, which is evidence that Pascal is superior to Java."
"I thought this example might help you better understand Java if you've ever seen Pascal before."
"No, I haven't. Still, it's interesting to compare two programming languages."
"Fair enough. I'll continue then."
"In Pascal, we place code in the program body, procedures or functions. In Java, this process is simplified: the program body, procedures, and functions are all replaced by functions called methods."
Java | Pascal |
---|---|
Main method
|
Program body
|
Function/method
|
Function
|
Function with void return type
|
Procedure
|
"In the Pascal column, I see the words 'program body', 'function', and 'procedure', but in Java they are all called methods. That's a little weird."
"Yes, we extraterrestrials find it very weird. But humans like to unify everything."
"In Java, all code is part of a method, so you don't even need to write the word Function, like in Pascal, to declare a method."
"It's all very simple. If a line of code looks like Type + Name, it's a declaration of either a method or a variable. If the name is followed by parentheses, then it's a declaration a new method. If there are no parentheses, then it's a declaration of a variable."
"Declarations of variables and methods in Java are very similar. See for yourself:"
Code | Description |
---|---|
|
Variable called name that is a String . |
|
Method called getName that returns a String . |
"But that's not all. In Java, methods cannot exist in isolation. They must be inside a class. Thus, when humans need to write one small program in Java, they must first create a class, declare a main method in it, and only then can they write their code in the method. These earthlings are so weird!"
"Diego dropped by earlier today and asked me to give you these tasks. I hope you like them."
GO TO FULL VERSION