Java for Complete Beginners StringBuilder and String Formatting

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值