Java Tips · StringBuffer is like a String that can be modified. 13 . Composition vs. Inheritance...
Transcript of Java Tips · StringBuffer is like a String that can be modified. 13 . Composition vs. Inheritance...
Java Tips
Professeur Patrick BELLOT
TELECOM ParisTech
Variable number of arguments… The … notation
public void m(String param, Data… data) { for (Data d : data) { // do something with d
} m2(123,”abc”,data) ; // can be transmitted }
Must be in last position.
Valid calls: m(“abc”) ; m(“abc”,d1) ; m(“abc”,d1,d2) ; etc.
2
Iterators
You may use all data structures of Java proposed by the Collection Framework : LinkedList, ArrayList, etc.
All these structures are able to give an iterator:
LinkedList<Data> list = new … ; Iterator<Data> iter = list.iterator() ;
Iterators are used to run through the data structure:
while (iter.hasNext()) { Data d = iter.next() ; // do something with d … }
3
Object default initialization
The default values are as follows:
* For the boolean type the default value is false. * For the byte type the default value is byte (0). * For the short type the default value is short (0). * For the int type the default value is 0. * For the long type the default value is OL. * For the char type the default value is u000. * For the float type the default value is 0.0f. * For the double type the default value is 0.0d. * For the object reference type the default value is null.
4
Formatting strings
import java.text.*;
String message = "The opposite of {0} is {1}.” ;
Object values[] = { "yes", "no" } ;
String s = MessageFormat.format(message, values) ;
System.out.println(s) ;
5
External packages
It is possible to use external packages that are not present in your development or execution environment.
For instance commons-lang-2.4 provides very useful string functions.
Such packages come under the form of a jar file.
The jar file must be put somewhere and included in the CLASSPATH.
Then classes of the package can be used as usual classes.
6
Internationalization Identify Culturally Dependent Data
• Messages • Labels on GUI components • Online help • Sounds • Colors • Graphics • Icons • Dates • Times • Numbers • Currencies • Measurements • Phone numbers • Honorifics and personal titles • Postal addresses
• Page layouts
7
Internationalization of visible texts
import java.util.* ;
public class I18NSample {
static public void main(String[] args) { String language = args[0] ; // example: “fr” String country = args[1] ; // example: “en”
Locale currentLocale = new Locale(language, country);
ResourceBundle messages = ResourceBundle.getBundle("MessagesBundle”,currentLocale) ;
System.out.println(messages.getString(”say_ hello")); } }
8
Internationalization of visible texts
File MessagesBundle.properties contains default values:
say_hello = Hello !
File MessagesBundle.fr_FR.properties contains values for language fr and country FR:
say_hello = Bonjour !
File MessagesBundle.fr_CA.properties contains values for language fr and country CA. Etc.
9
Internationalization of visible texts
For complex sentences with parameters, use formatting strings in conjunction with internationalization:
• en_EN : “You are {0} years old.”
• fr_FR : “Vous avez {0} ans.”
To process singular and plural options, see ChoiceFormat.
For the rest of internationalization, take a look at the SUN Java tutorial…
10
Scientific computations
Attention, computing with real floating point numbers is not trivial. The problem is not specific to JAVA.
Problem : as you do more floating point operations, you dramatically loose precision in the final result.
Floating point types:
• float : -3.4 x 1038 to 3.4 x 1038, greater than 1.17 x 10-38
• double : -1.8 x 10308 to 1.8 x 10308, greater than 2.22 x 10-308
• BigDecimal : arbitrary precision. 2
11
Programming efficient programs Avoid creating objects when possible. Reuse objects.
Prefer virtual methods than interface (2 x longer) when possible.
Use the final declaration when possible for constant attributes and methods that will not be redefined.
Avoid Enum, use constant integers.
Avoid inner classes access to the outer class when possible.
Use multithreading when appropriate.
Avodi synchronization when possible.
Bufferizes I/O. 12
Optimizing programs
Don’t forget the 90/10 rule.
Run benchmarks.
Use the right data structures. Read carefully the JAVA documentation.
Sometimes, rewrite the JAVA classes when they are too powerful for what you need and when this power has a cost. Base the rewritten class on the existing source for the replaced code.
Avoid String computations, use StringBuffer when appropriate. A StringBuffer is like a String that can be modified.
13
Composition vs. Inheritance When doing object programming, you are sometimes faced to the well-known
problem of composition vs. inheritance.
For instance, you want to design a Queue. • Do you want the class Queue to inherit LinkedList ? And then, you enrich the
class LinkedList with the methods enqueue() and pull(). • Or do you want the class Queue to contain an attribute of class LinkedList.
JAVA recommends to use composition because a queue is not a linked list but a queue can be implemented using a linked list.
Note that JAVA sometimes breaks this rule: a Stack is not a Vector. Usually, breaking this rule means many problems in the future.
The paradigmatic example is that of a car: a car is not a motor but a car contains a motor. A car is not a wheel but has four wheels.
14
Google is your friend…. Don’t forget about Google !
Everything about Java is on the web.
Example: JAVA SE6 How to SSL socket
Don’t buy books, they are incomplete and will be soon back-released…
Example: JAVA SE6 How to access a MySQL data base
Don’t try to learn everything by heart.
Be careful with forums !!!!!!!!
Example: JAVA SE6 LinkedList
15
Programmers
Programmer (noun): An organism that can turn caffeine into code.
Programmers are people that make programs that run on Turing machines.
Their ultimate goal is to be able to approximately finish their work within the deadlines imposed by their employer.
Programmers are average persons that are more intelligent than the average person, because they understand how computers work, which is beyond the reach of the average human.
Whatever type of programmer you choose to hire, make sure he is not exposed to sunlight at any point. Any light brighter than the glow of a monitor will kill a programmer.
16
That’s all folks !
17