|
JavaFAQ Home » Java Newsletters

****************************************************************** *
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * * > The Java FAQ Daily
Tips, weekly publication < * *
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * * * * Issue No: 13 29
November 2000 * * http://www.javafaq.nu/java * * * * * * Please
recommend us and our FREE "100 Java Tips" book to your * * friends and
colleagues! * * http://javafaq.nu/java/advert/our_book.shtml *
******************************************************************
Table
of Contents
1. Is there any performance or other benefit from importing
only the classes you need ... 2. Is it possible to redirect the
System.out.println to a file? 3. == and equals ()... These two still make me
confused... 4. Could some kind person please tell me how to save the object
as a file so as the same program can load it again? 5. Why do I get
message like “wrong magic number� when I am trying to run an applet? 6.
Can anyone write me a short method that lets me know what files are in a
particular directory? 7. In java, I found a lot of methods, which let you
enter a line ... They ALL wait until return is pressed. Does ...
******************************************************************
Hello
dear friends!
We prepared for publishing pdf-version (Acrobat Reader) of
our "100 Java Tips" book and planning to have it on the site next week.
So it will be possible for all of you to read this book.
We will
regularly update this book (every 2-3 weeks). So one nice beautiful sunny
day you will discover that your "100 Java Tips" become "100's Java Tips"!
Please feel free to present it to your friends, colleagues and just good
people!
You can read about book and download it from page:
http://javafaq.nu/java/advert/our_book.shtml
Also we started some job
on building in into our site the searching engine that will let you do
additional search of Java resources (really any resources) on whole web
without leaving our site. This engine has more than 2,000,000 best active
links in the continuolsly growing database! All links are selected by
people (not by automated web spider program)!
Tip 1
Is
there any performance or other benefit from importing only the
classes you need in a file using something like:
import
java.util.HashMap;
instead of using ,
import java.util.*;
to import all the classes in a package.
Answer: Strictly speaking,
"import java.util.*;" does not import the whole of java.util. It is an
"import on demand" which imports any class or interface in java.util that is
needed. If the first import statement would have done the job, then HashMap
is the only class the second one would import.
No measurable
differences in compile time performance. You can do
the test, but I suspect you would get identical byte code, so no
difference in run time performance.
There is a practical difference
when two packages contain classes with the same name. Suppose you also
imported java.awt.* and tried to declare a List. It would be ambiguous. If
you use the second form you would either import java.util.List or
java.awt.List.
There is also a documentation difference. The first one
makes it clear exactly what imported classes are being used.
******************************************************************
Tip 2
Q: Is it possible to redirect the System.out.println to a file?
Answer:
Connect a PrintStream to the file, and then call
System.setOut (PrintStream out) that reassigns the "standard" output
stream.
******************************************************************
Tip 3
Q: == and equals ()... These two still make me confuse a lot of
time. Can somebody give me some thumb rule or explain it to me?
Answer: When you use == with a primitive -int, double, char, ...
you are checking that the values are identical. But if you use == with
an object, you are checking that the 2 objects are stored at the same
address. In other words the references pointing to the same object...
Method equals () is different. It is the same as ==, if it isn't overriden by
the object class. Many classes override the method equals (). In this case
this method will check that content of the object is the same or not,
not addresses.
******************************************************************
Tip 4
Q: Could some kind person please tell me how to save the object as a file so
as the same program can load it again?
Answer: try this program. It saves
obect into file:
import java.io.File; import java.io.FileOutputStream;
import java.io.ObjectOutputStream; import java.io.IOException;
public
class Save{ public void saveMyObject(String filename, Object obj) { File
myFile = new File(filename); try { FileOutputStream fileOutStr = new
FileOutputStream(myFile); ObjectOutputStream outStr = new
ObjectOutputStream(fileOutStr); outStr.writeObject(obj); outStr.close();
}catch (IOException e){ System.out.println("?!!!!!!"); } } public
static void main (String args[]) { Save s = new Save(); Object myObject
= new Object(); String test = "test"; myObject = (Object)test;
s.saveMyObject("myfile", myObject); } }
If you open myfile you
will see that this object includes our string "test" In the same manner
you can read this object from a file...
******************************************************************
Tip 5
Q: Why do I get message like “wrong magic number� when I am trying
to run an applet? What is a magic number?
Answer: The first thing a
JVM does when it loads a class is check that the first four bytes are (in
hex) CA FE BA BE. This is the "magic number" and thats why you are getting
that error, you are trying to load a file that isnt a class and so the class
loader in the JVM is throwing out that exception.
Make sure you
transfer the class files to site in binary mode, rather than text or ASCII
mode. An error from the browser saying "cannot start applet ... bad magic
number" usually means that one of the class files on the server is
corrupted. Replace your class binary files on the web server; clean up the
cache of your browser, and reload your applet.
******************************************************************
Tip 6
Q: Can anyone write me a short method that lets me know what files
are in a particular directory? For example, I want to know that
directory, d:/temp/aaa, has files a.txt, b.java, b.class. Also related to
this, how do I find out what folders I have?
Thanks in advance.
Answer: use our program as a base and add checking for the files and
directories you need to find!
here it is:
import java.io.File;
public class Save{ public void showDirectoryList() { File dir = new
File("d:/temp/aaa"); File[] list = dir.listFiles(); for (int i=0; i
if (list[i].isFile()) { System.out.println("File "+list[i].getName()); }
else if (list[i].isDirectory()) { System.out.println("Directory
"+list[i].getName()); } } } public static void main (String args[])
{ Save s = new Save(); s.showDirectoryList(); } }
******************************************************************
Tip 7
Q: In java, I found a lot of methods, which let you enter a line
(read (), readLine () e.c.t). They all wait until return is pressed, and
then start providing you the information. Does anyone know if there is a read
method available whith the desired behaviour, i.e. which doesn't wait for
return being pressed?
Answer: Java does not provide it, the terminal
itself waits until return is pressed before sending the entered line to
Java. You need to use some platform specific mechanism to change the
terminal settings.
The Java FAQ Daily Tips is a newsletter that is
only sent to those who have specifically subscribed to it.
John
Andersson, Editor mailto:info@javafaq.nu
Copyright (c) 2000 John
Andersson ******************************************************************
* * * You can find our tips on site also! * * Please recommend us and our
FREE "100 Java Tips" book to your * * friends and colleagues! * *
http://javafaq.nu/java/advert/our_book.shtml * * *
******************************************************************
Printer Friendly Page
Send to a Friend
..
Search here again if you need more info!
|