ตัวอย่างการบรรยายพร้อมผู้ให้คำปรึกษาซึ่งเป็นส่วนหนึ่งของหลักสูตร Codegym University ลงทะเบียนสำหรับหลักสูตรเต็ม


"ฉันอยากจะบอกคุณเล็กน้อยเกี่ยวกับการเปรียบเทียบตัวแปรในภาษาจาวา "

"คุณรู้จักตัวดำเนินการเปรียบเทียบที่ง่ายที่สุดแล้ว – น้อยกว่า (<) และมากกว่า (>)"

"ใช่."

"ยังมีตัวดำเนินการเช่น เท่ากับ (==) และไม่เท่ากับ (!=) เช่นเดียวกับ น้อยกว่าหรือเท่ากับ (<=) และมากกว่าหรือเท่ากับ (>=)"

"ตอนนี้เริ่มน่าสนใจแล้ว"

"โปรดทราบว่าไม่มีตัวดำเนินการ =< หรือ => ใน Java!"

" เครื่องหมาย = ใช้สำหรับการดำเนินการกำหนด นั่นเป็นเหตุผลที่เครื่องหมายเท่ากับ (==) สองตัวถูกใช้เพื่อทดสอบความเท่าเทียมกันในการตรวจสอบว่าตัวแปรไม่เท่ากันให้ใช้ ตัวดำเนินการ != "

"ฉันเห็น."

"เมื่อเปรียบเทียบตัวแปรสองตัวใน Java โดยใช้ตัวดำเนินการ == เรากำลังเปรียบเทียบเนื้อหาของตัวแปร"

"ดังนั้น สำหรับตัวแปรดั้งเดิมค่าของตัวแปรจะถูกเปรียบเทียบ "

"สำหรับตัวแปรอ้างอิงการอ้างอิงจะถูกเปรียบเทียบสมมติว่าเรามีวัตถุที่เหมือนกันแต่แตกต่างกัน เนื่องจากการอ้างอิงถึงตัวแปรเหล่านั้นแตกต่างกันการเปรียบเทียบจะแสดงว่าไม่เท่ากัน นั่นคือผลการเปรียบเทียบจะเป็นเท็จการเปรียบเทียบการอ้างอิงจะเป็นจริงเฉพาะในกรณีที่การอ้างอิงทั้งสองชี้ไปที่วัตถุเดียวกัน "

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

รหัส คำอธิบาย
1
int a = 5;
int b = 5;
System.out.println(a == b);
เปรียบเทียบประเภทดั้งเดิม
จริงจะแสดงบนหน้าจอ
2
Cat cat1 = new Cat("Oscar");
Cat cat2 = cat1;
System.out.println(cat1 == cat2);
เปรียบเทียบการอ้างอิง
จริงจะแสดงบนหน้าจอ
ตัวแปรทั้งสองเก็บการอ้างอิงไปยังวัตถุเดียวกัน
3
String s = new String("Mom");
String s2 = s;
System.out.println(s == s2);
เปรียบเทียบการอ้างอิง
จริงจะแสดงบนหน้าจอ
ตัวแปรทั้งสองเก็บการอ้างอิงไปยังวัตถุเดียวกัน
4
Cat cat1 = new Cat("Oscar");
Cat cat2 = new Cat("Oscar");
System.out.println(cat1 == cat2);
เปรียบเทียบการอ้างอิง
เท็จจะแสดงบนหน้าจอ
ตัวแปรสองตัวอ้างอิงวัตถุ Cat ที่เหมือนกัน แต่ไม่เหมือนกัน
5
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s == s2);
เปรียบเทียบการอ้างอิง
เท็จจะแสดงบนหน้าจอ
ตัวแปรสองตัวอ้างอิงวัตถุสตริงที่เหมือนกัน แต่ไม่เหมือนกัน
6
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s.equals(s2));
เปรียบเทียบวัตถุ _
จริงจะแสดงบนหน้าจอ
ตัวแปรทั้งสองอ้างอิงวัตถุสตริงที่เหมือนกัน

"อ้อ ฉันเกือบลืม! นี่คือแบบฝึกหัดบางส่วนสำหรับคุณ:"

4
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Minimum of two numbers
All search and sort algorithms are based on comparisons. You'll be able to handle these very soon, if you so desire. In the meantime, we suggest starting with something small: write a program to find the minimum of two numbers. Find it and then display it. And if the numbers are the same, display either of them.
4
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Maximum of four numbers
Finding the maximum is an n-ary operation (an operation on n numbers) that returns the largest of several numbers. Never mind. We have no need for such definitions at the secret CodeGym center. We're here to learn how to write code. In this task, you need to use the keyboard to enter four numbers. Then determine the largest of them and display it on the screen.
8
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Sorting three numbers
Planet Linear Chaos is populated by isomorphs. They are believed to have invented sorting algorithms. Everything in their heads is extremely well-ordered. They only issue planetary visas to people who know at least 7 sorting algorithms. Let's take our first step toward Linear Chaos: Read three numbers from the keyboard, put them in descending order, and then display them on the screen.
4
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Jen or Jen?
Jen, Company X's admin, learned how to pilot a space ship and flew away to another planet. People in Company X are good and sincere. It's just that they're scatterbrained and they mix up names. So they decided that the new administrator would also be called Jen. Let's help Company X find their Jen: write a program that checks the identity of two entered names.