Below example demostrate String type value is immutable and every time the "+=" operation is done, the new string object will be created which is inefficent; This will be a problem when writing real projects in the perspective of performance;
//Inefficent way to do it
Strings info=""
info+="myname is bob"
info +=" "
infor +- "i am a builder";
System.out.printLn(info)
So here comes the stringbuilder:
//Efficient way to do it
Stringbuilder sb = new StringBuilder("");
sb.append("my name is sue");
sb.append(" My name is yellow ")
System.out.printLn(sb.toString);
StringBuilder s = new StringBuilder();
s.append("here").append(" is the miracle");
StringBuffer is a thread-safe version which basicly is the same as stringbuilder;
How to format properly:
System.out.println("hh\t is right\n");
System.out.printf("Totalcost %-10d\n, %.2f\n", 5, 9.6")
System.out.printf("Totalcost %-10d\n, %-6.2f\n", 5, 9.6")
for %f, it will do the round work automcatically(%.2f-->5.789=5.79)
Static (and Final)
If you want to access to a static variable, you need to access in a Class leverl; class variables are different from instance variables;
Static method only accesses to the static variable and vice verse; they are created before the actual creating of the class;
But the instance method could access the static data; //to be confirmed
<final> keyword is the jave version of constant; it needs initialized in the place where it is defined;
public final static int NUM=8
All the int variable in java will be 0 by default;
Deciding Which Java Collection to Use for Java Collections Framework
List
store lists of objects
duplicates are allowed
elements are indexed vai an integer
checking for particular item in list is slow
Looking an item up by index is fast
Iterrate thourhg lists is relatively fast
you can sort the list if you want to
//If you only add or remove items at end of list
List<String> list1= new ArrayList<String>() ;
//Removing or adding items elsewhere in the list
List<String> list2= new LinkedList<String>();
set
Only store unique values
Not indexed unlike list
Very fast to check if a particular object exists
If you want to use your own objects, you must implement hashcode() and equals();
//hashset is not ordered
Set<String> set1 = new HashSet<String>() ;
//sorted in natural order(1,2,3...,a,b,c) and must implement Comparable() for custom type
Set<String> set2 = new TreeSet<String>() ;
//elements remain in order as they are added
Set<String> set3 = new LinkedHashSet<String()> ;
map
key value pairs and the key is like a set
LIke lookup table
Retrieving a value by key is fast
Iterating over maps value is slow, if you do need to iterate, iterate over key.
if you want to use your own objects as keys, you must implement hashcode() and equals();
Map<String,String> map1 = new HashMap<String, String>() ;
//key sorted in natural order and must implement Comparable interface for custom type
Map<String,String> map2 = new TreeMap<String, String>() ;
//keys remain in order added
Map<String,String> map3 = new LinkedHashMap<String,String>() ;
//There are also Sortedset and SortedMap interface;