• 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

java.lang

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had some queries and hoping to bundle them all in 1:-

1st:-



The first argument is (zero-based) . If call has two arguments,the ending argument is not zero-based.

so 1st argument = index 1 = B
2nd arg. = 2nd one from left = B

and substring(3) = D

I would like to know why output is BDE.

2:-

answer = flase, false

knowing equals is used only to compare objects,I was thinking object S1 [ a string] is equal to object s2 , also a string.
Thanks for any clarification.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

if in the Substring method the second arguement is not provided , then the output results in the string that starts from the index thats provided till the end of the string that invoked it.

for eg .
String s = "ABCDE";
System.out.println(s.substring(3)); // results in "DE"

Shruti
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s.substring(x) returns a substring that begins with the character at position x to the end of the original string s. So if String s = "ABCDE",
returns "DE".

In your second question, remember that Strings are immutable. s1.toLowerCase() does not change s1, and s3.replace('b', 'a') do not change s1 or s3. Instead new Strings are returned, which in this example have no references.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the risk of repeating what has bee said previously, the API for class String explains it:

[ October 19, 2004: Message edited by: Barry Gaunt ]
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx. all for the substring part , and the "emptiness".substring(9) e.g is a nice one indeed.

about my 2nd question, I was told .
s1.toLowerCase() does not change s1, and s3.replace('b', 'a') do not change s1 or s3.[so far so good]
Instead new Strings are returned, which in this example have no references

as I see it:-
s1------>holds reference to "A"[string object]
s2------>holds reference to "a"[string object]
s3------>holds reference to "b"[string object]
the toLowerCase & replace create an object[abandoned] with no reference variable.[so far so good]
so seeing s1.equals(s2) tells me '"A"[which is a string object] is equal to "a"[which too is a string object].
am I making it unneccesary complicated for myself or ......
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're almost there! s1.equals(s2) is not telling you, it's asking you if they are equal.

So, is s1 equal to s2?
Is s2 equal to s3?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s1.toLowerCase() will change the case of s1 but it will not be stored in s1 rather it will be lost.
If you print s1 and s3 after the lines
s1.toLowerCase(); s3.replace('b','a');
you will still get s1 as "A" and s3 as "b"

If you do s1=s1.toLowerCase();
and then you compare it with s2 then you will get desired output(true).

I hope it helps
Nitin
 
reply
    Bookmark Topic Watch Topic
  • New Topic