Un frammento di lezione con un mentore come parte del corso Codegym University. Iscriviti al corso completo.


"Vorrei dirti qualcosa sul confronto delle variabili in Java. "

"Conosci già gli operatori di confronto più semplici: minore di (<) e maggiore di (>)."

"Sì."

"Esistono anche operatori come uguale a (==) e diverso da (!=). Oltre a minore o uguale a (<=) e maggiore o uguale a (>=)."

"Ora questo sta diventando interessante."

"Nota che non ci sono operatori =< o => in Java!"

" Il segno = viene utilizzato per le operazioni di assegnazione. Ecco perché vengono utilizzati due segni di uguale (==) per verificare l'uguaglianza. Per verificare che le variabili non siano uguali , utilizzare l' operatore != ."

"Vedo."

"Quando confrontiamo due variabili in Java usando l'operatore ==, stiamo confrontando il contenuto delle variabili."

"Così, per le variabili primitive , i loro valori vengono confrontati ."

"Per le variabili di riferimento , i riferimenti vengono confrontati . Supponiamo di avere oggetti identici ma distinti. Poiché i riferimenti ad essi sono diversi , un confronto mostrerà che non sono uguali, ovvero il risultato del confronto sarà falso . Un confronto di riferimenti sarà vero solo se entrambi i riferimenti puntano allo stesso oggetto. "

"Per confrontare i contenuti interni degli oggetti, usiamo lo speciale metodo equals . Questo metodo (e tutti i metodi della classe Object) vengono aggiunti alla tua classe dal compilatore anche se non li dichiari. Lascia che ti mostri alcuni esempi: "

Codice Spiegazione
1
int a = 5;
int b = 5;
System.out.println(a == b);
Confronta i tipi primitivi .
true verrà visualizzato sullo schermo.
2
Cat cat1 = new Cat("Oscar");
Cat cat2 = cat1;
System.out.println(cat1 == cat2);
Confronta i riferimenti .
true verrà visualizzato sullo schermo.
Entrambe le variabili memorizzano i riferimenti allo stesso oggetto .
3
String s = new String("Mom");
String s2 = s;
System.out.println(s == s2);
Confronta i riferimenti .
true verrà visualizzato sullo schermo.
Entrambe le variabili memorizzano i riferimenti allo stesso oggetto .
4
Cat cat1 = new Cat("Oscar");
Cat cat2 = new Cat("Oscar");
System.out.println(cat1 == cat2);
Confronta i riferimenti .
false verrà visualizzato sullo schermo.
Le due variabili fanno riferimento a oggetti Cat identici, ma non allo stesso.
5
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s == s2);
Confronta i riferimenti .
false verrà visualizzato sullo schermo.
Le due variabili fanno riferimento a oggetti String identici, ma non allo stesso.
6
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s.equals(s2));
Confronta oggetti .
true verrà visualizzato sullo schermo.
Le due variabili fanno riferimento a oggetti String identici

"Oh, quasi dimenticavo! Ecco alcuni esercizi per te:"

4
Compito
Java Syntax,  livello 4lezione 6
Bloccato
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
Compito
Java Syntax,  livello 4lezione 6
Bloccato
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
Compito
Java Syntax,  livello 4lezione 6
Bloccato
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
Compito
Java Syntax,  livello 4lezione 6
Bloccato
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.