Good Java Document Website
Today, I found a good site for java documents, it even has plugin for mozilla, marks here. It can greatly simplify my work, many thanks to the guys.
Abstract some points here:
"JDocs is a comprehensive online resource for Java API documentation. All the javadocs for a variety of popular packages are loaded into our db-driven system, and users can contribute their own notes to virtually any class, field, method. In short, JDocs provides a knowledge base defined around the major Java api's themselves, so you can find the information you're looking for right where it should be... in the documentation!"
Enjoy~
( 2004年08月17日, 05:53:42 下午 GMT+08:00 ) Permalink
Wonderful first day
It's really wonderful start of the first day's match. China won 4 gold medals, 1 silver medal and 1 copper medal. And currently, China was in the first place in the Golden Metal List. Best wishes to China, hope we will award more and more golden metals. :-)
By the way, what affect me most is the veteran Yifu Wang. I even can remember the scene he lost the champion of 1996 olympic games. He is so strong, hope we can also see him in the 2008 Beijing's field. Add oil, Yifu Wang, add oil, China.
( 2004年08月15日, 12:59:05 下午 GMT+08:00 ) Permalink
Can I use "==" to compare two String? :P
These days, when I'm reading some other guy's code, I encounter some problem. In these code, I can read following codes:
JButton button = new JButton("OK");
button.setActionCommand("OK");
button.addActionListener(this);
......
actionPerformed(e) {
String s = e.getActionCommand();
if(s=="OK")
......
}
Look, it use "==" but not "equals" here. "Oh, it must be a bug!", I told myself. But, wait, when I excuted the program, it worked quite well. And till this moment, I remembered, current powerful JVM will maintain a constant pool for String, so maybe two "OK" will reference the same place. So, such mechanism will work.
But, does Java Language Specification has such standard? and all JVM will follow this standard? I continue to dig into it.
First, I found such item in JLS 3.10.5 -- String literals.
Abstract some points here:
Literal strings within the same class ($8) in the same package ($7) represent references to the same String object ($4.3.1).
Literal strings within different classes in the same package represent references to the same String object.
Literal strings within different classes in different packages likewise represent references to the same String object.
Strings computed by constant expressions ($15.28) are computed at compile time and then treated as if they were literals.
Strings computed at run time are newly created and therefore distinct.
The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
So, this is the feature of String class, every JVM should follow. And, when I read the code of String.java, I found such comments in method "intern".
public String intern()
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~So good:-)When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are interned. String literals are defined in $3.10.5 of the Java Language Specification.
It's quite clear about the case I encountered. Then I understand, this is not a bug, but the trick :P. But, why he use "==" but not "equals", I think that will be more clear and more easy to read, I think there must be other guys to be puzzled as me :-).
I think the author must care more about the performance than the code, the performance of "==" sure if better than "equals", especially when the String is long~.
Ok, everything clear now, and sure, not only String has such pool. For example:
Integer i = 1;
Integer j = 1;
what will System.out.println(i==l) output? I think you can get the right answer. :P
( 2004年08月14日, 05:25:10 下午 GMT+08:00 ) Permalink 评论 [5]
JDS & Redhat now can run on my Dell GX270 Box
I met a problem when installing Java Desktop System and Redhat Linux into my Dell OPTIPLEX GX270 Box, the X server can not startup. When dig into a lot, I finally solved the problem. It's the bios's fault, after upgrading the Bios from A03 to A04, and changing the video RAM setting from 1M to 8M, it works.
Mark the article here. ( 2004年08月10日, 09:59:16 上午 GMT+08:00 ) Permalink
Events Handling in Java and C#
Recently, a friend of mine discussed the Event handle issues in C# and Java with me, sounds interesting. I would like to blog the discussion and my investigation here.
C# (pronounced C Sharp) is a programming language developed by MS as part of its .NET platform. When I saw it the first time, I ask myseft:"is it another version of Java?". Just a joke, C# is extremely similar to Java: GC, VM, single inheritance, interfaces, packages(use "using" instead of "import" :P), pure OO etc. However, C# also invent many new features, delegate is a famous one.
Ok, let's talk about the event-handle issues. In Java, we use the well-known Observer Pattern (Gamma et, Design Patterns) to implmented the events. For example, if we wanna to handle the Button's Click event, we simply add a ActionListener to this button:
JButton button = new JButton("ok");
button.setActionCommand("ok");
button.addActionListener(new Handler());
......
class Handler implements ActionListener {
public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if (command == "OK") {
onOK();
}
......
}
}
Or we can even add listener in following way:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if (command == "OK") {
onOK();
}
......
}
});
In C#, Anders Hejlsberg introduced the new keyword "delegate", I think it's actually like the type-safe function pointers. Anders really love the function pointers, he bring this to Delphi & Visual J++ too. Let's first take a look at how this keyword works:
In Java, Since the listener rely on the interfaces, so, we can not call the methods without knowledge of the target object. For example:
public class Class1 {
public void show(String s) {};
}
public class Class2 {
public void show(String s) {};
}
A more complex example is the method whose name can be variant, you can find detail information in this article.
Although these two classes have common method, but because they do not share a common interface, so, we can not call them in a uniform way. How to solve the problem? In Java, we can use the Proxy Pattern(Gamma et, Design Patterns) and Adapter Pattern(Gamma et, Design Patterns) or use the reflection mechanism to solve the problem. In C#, ok, it's delegate's work.
public delegate void show(String s);
show s1 = new show(new Class1().show);
s1("Test");
s1 = new show(new Class2().show);
s1("Test");
The most important use of delegates is for event handling:
Button button = new Button();
button.Click += new EventHandler(onOK); // BTW: we can see, method is the first-class object in C#.
private void onOK(...) {
......
}
Ok, that is the mechanism C# handle events. How about it? I think everyone has his cents. In my opnion, at the first look, it's very attractive to me, things get easy now. But soon I feel uncomfortable. I don't think this function pointer-like mechanism should exist in a pure OO languages :P. So, I would rather to use patterns to keep the code clean.
To summarize:
At the above situation:
1) If I can controll all the code, then I would rather retrofit a common interface that has "show" method;
2) If things are not that easy, for example: all classes are library codes, or method has different name, I'd like to use adapter and proxy pattern to solve this problems.
Just my cents. :-)
Reference:
A Java Programmer Looks at C# Delegates
( 2004年08月06日, 07:17:42 下午 GMT+08:00 ) Permalink 评论 [8]
George's Blog:P
Even George is blogging:P, but......
You can find more here. ( 2004年08月05日, 09:06:51 下午 GMT+08:00 ) Permalink
Mark my friend's homepage:-)
He is YuePing Zhang, my schoolmates in college, and now in America. Quite a good guy~
Mark here. ( 2004年08月05日, 05:26:55 下午 GMT+08:00 ) Permalink
$500 a hole:-)
Quite interesting, people who report a security hole will be paid $500 by Mozilla Foundation. Detail in here
Along with more and more security holes are found in IE recently, quite many people switched to Mozilla and Opera to avoid the security problems of IE. But, unluckily, soon security bugs are found in Mozilla and Opera too. So, is there a secure browser? You see, brower's security is very important to some people, such as those who trade through B2B website and those who do shopping from the internet.
So, I think Mozilla's Bonus Program is quite helpful and will be welcome by the users, and will IE do that too?:P
( 2004年08月04日, 03:43:27 下午 GMT+08:00 ) Permalink
Best wishes to China Soccer Team
![]() |
China are through and the celebrations begin [Adnan Hajj/WSG] |
Last night, after 120 minutes's match, China Soccer Team beat Iran Soccer Team 4:3. This is a wonderful match, China will meet Japan, the last champion in Saturday's final.
I watched the match in my dorm with other two guys, we are really strained during the match. LiYe even dare not to see the match when the penalties began. Any way, we won finally:-)
Best wishes to China, I will go to bar to watch the match with our 70's@Weekend club members. Wonderful match, wonderful beer, enjoy:-) ( 2004年08月04日, 10:46:10 上午 GMT+08:00 ) Permalink 评论 [3]
"SUN, Beach, Belle, I'm coming", Wonderful Trip~~~
7.31, the last weekend, I go to FeiCui Island resort with my friends.
We are all the guys born in the 70's, and formed a 70's@Weekend club together. We always play football, eat, outgoing in the weekend.
This Saturday, we got up at 3:00 and set out at 5:00. We arrived at ChangLi at 8:00. After buying some water, beer, banger from the supermarket near the railway station, we take a taxi to FeiCui Island. When we arrived, we were deeply entranced with the lookout of the beach. We played football in the beach, bask in the warm sunshine, swim in the sea. During the night, we sleep in the tent, chat with others, and enjoy the Moon in ShiWu:-). The second moring, we got up at 4:30 in order to see the sunrise, but due to the cloudy sky, we miss the sunrise spectacle.
By the way, One of my friend WUBAI really long for the trip, he is a fun of football and wanna to show us his skill in the beach. But his company ask him to work overtime in Sunday, so he didn't go with us, what a pity~. Here, I would like to reference one word of him to be the title "SUN, Beach, Belle, I'm coming:-)". Hope this would not be seen by his girlfriend :P.
( 2004年08月02日, 06:15:28 下午 GMT+08:00 ) Permalink 评论 [4]
Feeling about Java
Before joining Sun, I am the developer in windoze platform, and C++ is my favourite programming languages, I like it very much. I finished nearly all my project with it, even my graduate thesis. I implemented a simple cross-platform component model (LEO) in my gradudate thesis, and design a large project base on it. LEO is just like XPCOM of Mozilla, hehe, but a simple and light one I think.
After join Sun, I have the chance to use Java, a great programming language after a long time of "language drought". To be frank, I am not the fans of Java before, because I think it's a simple C++. I ever told my friends that maybe these guys can not fully understand and use C++ well, so they invent Java. But after read some books ("Thinking in Java" & "Effective Java") about Java and use it for about 2 months, I begin to realize what ridiculous I was.
Java's aim is to simply the programming work, is to help the programmer to develop more efficient(with J2SE1.5, you can understand Java Team's guys are really care about how to simply our developer's job). Most important, Java is the language of internet, sound good, right? C++ is more like the system programming language, it is a powerful and very expressive language. But it is also difficult and programmer-unfriendly. With it, you can and you must manage every thing(like "new" & "delete" issues).
To summarize Java's advantage I feel:
1) Cross-Platform: :-)It sure is the first reason;
2) GC: hehe, I never need to remember when and where to delete;
3) Single inherit and container: Even better with generic types in container; I am really a fan of C++'s Stand Templete Library, but it's more nature in Java because of it's single inherit(sure I admire A.Stepanov, he can implement this in C++, great guy~);
4) Interface: this is the true Component & binary-compatible languages;
5) Import instead of include: same reason, binary but not source code compatible; Even better with static import in tiger;
6) JavaDoc: I seldom comment before, but now often;
7) Many useful librarys: besides Java's own package and technology(such as Jave Web Start), JUnit, Ant are all great work I think, help me much;
8) Exception Handling: Every method must declare the exception and every caller must catch it, hehe, good error-handling approach;
9) Java & Pattern: The last one I want to imension is Java & Pattern, Java is so well implemented with well-known patterns, such as use observer pattern(Event Listener), decorator pattern(Reader). Also there's some bad-implemented, such as Stack:-), it should be composite but not inherit.
Today, one of my friends ask what I am doing in Sun, I tell him I am writting a desktop application in Java. He is really supprised with my answer, "what? Can java be used to write desktop application? I bet it must be slow...". En, Many friends of mine are using Java, but mainly J2EE. Every time talk about the desktop application, "slow" must be the comments. But I think, VM are more and more powerful, machines are faster, memory are larger, Java can also be used to write large desktop applications(dont't forget it can cross-platform, hehe). YongZhong office is a good example.
Mark two pretty interesting Java vs C++ performance benchmark:
http://www.kano.net/javabench/
http://developers.slashdot.org/developers/04/06/15/217239.shtml?tid=108&tid=126&tid=156
( 2004年08月02日, 09:03:48 上午 GMT+08:00 ) Permalink 评论 [20]


