This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what should i know about Strings in the 1.4 exam

 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what should we know about Strings for the 1.4 exam? in SUN 1.4 exam objectives, it say: Describe the significance of the immutability os String objects.
i've already done that, is there anything else i should be aware of, such as, StringBuffer class, Constructors and Methods for the String Class, Constructors and Methods for the StringBuffer Class ...etc
 
Toby Davis
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Know these methods for String:
charAt, concat, equalsIgnoreCase, length, replace, substring, trim, toString, toLowerCase, toUpperCase (also "equals" should be in the list - in fact, it's an Object method so it should be obvious - so understand how equals can be overriden and implemented, and its contract... it may show up on the exam for Strings)
StringBuffer: append, insert, reverse, toString
(Let me know if I missed one above, anyone...)
Be aware of incorrect String method being applied to a StringBuffer, or vice-versa. Understand the substring's arguments, where first element is index-based (0-based) but the second element is position-based (1+). If you understand String's immutability, then you'll have no problems understanding how String objects cannot be changed, while StringBuffer objs can - and how to catch that in code questions on the exam.
If you have the K&B book (and if you don't, I highly recommend it), Chapter 6 was excellent on this objective...
[ August 09, 2003: Message edited by: Toby Davis ]
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vicken,
Maybe may have you a look a this code's snippet:

And guess the output...
Maybe you'll have some surprise...
Cyril.
 
venu gopal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cyril,
It's interesting. The result is
false
true
true
true
As long as the method didn't change the contents of a String it returns the same String without changing it's reference. Here s1 and s2 are pointing to different address and there is no change in the remaining strings.
public class EssaiString{public static void main (String args[]){ String s1 = "ABC";
String s2 = new String(s1);
String s3 = new String(s1);

System.out.println(s1==s2);
/* Referring to different strings */

System.out.println(s2.trim() == s2.toUpperCase());
/* s2.trim() doesn't change the string and s2.toUpperCase() doesn't change the string. It returns true. */

System.out.println(s3.concat("") == s3.replace('D','E'));
/* s3.concat("") doesn't change the string and s3.replace('D','E') doesn't change the string.(there is no D to replace). It returns true. */

String s4 = s1.toString();
/*s1.toString returns same string */

System.out.println(s4==s1);
/* s4 is refering s1 ("ABC"). it returns true. */
}
}
Cheers,
Venu.
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job Venu!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic