image-ru-01-05

"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
public class MyFirstClass
{
   public static void main(String[] args)
   {
      int a, b, c;
      String s1, s2;
      System.out.println("Enter two numbers");
      a = new Scanner(System.in).nextInt();
      b = new Scanner(System.in).nextInt();
      c = a + b;
      System.out.println("The sum is " + c);
   }
}
Program MyFirstProgram;
Var
   a, b, c: Integer;
   s1, s2: String;
Begin
   WriteLn("Enter two numbers");
   ReadLn(a);
   ReadLn(b);
   c := a + b;
   WriteLn("The sum is ", c);
End.

"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
public static void main(String[] args)
{
   System.out.println("Ho-ho-ho!");
}
Program body
Begin
   WriteLn("Ho-ho-ho!");
End.
Function/method
double sqr(double a)
{
   return a * a;
}
Function
Function Sqr(a: Real): Real
Begin
   Sqr := a * a;
End;
Function with void return type
void doubleWrite(String s)
{
   System.out.println(s);
   System.out.println(s);
}
Procedure
Procedure DoubleWrite(s: String);
Begin
   WriteLn(s);
   WriteLn(s);
End;

"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
String name;
Variable called name that is a String.
String getName()
{
}
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."