เปรียบเทียบกับภาษาปาสคาล - 1

"สวัสดี ฉันชื่อ Laga Bilaabo ฉันเป็นคนนอกโลกและเป็นแพทย์ประจำเรือลำนี้ ฉันหวังว่าเราจะเป็นเพื่อนที่ดีต่อกัน"

"ฉันด้วย."

"บนดาวบ้านเกิดของฉัน เราใช้ภาษาโปรแกรม Pascal ขั้นสูงแทนภาษา Java รุ่นเก่า ดูการเปรียบเทียบโค้ด 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.

"นี่เป็นโปรแกรมเดียวกันที่เขียนด้วยสองภาษาที่แตกต่างกันอย่างที่คุณเห็น มันมีบรรทัดน้อยกว่าในภาษาปาสคาล ซึ่งเป็นหลักฐานว่าภาษาปาสคาลเหนือกว่าภาษาจาวา"

"ฉันคิดว่าตัวอย่างนี้อาจช่วยให้คุณเข้าใจ Java ได้ดีขึ้นหากคุณเคยเห็น Pascal มาก่อน"

"ไม่ ฉันยังไม่เคย อย่างไรก็ตาม การเปรียบเทียบภาษาโปรแกรมสองภาษาก็น่าสนใจ"

“พอแล้ว ฉันจะทำต่อไป”

"ในภาษาปาสคาล เราวางโค้ดไว้ในส่วนเนื้อหาของโปรแกรม โพรซีเดอร์ หรือฟังก์ชัน ส่วนใน Java กระบวนการนี้จะง่ายขึ้น: เนื้อหาของโปรแกรม โพรซีเดอร์ และฟังก์ชันทั้งหมดจะถูกแทนที่ด้วยฟังก์ชันที่เรียกว่าเมธอด"

ชวา ปาสคาล
วิธีการหลัก
public static void main(String[] args)
{
   System.out.println("Ho-ho-ho!");
}
ตัวโปรแกรม
Begin
   WriteLn("Ho-ho-ho!");
End.
ฟังก์ชัน/วิธีการ
double sqr(double a)
{
   return a * a;
}
การทำงาน
Function Sqr(a: Real): Real
Begin
   Sqr := a * a;
End;
ฟังก์ชันที่มีประเภทการคืนค่าเป็นโมฆะ
void doubleWrite(String s)
{
   System.out.println(s);
   System.out.println(s);
}
ขั้นตอน
Procedure DoubleWrite(s: String);
Begin
   WriteLn(s);
   WriteLn(s);
End;

"ในคอลัมน์ Pascal ฉันเห็นคำว่า 'program body', 'function' และ 'procedure' แต่ใน Java ทั้งหมดนี้เรียกว่าเมธอด มันค่อนข้างแปลกเล็กน้อย"

"ใช่ พวกเราต่างดาวคิดว่ามันแปลกมาก แต่มนุษย์ชอบที่จะรวมทุกอย่างเป็นหนึ่งเดียว"

"ใน Java โค้ดทั้งหมดเป็นส่วนหนึ่งของเมธอด ดังนั้นคุณไม่จำเป็นต้องเขียนคำว่าFunction เหมือนใน Pascal เพื่อประกาศเมธอด "

"ทุกอย่างง่ายมาก ถ้าบรรทัดของโค้ดดูเหมือนType + Nameแสดงว่าเป็นการประกาศเมธอดหรือตัวแปรถ้าชื่อมีวงเล็บตามหลัง แสดงว่าเป็นการประกาศเมธอดใหม่ หากไม่มีวงเล็บ แล้วก็เป็นการประกาศตัวแปร "

"การประกาศตัวแปรและเมธอดใน Java นั้นคล้ายกันมาก ดูด้วยตัวคุณเอง:"

รหัส คำอธิบาย
String name;
ตัวแปรที่เรียกnameว่า a String.
String getName()
{
}
เมธอดที่เรียกgetNameว่าส่งคืน a String.

"แต่นั่นไม่ใช่ทั้งหมด ใน Java เมธอดไม่สามารถอยู่แยกกันได้ ต้องอยู่ในคลาส ดังนั้นเมื่อมนุษย์ต้องการเขียนโปรแกรมเล็กๆ หนึ่งโปรแกรมใน Java พวกเขาจะต้องสร้างคลาสก่อน ประกาศเมธอดหลักในนั้นและพวกเขาสามารถเขียนโค้ดด้วยวิธีนี้ได้เท่านั้น

"ดิเอโกแวะมาเมื่อเช้าวันนี้และขอให้ฉันมอบงานเหล่านี้ให้คุณ ฉันหวังว่าคุณจะชอบ"

1
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
The great purge
Whoever wrote this program obviously did it in a hurry. Actually, that's not true: this program was written for educational purposes, and the author deliberately crammed in superfluous variables and simultaneously failed to declare necessary variables. We're going to correct this: Comment out the unnecessary variables, and declare the missing variables. Then the program will achieve universal harmony.
1
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Don't feel like it? Do it anyway.
Laziness afflicts even the best programmers. And not only programmers. Nevertheless, people have managed to become professionals by teaching themselves. So, we suggest not being lazy. Instead, display this slogan on the screen: "If you feel like it, do the task. If you don't feel like it, do it anyway". And to really remember this, display it 16 times.
5
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Square of a number
There are several ways to square a number. For example, some people write a number and then draw a square around it. This method is used by people who haven't studied anywhere. Everybody else has to multiply, remember the table of squares, ... or use a program. Your program should display the square of 5.
5
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
As simple as 2+2
In a Java application, all the actions are performed by functions. Or more accurately, methods. In our program, a kind mentor has already implemented a method (that is, written the method's code) that can calculate the sum of two numbers. All you need to do is call this method with the arguments 2 and 2. You need to do this in the main method.
1
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Choose healthy food! Choose fruit!
Display the variables whose values are names of fruit. Display each variable on a new line.