Codegym University 과정의 일부로 멘토와 함께하는 강의 스니펫. 전체 과정에 등록하십시오.


"자바에서 변수를 비교하는 방법에 대해 조금 말씀드리고 싶습니다 . "

"당신은 이미 가장 간단한 비교 연산자인 보다 작음(<)과 보다 큼(>)을 알고 있습니다."

"네."

"같음(==) 및 같지 않음(!=)과 같은 연산자도 있습니다. 또한 작거나 같음(<=) 및 크거나 같음(>=)."

"이제 점점 흥미로워지고 있습니다."

"Java에는 =< 또는 => 연산자가 없습니다!"

" = 기호는 대입 연산에 사용됩니다. 이것이 두 개의 등호(==)가 동등성을 테스트하는 데 사용되는 이유입니다. 변수가 같지 않은지 확인하려면 != 연산자를 사용하십시오 ."

"알겠어요."

"== 연산자를 사용하여 Java에서 두 변수를 비교할 때 변수의 내용을 비교합니다."

"따라서 기본 변수 의 경우 해당 값이 비교 됩니다 ."

" 참조 변수 의 경우 참조가 비교됩니다 . 동일하지만 별개의 객체가 있다고 가정합니다 . 참조가 다르기 때문에 비교 결과가 동일하지 않음을 보여줍니다. 즉, 비교 결과는 거짓 입니다 . 참조 비교는 입니다. 두 참조가 동일한 개체를 가리키는 경우에만. "

"개체의 내부 내용을 비교하기 위해 특별한 equals 메서드를 사용합니다. 이 메서드(및 Object 클래스의 모든 메서드)는 선언하지 않더라도 컴파일러에 의해 클래스에 추가됩니다. 몇 가지 예를 보여 드리겠습니다. "

암호 설명
1
int a = 5;
int b = 5;
System.out.println(a == b);
기본 유형을 비교합니다 .
true가 화면에 표시됩니다.
2
Cat cat1 = new Cat("Oscar");
Cat cat2 = cat1;
System.out.println(cat1 == cat2);
참조를 비교하십시오 .
true가 화면에 표시됩니다.
두 변수 모두 동일한 개체에 대한 참조를 저장합니다 .
String s = new String("Mom");
String s2 = s;
System.out.println(s == s2);
참조를 비교하십시오 .
true가 화면에 표시됩니다.
두 변수 모두 동일한 개체에 대한 참조를 저장합니다 .
4
Cat cat1 = new Cat("Oscar");
Cat cat2 = new Cat("Oscar");
System.out.println(cat1 == cat2);
참조를 비교하십시오 .
false가 화면에 표시됩니다.
두 변수는 동일한 Cat 개체를 참조하지만 동일한 개체는 참조하지 않습니다.
5
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s == s2);
참조를 비교하십시오 .
false가 화면에 표시됩니다.
두 변수는 동일한 String 객체를 참조하지만 동일한 객체는 아닙니다.
6
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s.equals(s2));
개체를 비교합니다 .
true가 화면에 표시됩니다.
두 변수는 동일한 String 개체를 참조합니다.

"아, 잊을 뻔 했어요! 여기 당신을 위한 몇 가지 연습이 있습니다."

4
과제
Java Syntax,  레벨 4레슨 6
잠금
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,  레벨 4레슨 6
잠금
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,  레벨 4레슨 6
잠금
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,  레벨 4레슨 6
잠금
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.