1. Mbandhingake senar

Iki kabeh apik lan apik. Nanging sampeyan bisa ndeleng manawa senar s1lan s2senar pancen padha, tegese padha ngemot teks sing padha. Nalika mbandhingake senar, kepiye carane sampeyan ngandhani program supaya ora ndeleng alamat obyek String, nanging isine?

Kanggo mbantu iki, Stringkelas Jawa duwe equalsmetode. Telpon katon kaya iki:

string1.equals(string2)
Mbandhingake rong senar

Cara iki bali trueyen strings padha, lan falseyen padha ora padha.

Tuladha:

Kode Cathetan
String s1 = "Hello";
String s2 = "HELLO";
String s3 = s1.toUpperCase();

System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s2.equals(s3));
// Hello
// HELLO
// HELLO

false // They are different
false // They are different
true // They are the same, even though the addresses are different

Conto liyane:

Kode Panjelasan
"Hello".equals("HELLO")
false
String s = "Hello";
"Hello".equals(s);
true
String s = "Hel";
"Hello".equals(s + "lo");
true
String s = "H";
(s + "ello").equals(s + "ello");
true

4
tugas
Java Syntax,  tingkatwulangan
Dikunci
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
tugas
Java Syntax,  tingkatwulangan
Dikunci
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.

2. Perbandingan string sing ora sensitif huruf cilik

Ing conto pungkasan, sampeyan weruh yen mbandhingake ngasilake . Pancen, senar ora padha. Nanging..."Hello".equals("HELLO")false

Cetha, senar ora padha. Sing jarene, isine nduweni aksara sing padha lan mung beda karo kasus huruf. Apa ana cara kanggo mbandhingake lan ora nggatekake kasus huruf kasebut? Yaiku, supaya ngasilake ?"Hello".equals("HELLO")true

Lan jawaban kanggo pitakonan iki ya. Ing Jawa, Stringjinis kasebut nduweni cara khusus liyane: equalsIgnoreCase. Telpon katon kaya iki:

string1.equalsIgnoreCase(string2)

Jeneng cara nerjemahake kira-kira minangka mbandhingake nanging nglirwakake kasus . Huruf ing jeneng metode kasebut kalebu rong garis vertikal: sing pisanan yaiku huruf cilik L, lan sing nomer loro yaiku huruf gedhe i. Aja nganti bingung.

Tuladha:

Kode Cathetan
String s1 = "Hello";
String s2 = "HELLO";
String s3 = s1.toUpperCase();

System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.equalsIgnoreCase(s3));
System.out.println(s2.equalsIgnoreCase(s3));
// Hello
// HELLO
// HELLO

true
true
true

8
tugas
Java Syntax,  tingkatwulangan
Dikunci
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.

3. Tuladha tandhinganipun senar

Ayo menehi mung siji conto prasaja: umpamane sampeyan kudu ngetik rong garis saka keyboard lan nemtokake manawa padha. Iki bakal katon kaya kode kasebut:

Scanner console = new Scanner(System.in);
String a = console.nextLine();
String b = console.nextLine();
String result = a.equals(b) ? "Same" : "Different";
System.out.println(result);

4. Nuance menarik saka comparison senar

Ana siji nuansa penting sing sampeyan kudu ngerti.

Yen compiler Jawa nemokake sawetara strings podho rupo ing kode (khusus ing kode Panjenengan), banjur bakal nggawe mung obyek siji kanggo wong-wong mau kanggo nyimpen memori.

String text = "This is a very important message";
String message = "This is a very important message";

Lan iki minangka akibat saka memori:

perbandingan string

Lan yen sampeyan mbandhingake text == messagekene, sampeyan entuk true. Mula aja gumun.

Yen sakperangan alesan sampeyan pancene mbutuhake referensi supaya beda, sampeyan bisa nulis iki:

String text = "This is a very important message";
String message = new String ("This is a very important message");

Utawa iki:

String text = "This is a very important message";
String message = new String (text);

Ing loro kasus kasebut, variabel textlan messagenuduhake obyek beda sing ngemot teks sing padha.


4
tugas
Java Syntax,  tingkatwulangan
Dikunci
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.