Skip to content

Instantly share code, notes, and snippets.

@cristobal
Created March 27, 2014 17:32
Show Gist options
  • Save cristobal/9813251 to your computer and use it in GitHub Desktop.
Save cristobal/9813251 to your computer and use it in GitHub Desktop.
public class JavaTutorial10 {
public static void main(String[] args) {
String value = "abc";
// 1. Strings are not null terminated in java
System.out.println(value.toCharArray());
System.out.println("");
// 2. Strings are immutable and final in Java
System.out.println("vh: " + value.hashCode());
System.out.println("vhl: "+ value.toLowerCase().hashCode());
System.out.println("vhu: "+ value.toUpperCase().hashCode());
System.out.println("");
// 3. Strings are maintained in String Pool
String name = "Scala";
String name_1 = "Scala";
String name_2 = new String("Scala");
if (name == name_1) {
System.out.println("both name and name_1 is pointing to same string object");
}
if (name != name_2) {
System.out.println("name_1 and name_2 are not pointing to same string object");
}
if (name != name_2.intern()) {
System.out.println("name_1 and name_2.intern() point to same string object");
}
System.out.println("");
// 4. Use Equals methods for comparing String in Java
if (name_2.equals(name_1)) {
System.out.println("name_2.equals(name_1)");
}
// 5. Use indexOf() and lastIndexOf() or matches(String regex) method to search inside String
String haystack = "Well Java may be an okay language, discussable!";
if(haystack.indexOf("Java") != -1) {
System.out.println("String contains Java at index :" + haystack.indexOf("Java"));
}
if(haystack.matches("W.*")) {
System.out.println("String Starts with W");
}
if(haystack.lastIndexOf("discussable!") != -1) {
System.out.println("String contains `discussable! at: " + haystack.lastIndexOf("Java"));
}
System.out.println("");
// 6. Use SubString to get part of String in Java
// this will return part of String str from index 5 to 9
String subString = haystack.substring(5, 9);
System.out.println("Substring: " + subString);
// 7. "+" is overloaded for String concatenation
/*
Java doesn't support Operator overloading but String is special and + operator can be used to concatenate two Strings.
It can even used to convert int, char, long or double to convert into String by simply concatenating with empty string "".
internally + is implemented using StringBuffer prior to Java 5 and StringBuilder from Java 5 onwards. This also brings point of using StringBuffer or StringBuilder for manipulating String.
Since both represent mutable object they can be used to reduce string garbage created because of temporary String. Read more about StringBuffer vs StringBuilder here.
Read more: http://javarevisited.blogspot.com/2013/07/java-string-tutorial-and-examples-beginners-programming.html#ixzz2xBUirwGg
*/
// 8. Use trim() to remove white spaces from String
String value2 = " Evil Knievel ";
System.out.println("");
System.out.println("I Break all rules like \"" + value2.trim() + "\"");
// 9. Use split() for splitting String using Regular expression
for (String arg: value2.trim().split("Evil ")) {
System.out.print(arg);
}
System.out.println("");
// 10) Don't store sensitive data in String
/*
String pose security threat if used for storing sensitive data like passwords, SSN or any other sensitive information.
Since String is immutable in Java there is no way you can erase contents of String and since they are kept in String pool (in case of String literal)
they stay longer on Java heap ,which exposes risk of being seen by anyone who has access to Java memory, like reading from memory dump.
Instead char[] should be used to store password or sensitive information. See Why char[] is more secure than String for storing passwords in Java for more details.
Read more: http://javarevisited.blogspot.com/2013/07/java-string-tutorial-and-examples-beginners-programming.html#ixzz2xBY0kTbf
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment