In Java String w1="abab", w2="abe"; System.out.println(w1.compareTo(w2));
In Java String w1="abab", w2="abe"; System.out.println(w1.compareTo(w2));
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
In Java
String w1="abab", w2="abe";
System.out.println(w1.compareTo(w2));
Expert Solution
Explanation
1) In Java string compareTo() method is used to compare two strings lexicographically.
- Each character of both the strings is converted into a Unicode value for comparison.
2) It returns the following values:
- if (string1 > string2) it returns a positive value.
- if both the strings are equal lexicographically
i.e.(string1 == string2) it returns 0. - if (string1 < string2) it returns a negative value.
3) In above program
- We string w1 = "abab" and w2 = "abe"
- It compare w1 and w2 using compareTo function
- And prints comparision result
4) It compare each character unicode value
- 'a' from w1 is same as 'a' from w2
- 'b' from w1 is same as 'b' from w2
- 'a' from w1 is not same as 'e' from w2, so result will be negative
- unicode value of 'a' is 97 and uncode value of 'e' is 101, so differece = 97-101 = -4
- So it displays -4
Step by step
Solved in 2 steps
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education