Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Easy Learn Java: Programming Articles, Examples and Tips - Page 143


Previous 1060 Stories (530 Pages, 2 Per Page) Next

Java Newsletters: Java Newsletters Archive: 14

Go to all tips in Java Newsletters

******************************************************************
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* *
* Issue No: 14 6 December 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 it possible for a minimized window in MS Windows task bar
to start blinking?
2. How do I indicate Unicode characters that cannot be represented
in ASCII, such as ц?
3. Can I pass a function as a parameter in Java? If so, how?
4. How do I find the list of all system properties?
5. ... Finally I found that Java API has a bug!
6. I need to store an object NOT a string in a cookie.
Is that possible?
7.
******************************************************************

Hello dear friends!

We published pdf-version (Acrobat Reader) of our "100 Java Tips"
http://javafaq.nu/java/advert/our_book.shtml Check here!
So it is possible for all of you to read this book now!

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!

Today we built in into our site the searching engine that will let
you do additional search of Java resources (really any web resources!)
on whole web without leaving our site.
This engine has more than 2,000,000 best active links in the
continuously growing database with description!
All links are selected by people (not by automated web spider program)!
Please take a look! And report us if smth. doesn't work properly.

We receiving many letters with questions. We are trying answer you,
but unfortunately we have no enough time to answer all people.
Often it takes too much time to investigate the problem.
Please ask your questions and help each other at our Java Forum:
http://javafaq.nu/cgi-bin/UltraBoard/UltraBoard.cgi?action=Headlines&BID=1

******************************************************************

Tip 1

Q: Is it possible for a minimized window in MS Windows task bar
to start blinking? In any IRC client a minimized window starts
blinking when a new message appears in it. Is it possible in Java?

Answer:
I doubt very much that it is possible with pure java; you would
have to use some native code to achieve that. Maybe what you could
try though, is to play around with the icon used in the title bar
of the frame. I haven't tried this myself - and in any case I'm
running linux, but it might be possible to have two icons and to
switch them around at some rate using the Frame.setIconImage(Image)
method. This will only affect the icon itself - not the whole
window, but it's better than nothing.

******************************************************************

Tip 2

Q: How do I indicate Unicode characters that cannot be represented
in ASCII, such as ц?

Answer: from "Java Tutorial
(http://java.sun.com/docs/books/tutorial/i18n/text/convertintro.html)
"To indicate Unicode characters that cannot be represented in ASCII,
such as o, we used the uXXXX escape sequence. Each X in the escape
sequence is a hexadecimal digit. The following example shows how to
indicate the o character with an escape sequence:

String str = "u00F6";
char c = 'u00F6';
Character letter = new Character ('u00F6'); "

******************************************************************

Tip 3

Q: Can I pass a function as a parameter in Java? If so, how?

A: No, you cannot. But you can pass an object with method and then
just use this method like this:

myFunction (object); // object has method useMyMethod(){ do smth here...}
..
..
somewhere in another place where use this function..
object.useMyMethod();

******************************************************************

Tip 4
Q: How do I find the list of all system properties?

Answer: do smth like this:

Enumeration list = System.getProperties().propertyNames();
while(list.hasMoreElements()){
System.out.println((String) list.nextElement());
}
}

******************************************************************

Tip 5

When I tried to read one string representing boolean value and
convert it into boolean it didn't work. Finally I found that Java
API has a bag!
Q: I wrote the program that uses redaing ini file settings for
initialization. All settings in a file are strings. I am converting
them to appropriate type during reading. When I tried to read one
string representing boolean value and convert it into boolean it
didn't work. Finally I found that Java API has a bag:

boolean x = true;
getBoolean(x);
will show false!!!!

Why Java has method that doesn't work? Is it bug in Java
or I am stupid?

A: neither statement is true! It is not a bug and you are Ok! Just
please read more carefully JavaDoc next time.

It is written there for getBoolean ():
"Returns is true if and only if the system property named by the
argument exists and is equal to the string "true". (Beginning with
Java 1.0.2, the test of this string is case insensitive.)
A system property is accessible through getProperty, a method
defined by the System class."

So you didn't use this method properly...
Use instead:

public static Boolean valueOf(String s)

This method returns the boolean value represented by the specified
String. A new Boolean object is constructed. This Boolean contains
the value true if the string argument is not null and is equal,
ignoring case, to the string "true".

example:
boolean x= true;
(Boolean.valueOf(x)).booleanValue()
gives you proper boolean (not Boolean!) value


******************************************************************

Tip 6

Hi, I am using servlets. I need to store an object NOT a string
in a cookie. Is that possible? The helpfile says BASE64 encoding
is suggested for use with binary values. How can I do that???

Answer: You could serialize the object into a ByteArrayOutputStream
and then Base64 encode the resulting byte []. Keep in mind the size
limitations of a cookie and the overhead of transporting it back
and forth between the browser and the server.
Limitations are:
* at most 300 cookies

* at most 4096 bytes per cookie (as measured by the characters
that comprise the cookie non-terminal in the syntax
description of the Set-Cookie2 header, and as received
in the Set-Cookie2 header)

* at most 20 cookies per unique host or domain name
For more details please refer to RFC 2965.

******************************************************************

Tip 7

Q: How can I read a Tab delimited text file into a JList?
All I need is the first item in each record. I import each line
with a BufferedReader.Readline() and then look at each char with
record.substring(n,n+1). How can I check if the next char is a Tab?

Answer: StringTokenizer st = new StringTokenizer(record, " ");
while (st.hasMoreTokens()) {
String token = st.nextToken();
// ... do something with the token
}



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 *
* *
******************************************************************



5 comments | Printer Friendly Page  Send to a Friend | Java Newsletters | Score: 0
Posted by jalex on Wednesday, January 28, 2004 (16:26:32) (2621 reads)

Java Newsletters: Java Newsletters Archive: 12

Go to all tips in Java Newsletters

******************************************************************
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* *
* Issue No: 12 22 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. How can Java program can catch when someone
sends a "kill" to my app in Unix or does a Ctrl-C in windows?
2. Does it possible to have two thread running at the same time
which reads from the same socket...
3. How do I launch a native Document by its Associated MIME Type?
4. Why cannot I cast from double to java.lang.Object?
5. Can anyone write me a short method that lets me know what files
are in a particular directory?
6. I'm researching methods by which one JVM can interact with
another JVM, which is running on the same machine...
7. I want to execute a command whose argument (a filename)
has spaces in it.
******************************************************************

Hello dear friends!

We keep our promise and today we glad to announce that our first
version of "100 Java Tips" E-Book is ready. From the beginning
only Windows users can read this book, but we are going to
prepare pdf-version (Acrobat Reader) soon also. You can read
about book and download from page:
http://javafaq.nu/java/advert/our_book.shtml

We are planning 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!

******************************************************************

Tip 1

I would like to know how my Java program can catch when someone
sends a "kill" to my app in Unix or does a Ctrl-C in windows?

In Unix there is atexit() function that handles this type of
situation. Is this possible in Java ?

Answer: Starting with 1.3 there is Runtime.addShutdownHook().
This is for cleanup only.
from API: "A shutdown hook is simply an initialized but unstarted
thread. When the virtual machine begins its shutdown sequence it
will start all registered shutdown hooks in some unspecified order
and let them run concurrently. When all the hooks have finished it
will then run all uninvoked finalizers if finalization-on-exit has
been enabled.
Finally, the virtual machine will halt. Note that daemon threads
will continue to run during the shutdown sequence, as will
non-daemon threads if shutdown was initiated by invoking the
exit method. Once the shutdown sequence has begun it can be
stopped only by invoking the halt method, which forcibly
terminates the virtual machine.
Once the shutdown sequence has begun it is impossible to register
a new shutdown hook or de-register a previously-registered hook.
Attempting either of these operations will cause an
IllegalStateException to be thrown.
Shutdown hooks should also finish their work quickly. When a
program invokes exit the expectation is that the virtual machine
will promptly shut down and exit. When the virtual machine is
terminated due to user logoff or system shutdown the underlying
operating system may only allow a fixed amount of time in which
to shut down and exit. It is therefore inadvisable to attempt
any user interaction or to perform a long-running computation in
a shutdown hook."
more read here: http://java.sun.com/j2se/1.3/docs/api/index.html

******************************************************************

Tip 2

Does it possible to have two thread running at the same time
which reads from the same socket. If a message is received, does
both threads then receive it?

Answer: Two threads can read data from the same socket input
stream, but they WON'T each get copies of the same data. They'll
each get separate parts of the message.

******************************************************************

Tip 3

How do I launch a native Document by its Associated MIME Type?
For example, I would like to ask the 'operating system' what
application is associated with .DOC and then launch it.

Answer: On WinNt,

String docName = "c:someyourdirnameofdoc.doc";
Runtime.getRuntime().exec("cmd.exe /c " + docName);

Tip 4

Why cannot I cast from double to java.lang.Object?

Q: I'm trying to build a vector, however, one of the objects that
I'm passing to the vector is of type double. How do I cast the
double as an object so that I may insert the value into a vector?
Does this make sense? Here is the following snippet of code I was
trying to use:

myVector.add (1, (Object)myDouble);

Of course when I try to compile I get the following message:

Invalid cast from double to java.lang.Object

Could someone please explain why? I realize that Object is the
mother of all objects and therefore ANY reference data type "is an"
Object. So therefore I shouldn't have to cast the double, right?

Answer: A double is not a reference type, but a primitive one.
Hence, it doesn't inherit from Object (or anything else, for that
matter). To put primitives (byte, short, int, long, float, double,
boolean, char) into something that requires an Object, use
Java's wrapper classes.

The wrapper classes are Double, Integer, Long, Boolean, etc., and
are basically an object "wrapped" around a primitive type. You
make a Double object by:
Double d = new Double (myDouble);

and to get the actual value back,

double z = d.doubleValue();

It works the same way for all the rest of the primitive/wrapper
pairs.

******************************************************************

Tip 5

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, c:mystuff, has files
hello.txt, world.txt, and readme.txt.

Answer: Create a File with the name of the directory whose contents
you are interested in. listFiles () will then give you an array of
File objects, representing both files and directories in that
directory:

File dir = new File ("c:");
File [] contents = dir.listFiles ();

Contents now contain a list of files and directories in c:.
To see which of these are files and which are directories, use
isFile () or isDirectory () on each of them:

for (int i=0; i if (contents[i].isFile()) {
System.out.println (contents [i]. getName ()
+ " is a file");
} else if (contents[i].isDirectory()) {
System.out.println (contents [i]. getName ()
+ " is a directory");
}
}


******************************************************************

Tip 6

Q: I'm researching methods by which one JVM can interact with
another JVM, which is running on the same machine.
I know that there are various network models, which can be applied
if a JVM needs to talk to another one across a network, but in
addition to these (which could I guess be applied to JVMs on the
same machine) I wondered if you knew of a system of JVM
communication that requires less system resources, where the JVMs
are both running on the same system.

Answer: CORBA, RMI, HTTP, sockets....
But if you have no TCP/IP stack on your platform, so for Windows
it could be clipboard...

******************************************************************

Tip 7

Q: I've run into a somewhat exotic problem. I want to execute a
command whose argument (a filename) has spaces in it. Unix allows
it [windows even encourages it, what with Program Files etc] e.g.

cat 'silly file.txt'

works in both sh and csh. However, those quotes (or a before the
space) do not work with Runtime.exec (), because it uses
StringTokenizer with default-delim constructor to parse the command
string. The dflt delims include space, so 'silly file.txt' spawns
two tokens.

Answer: Use the "other" Runtime.exec, i.e. the one that takes an
array of Strings. Pass it your command line, pre-tokenized:

String [] cmd = {"cat", "silly file.txt"};
Runtime.getRuntime (). Exec(cmd);


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 *
* *
******************************************************************



18 comments | Printer Friendly Page  Send to a Friend | Java Newsletters | Score: 0
Posted by jalex on Wednesday, January 28, 2004 (16:21:11) (2740 reads)

Previous 1060 Stories (530 Pages, 2 Per Page) Next

530| 529| 528| 527| 526| 525| 524| 523| 522| 521| 520| 519| 518| 517| 516| 515| 514| 513| 512| 511| 510| 509| 508| 507| 506| 505| 504| 503| 502| 501| 500| 499| 498| 497| 496| 495| 494| 493| 492| 491| 490| 489| 488| 487| 486| 485| 484| 483| 482| 481| 480| 479| 478| 477| 476| 475| 474| 473| 472| 471| 470| 469| 468| 467| 466| 465| 464| 463| 462| 461| 460| 459| 458| 457| 456| 455| 454| 453| 452| 451| 450| 449| 448| 447| 446| 445| 444| 443| 442| 441| 440| 439| 438| 437| 436| 435| 434| 433| 432| 431| 430| 429| 428| 427| 426| 425| 424| 423| 422| 421| 420| 419| 418| 417| 416| 415| 414| 413| 412| 411| 410| 409| 408| 407| 406| 405| 404| 403| 402| 401| 400| 399| 398| 397| 396| 395| 394| 393| 392| 391| 390| 389| 388| 387| 386| 385| 384| 383| 382| 381| 380| 379| 378| 377| 376| 375| 374| 373| 372| 371| 370| 369| 368| 367| 366| 365| 364| 363| 362| 361| 360| 359| 358| 357| 356| 355| 354| 353| 352| 351| 350| 349| 348| 347| 346| 345| 344| 343| 342| 341| 340| 339| 338| 337| 336| 335| 334| 333| 332| 331| 330| 329| 328| 327| 326| 325| 324| 323| 322| 321| 320| 319| 318| 317| 316| 315| 314| 313| 312| 311| 310| 309| 308| 307| 306| 305| 304| 303| 302| 301| 300| 299| 298| 297| 296| 295| 294| 293| 292| 291| 290| 289| 288| 287| 286| 285| 284| 283| 282| 281| 280| 279| 278| 277| 276| 275| 274| 273| 272| 271| 270| 269| 268| 267| 266| 265| 264| 263| 262| 261| 260| 259| 258| 257| 256| 255| 254| 253| 252| 251| 250| 249| 248| 247| 246| 245| 244| 243| 242| 241| 240| 239| 238| 237| 236| 235| 234| 233| 232| 231| 230| 229| 228| 227| 226| 225| 224| 223| 222| 221| 220| 219| 218| 217| 216| 215| 214| 213| 212| 211| 210| 209| 208| 207| 206| 205| 204| 203| 202| 201| 200| 199| 198| 197| 196| 195| 194| 193| 192| 191| 190| 189| 188| 187| 186| 185| 184| 183| 182| 181| 180| 179| 178| 177| 176| 175| 174| 173| 172| 171| 170| 169| 168| 167| 166| 165| 164| 163| 162| 161| 160| 159| 158| 157| 156| 155| 154| 153| 152| 151| 150| 149| 148| 147| 146| 145| 144|
143
| 142| 141| 140| 139| 138| 137| 136| 135| 134| 133| 132| 131| 130| 129| 128| 127| 126| 125| 124| 123| 122| 121| 120| 119| 118| 117| 116| 115| 114| 113| 112| 111| 110| 109| 108| 107| 106| 105| 104| 103| 102| 101| 100| 99| 98| 97| 96| 95| 94| 93| 92| 91| 90| 89| 88| 87| 86| 85| 84| 83| 82| 81| 80| 79| 78| 77| 76| 75| 74| 73| 72| 71| 70| 69| 68| 67| 66| 65| 64| 63| 62| 61| 60| 59| 58| 57| 56| 55| 54| 53| 52| 51| 50| 49| 48| 47| 46| 45| 44| 43| 42| 41| 40| 39| 38| 37| 36| 35| 34| 33| 32| 31| 30| 29| 28| 27| 26| 25| 24| 23| 22| 21| 20| 19| 18| 17| 16| 15| 14| 13| 12| 11| 10| 9| 8| 7| 6| 5| 4| 3| 2| 1|


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy