• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compareTo() and intern()

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s = "aha";
String s1 = "aba";
System.out.println(s.compareTo(s1));
System.out.println(s.compareTo(""));
Ans:
6
3
What exactly these integer outputs represent apart from explaining that is lexicographically greater than s1
intern()
String s = "java";
s.intern();
from jdk
***********
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
****************
In both the cases, string is only returned..
so what does it mean by "refernce to this string object is returned" ??/
ragu
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ragu,
compareTo works as follows:
this string and the parameter string are lexicographically compared, i.e. we take the index of the first character being different in both string.
Here "aha" and "aba" the first different character index is 1.
Then, the following number is returned
"aha".charAt(1) - "aba".charAt(1) => 'h' - 'b' => 104 - 98 = 6
If no index is found (i.e. -1) then the different of the length of the string is returned
"aha".length() - "".length() => 3 - 0 => 3
When a String is interned that String will be available in the String pool of the class. When you request the same String you just receive a reference to that String from the pool and not a new String object.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
Ragu,
compareTo works as follows:
this string and the parameter string are lexicographically compared, i.e. we take the index of the first character being different in both string.
Here "aha" and "aba" the first different character index is 1.
Then, the following number is returned
"aha".charAt(1) - "aba".charAt(1) => 'h' - 'b' => 104 - 98 = 6
If no index is found (i.e. -1) then the different of the length of the string is returned
"aha".length() - "".length() => 3 - 0 => 3
When a String is interned that String will be available in the String pool of the class. When you request the same String you just receive a reference to that String from the pool and not a new String object.
HIH



Wonderful!!
Thankx Val
I appreciate your precise comments
ragu
 
reply
    Bookmark Topic Watch Topic
  • New Topic