ebook img

1000 Java Tips PDF

854 Pages·26.627 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview 1000 Java Tips

Even More Tips at http://JavaA.com and http://JavaFAQ.nu every day!!! Copyright Info This book can be copied and distributed in any form. Nobody, except for copyright owners, has a right to modify the book content. It can be published online on any site if this copyright info is displayed as well. All the tips can be used in other publications or published separately if this copyright info is added. This book is completely free and nobody has a right to charge for this book, its distribution and so on. Copyright owners keep the right on mistake that could be made in the book and have no responsibility for possible consequences of such error. The copyright owners of this book are: Alexandre Patchine – http://JavaA.com , http://JavaFAQ.nu and Dr. Heinz M. Kabutz in “More Java Tips by Dr. Heinz M. Kabutz” section - http://www.javaspecialists.co.za You can contribute to this book if you have valuable Java advice, tip, and code example and so on. Your copyright info – name, site will be added as well. Reviews "This book has saved me hours of research while coding Java. From the obvious to the obscure, this book answers the real world questions that bring development to a halt. And the authors really know their stuff. A great help.” - Robin Kay from user opinions on CNET about free first edition "My 5.5 years Java experience can to be improved even more!” - Net Rambler from Europe from user opinions on CNET about free first edition "Nice format to learn Java. Excellent e-book for developers who do not have mentors around in the initial stages.” From user opinions on CNET about free first edition Copyright © 2005 http://JavaA.com http://JavaFAQ.nu [email protected] Even More Tips at http://JavaA.com and http://JavaFAQ.nu every day!!! Foreword Hello dear reader! "1000 Java Tips" is my collection of tricky questions and answers from my newsletters for last four years and answers I gave to people in my emails. Let me ask you a few questions: 1. Are you preparing for Java certification exam and want to be sure that you will pass it from first attempt? This book gives you real examples how to use Java. 2. Are you looking for a job as a Java programmer and want to be prepared for the Java interview questions? 3. Are you looking for a good design idea? You have come to right place! This e-Book will give you more knowledge, better perspective to programming and computer science especially. It covers wide range of questions. My "1000 Java Tips" e-book will help you to understand Java much better and be well prepared for your Java Certification Examination. The e-Book is made in the form of questions and answers. I included also the big section of Advanced Java Tips by Dr. Heinz Kabutz where topics are bigger. They are well written articles! Every article exploring one area and gives you examples. This e-book is an easy-to-navigate huge collection of Java Tips. Easy style, right proportion of humor and Java lets you read them at once, in one breath! Author of “1000 Java Tips” e-book, Alexandre Patchine. Copyright © 2005 http://JavaA.com http://JavaFAQ.nu [email protected] Even More Tips at http://JavaA.com and http://JavaFAQ.nu every day!!! Table of Contents Copyright Info............................................................................................... 2 Reviews ....................................................................................................... 2 Foreword...................................................................................................... 3 Table of Contents........................................................................................... 4 1. Introduction............................................................................................... 7 2. Applets..................................................................................................... 8 3. Databases & Beans....................................................................................25 4. Distributed systems....................................................................................32 5. File Systems.............................................................................................34 6. Graphics, AWT, Swing.................................................................................42 7. General Java.............................................................................................66 8. Java Bugs..............................................................................................175 9. Java Hardware........................................................................................176 10. Java Tools and Software..........................................................................177 11. Job, fun...............................................................................................193 12. Miscellaneous........................................................................................196 13. Mobile Java...........................................................................................218 14. Networking, JSP, Mail... etc......................................................................230 15. Operation Systems and Java.....................................................................251 16. RMI.....................................................................................................260 17. Security...............................................................................................265 18. Servlets, Servers, Mail etc........................................................................269 19. Sound, Speech and Multimedia.................................................................283 20. String, text, numbers, I/O........................................................................290 21. Threads................................................................................................316 22. Code Examples......................................................................................332 23. More Java Tips by Dr. Heinz M. Kabutz.......................................................346 …… [Issue 001] - Deadlocks..........................................................................346 …… [Issue 002] - Anonymous Inner Classes......................................................349 …… [Issue 003] - Logging part 1.....................................................................353 …… [Issue 004] - Logging part 2.....................................................................357 …… [Issue 005] - Dynamic Proxies - Short Tutorial.............................................361 …… [Issue 006] - Implementation code inside interfaces......................................367 …… [Issue 007] - java.awt.EventQueue............................................................369 …… [Issue 008] - boolean comparisons............................................................374 …… [Issue 009] - Depth-first Polymorphism......................................................377 …… [Issue 010] - Writing GUI Layout Managers.................................................381 …… [Issue 011] - Hooking into the shutdown call...............................................387 …… [Issue 012] - Setting focus to second component of modal dialog ....................390 …… [Issue 013a] - Serializing GUI Components Across Network............................394 …… [Issue 013b] - Follow-up.........................................................................400 …… [Issue 014] - Insane Strings.....................................................................403 …… [Issue 015] - Implementing a SoftReference based HashMap..........................407 …… [Issue 016] - Blocking Queue ...................................................................412 …… [Issue 017a] - Switching on Object Handles.................................................416 …… [Issue 017b] - Follow-up.........................................................................420 …… [Issue 018] - Class names don't identify a class...........................................425 …… [Issue 019] - Finding Lost Frames.............................................................429 …… [Issue 020] - Serializing Objects Into Database............................................433 …… [Issue 021] - Non-virtual Methods in Java...................................................437 …… [Issue 022] - Classloaders Revisited: "Hotdeploy".........................................440 Copyright © 2005 http://JavaA.com http://JavaFAQ.nu [email protected] Even More Tips at http://JavaA.com and http://JavaFAQ.nu every day!!! …… [Issue 023] - Socket Wheel to handle many clients .......................................455 …… [Issue 024] - Self-tuning FIFO Queues.......................................................461 …… [Issue 025] - Final Newsletter...................................................................468 …… [Issue 026] - Package Versioning..............................................................475 …… [Issue 027] - Circular Array List................................................................479 …… [Issue 028] - Multicasting in Java..............................................................487 …… [Issue 029] - Determining Memory Usage in Java.........................................494 …… [Issue 030] - What do you Prefer?.............................................................501 …… [Issue 031] - Hash, hash, away it goes!......................................................508 …… [Issue 032] - Exceptional Constructors - Resurrecting the dead.......................513 …… [Issue 033] - Making Exceptions Unchecked................................................518 …… [Issue 034] - Generic Types with Dynamic Decorators...................................523 …… [Issue 035] - Doclets Find Bad Code ..........................................................532 …… [Issue 036] - Using Unicode Variable Names................................................536 …… [Issue 037] - Checking that your classpath is valid........................................541 …… [Issue 038a] - Counting Objects Clandestinely.............................................546 …… [Issue 038b] - Counting Objects Clandestinely - Follow-up..............................552 …… [Issue 039] - Why I don't read your code comments .....................................554 …… [Issue 040] - Visiting your Collection's Elements...........................................557 …… [Issue 041] - Placing components on each other...........................................563 …… [Issue 042] - Speed-kings of inverting booleans...........................................567 …… [Issue 043] - Arrgh, someone wants to kill me!............................................571 …… [Issue 044] - Review: Object-Oriented Implementation of Numerical Methods....576 …… [Issue 045] - Multi-line cells in the JTable...................................................582 …… [Issue 046] - "The compiler team is writing useless code again ..."...................586 …… [Issue 047] - Lack of Streaming leads to Screaming......................................589 …… [Issue 048] - Review: The Secrets of Consulting...........................................595 …… [Issue 049] - Doclet for finding missing comments........................................599 …… [Issue 050] - Commenting out your code?...................................................607 …… [Issue 051] - Java Import Statement Cleanup..............................................610 …… [Issue 052] - J2EE Singleton....................................................................617 …… [Issue 053] - Charting unknown waters in JDK 1.4 Part I................................627 …… [Issue 054] - HashMap requires a better hashCode() - JDK 1.4 Part II ..............631 …… [Issue 054b] - Follow-up to JDK 1.4 HashMap hashCode() mystery..................639 …… [Issue 055] - Once upon an Oak ...............................................................640 …… [Issue 056] - Shutting down threads cleanly................................................646 …… [Issue 057] - A Tribute to my Dad, Hans Rudolf Kabutz..................................652 …… [Issue 058] - Counting bytes on Sockets.....................................................655 …… [Issue 059] - When arguments get out of hand............................................666 …… [Issue 059b] - Follow-up to Loooong Strings................................................675 …… [Issue 060] - Nulling variables and garbage collection....................................677 …… [Issue 061] - Double-checked locking.........................................................681 …… [Issue 062] - The link to the outer class......................................................684 …… [Issue 062b] - Follow-up and Happy New Year!............................................689 …… [Issue 063] - Revisiting Stack Trace Decoding..............................................691 …… [Issue 064] - Disassembling Java Classes....................................................697 …… [Issue 065] - Wait, Cursor, Wait!...............................................................699 …… [Issue 066] - Book Review: Java Performance Tuning by Jack Shirazi...............708 …… [Issue 067] - BASIC Java.........................................................................714 …… [Issue 068] - Appending Strings................................................................719 …… [Issue 069] - Treating Types Equally - or - Life's Not Fair!..............................725 …… [Issue 069b] - Results of last survey..........................................................730 …… [Issue 070] - Too many dimensions are bad for you......................................731 …… [Issue 070b] - Multi-Dimensional Arrays - Creation Performance......................734 …… [Issue 071] - Overloading considered Harmful..............................................736 Copyright © 2005 http://JavaA.com http://JavaFAQ.nu [email protected] Even More Tips at http://JavaA.com and http://JavaFAQ.nu every day!!! …… [Issue 072] - Java and Dilbert..................................................................743 …… [Issue 073] - LinkedHashMap is Actually Quite Useful....................................746 …… [Issue 074] - GoF Factory Method in writing GUIs.........................................751 …… [Issue 075] - An Automatic Wait Cursor: WaitCursorEventQueue .....................761 …… [Issue 076] - Asserting Locks...................................................................790 …… [Issue 077] - "Wonderfully disgusting hack"................................................796 …… [Issue 078] - com.maxoft.memory.MemoryCounter for Java 1.4......................798 …… [Issue 079] - Generic toString()................................................................806 …… [Issue 080] - Many Public Classes in One File...............................................814 …… [Issue 081] - Catching Exceptions in GUI Code.............................................816 …… [Issue 082] - TristateCheckBox based on the Swing JCheckBox.......................822 …… [Issue 083] - End of Year Puzzle ...............................................................828 …… [Issue 083b] - End of Year Puzzle Follow-up................................................830 …… [Issue 084] - Ego Tripping with Webservices................................................831 …… [Issue 085] - Book Review: Pragmatic Programmer.......................................839 …… [Issue 086] - Initialising Fields before Superconstructor call............................844 …… [Issue 086b] - Initialising Fields before Superconstructor call (Follow-up)..........848 24. XML.....................................................................................................850 25. About The Author...................................................................................852 Copyright © 2005 http://JavaA.com http://JavaFAQ.nu [email protected] Even More Tips at http://JavaA.com and http://JavaFAQ.nu every day!!! 1. Introduction Do not lose time here, please go ahead and read my book right now! J Copyright © 2005 http://JavaA.com http://JavaFAQ.nu [email protected] Even More Tips at http://JavaA.com and http://JavaFAQ.nu every day!!! 2. Applets Ł Ł Question: What are restrictions for an applet? What are applets prevented from doing? Answer: In general, applets loaded over the net are prevented from reading and writing files on the client file system, and from making network connections except to the originating host. In addition, applets loaded over the net are prevented from starting other programs on the client. Applets loaded over the net are also not allowed to load libraries, or to define native method calls. If an applet could define native method calls, which would give the applet direct access to the underlying computer. Ł Question: How can my applet require some optional packages (specific vendor and versions)? Answer: Since version 1.3 Java introduced support for an expanded set of Jar-file manifest attributes. The following portion of a sample manifest file illustrates the attributes that are available for use by optional packages. Extension-Name: javax.help Specification-Vendor: Sun Microsystems, Inc Specification-Version: 1.0 Implementation-Vendor-Id: com.sun Implementation-Vendor: Sun Microsystems, Inc Implementation-Version: 1.0 Note that all of the above manifest attributes should be included in an optional package's manifest. If the plug-in looks for but doesn't find one of these attributes in an optional package's manifest, it will assume that the optional package is not suitable for the applet. Note: Optional packages are the new name for what used to be known as standard extensions. The "extension mechanism" is that functionality of the Java 2 SDK and Java 2 Runtime Environment that supports the use of optional packages. Optional packages are packages of Java classes (and any associated native code) that application developers can use to extend the functionality of the core platform. The extension mechanism allows the Java virtual machine (VM) to use the classes of Copyright © 2005 http://JavaA.com http://JavaFAQ.nu [email protected] Even More Tips at http://JavaA.com and http://JavaFAQ.nu every day!!! the optional extension in much the same way as the VM uses classes in the Java 2 Platform. The extension mechanism also provides a way for needed optional packages to be retrieved from specified URLs when they are not already installed in the Java 2 SDK or Runtime Environment. From SUN's description of the Java Extension Mechanism Ł Good to know: Invoking modal popup dialogs from an applet's stop or destroy methods is not recommended if you use Java 1.4.1 and below. Using modal popup dialogs inside these methods could result in problematic behavior, including NullPointerException, the dialog box not being displayed, the dialog box remaining alive even after the applet was destroyed, or in the worst case, browser hang. This problem has been fixed in 1.4.2. Ł Question: Do I need special server software to use applets? Answer: No. Java applets may be served by any HTTP server. On the server side they are handled the same as any other file, such as a text, image, or sound file. All the special action happens when the applet class files are interpreted on the client side by a Java technology-enabled browser, such as HotJava browser or 1.x or Netscape 3.x/4.x. source: http://java.sun.com/products/jdk/faq.html#A8 Ł Question: I know that applets have limited possibility to do many things. It is about network connections, file reading/writhing and more. Can applet read all system properties and if not how many of them are restricted? Answer: Applets can read quite many of system properties by using: String ss = System.getProperty(String key): java.version java.vendor java.vendor.url java.class.version os.name os.arch os.version file.separator path.separator line.separator Applets are prevented from reading these system properties: java.home java.class.path user.name user.home user.dir source: http://java.sun.com/sfaq/ Ł Question: I have an applet. I have successfully added the class to a JAR, but when the web page tries to load the applet, the browser gives us an error "can't find class file". When unzipping the JAR, my added class is present. Answer: Do not forget to add the "ARCHIVE" option to the APPLET tag like this Copyright © 2005 http://JavaA.com http://JavaFAQ.nu [email protected] Even More Tips at http://JavaA.com and http://JavaFAQ.nu every day!!! <applet code=my.class archive=archive.jar width=10 height=10> </applet> If "archive" is missing the browser will not know that your class is in a jar file... Ł Question: Is there a way to write information to cookies through an Applet? Answer: Applets are not allowed to access information on the Client Side...it overrides the Sandbox restriction... However, JavaScript can access cookies and Java applet can talk to JavaScript. So, a Java applet indirectly has access to cookies... An example of JavaScript <--> Java was published in my newsletter before... Look for the JSObject class from Netscape which works in IE as well. Ł Question: I am writing a chat applet and I would like to put icons in user messages. Is it possible? Answer: A text area can parse HTML. Add image (jpg or gif) into html code and it will display your icons. Ł Question: Can one applet load another applet on runtime, and can these two applets talk? Answer: the Answer found on "Internet related technologies" site: "Yes one applet can load another applet. And since you need both the applet to communicate, you need to have both applets in the same page. But the problem in this case is that both the applets will load independently of each other. You can control the behaviour of the other applet from the first one. But your requirement is of loading another applet from the first one. You can have two frames in a page. In one frame there will be your first applet which will load automatically when the page is loaded. And the other frame will contain a blank page. Now on certain event in the first applet you can use "this.showDocument("URL") " to make a call to another applet which you want to load. Give the target parameter as the other frame. (You will have to give the name of the other frame) Now when these two applets are loaded they can exchange data in their life time." http://developer.irt.org/script/4074.htm Ł Question: How do I hide system properties that applets are allowed to read by default? Answer: There's no way to hide the ten system properties (see below) from applets loaded into a Java-enabled browser. The reason is that the browsers don't consult any external files as part their Java configuration, as a security precaution, including the Copyright © 2005 http://JavaA.com http://JavaFAQ.nu [email protected]

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.