1. తీగలను పోల్చడం

ఇదంతా బాగానే ఉంది. s1కానీ మీరు మరియు స్ట్రింగ్‌లు నిజానికి ఒకే విధంగా ఉన్నాయని మీరు చూడవచ్చు s2, అంటే అవి ఒకే వచనాన్ని కలిగి ఉన్నాయని అర్థం. Stringస్ట్రింగ్‌లను పోల్చి చూసేటప్పుడు, ఆబ్జెక్ట్‌ల చిరునామాలను కాకుండా వాటి కంటెంట్‌ను చూడమని ప్రోగ్రామ్‌కి ఎలా చెబుతారు ?

దీనితో మాకు సహాయం చేయడానికి, జావా Stringక్లాస్ equalsపద్ధతిని కలిగి ఉంది. కాల్ చేయడం ఇలా కనిపిస్తుంది:

string1.equals(string2)
రెండు తీగలను పోల్చడం

trueస్ట్రింగ్‌లు ఒకేలా ఉంటే మరియు falseఅవి ఒకేలా లేకుంటే ఈ పద్ధతి తిరిగి వస్తుంది .

ఉదాహరణ:

కోడ్ గమనిక
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

మరిన్ని ఉదాహరణలు:

కోడ్ వివరణ
"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
టాస్క్
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.

2. కేస్-సెన్సిటివ్ స్ట్రింగ్ పోలిక

చివరి ఉదాహరణలో, మీరు పోలిక రాబడుతుందని చూశారు . నిజానికి, తీగలు సమానంగా లేవు. కానీ..."Hello".equals("HELLO")false

స్పష్టంగా, తీగలు సమానంగా లేవు. వారి కంటెంట్ ఒకే అక్షరాలను కలిగి ఉంటుంది మరియు అక్షరాల విషయంలో మాత్రమే భిన్నంగా ఉంటుంది. వాటిని పోల్చడానికి మరియు అక్షరాల కేసును విస్మరించడానికి ఏదైనా మార్గం ఉందా? అంటే, తద్వారా దిగుబడి వస్తుంది ?"Hello".equals("HELLO")true

మరి ఈ ప్రశ్నకు అవుననే సమాధానం వస్తోంది. జావాలో, Stringరకానికి మరొక ప్రత్యేక పద్ధతి ఉంది: equalsIgnoreCase. కాల్ చేయడం ఇలా కనిపిస్తుంది:

string1.equalsIgnoreCase(string2)

పద్ధతి యొక్క పేరు స్థూలంగా సరిపోల్చండి కాని కేసును విస్మరించండి అని అనువదిస్తుంది . పద్ధతి పేరులోని అక్షరాలు రెండు నిలువు వరుసలను కలిగి ఉంటాయి: మొదటిది చిన్న అక్షరం Lమరియు రెండవది పెద్ద అక్షరం i. అది మిమ్మల్ని గందరగోళానికి గురి చేయనివ్వవద్దు.

ఉదాహరణ:

కోడ్ గమనిక
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
టాస్క్
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.

3. స్ట్రింగ్ పోలిక ఉదాహరణ

కేవలం ఒక సాధారణ ఉదాహరణ ఇద్దాం: మీరు కీబోర్డ్ నుండి రెండు పంక్తులను నమోదు చేయాలి మరియు అవి ఒకేలా ఉన్నాయో లేదో నిర్ణయించుకోవాలి. కోడ్ ఇలా కనిపిస్తుంది:

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. స్ట్రింగ్ పోలిక యొక్క ఆసక్తికరమైన స్వల్పభేదాన్ని

మీరు తెలుసుకోవలసిన ముఖ్యమైన స్వల్పభేదం ఉంది.

జావా కంపైలర్ మీ కోడ్‌లో (ప్రత్యేకంగా మీ కోడ్‌లో) బహుళ సారూప్య స్ట్రింగ్‌లను కనుగొంటే , అది మెమరీని సేవ్ చేయడానికి వాటి కోసం ఒకే వస్తువును మాత్రమే సృష్టిస్తుంది.

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

మరియు ఫలితంగా ఏ మెమరీని కలిగి ఉంటుందో ఇక్కడ ఉంది:

స్ట్రింగ్ పోలిక

మరియు మీరు ఇక్కడ పోల్చినట్లయితే text == message, మీరు పొందుతారు true. కాబట్టి దాని గురించి ఆశ్చర్యపోకండి.

కొన్ని కారణాల వల్ల మీకు నిజంగా రిఫరెన్స్‌లు భిన్నంగా ఉండాలంటే, మీరు ఇలా వ్రాయవచ్చు:

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

లేదా ఇది:

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

ఈ రెండు సందర్భాలలో, textమరియు messageవేరియబుల్స్ ఒకే వచనాన్ని కలిగి ఉన్న విభిన్న వస్తువులను సూచిస్తాయి.


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.