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 139


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

Java Newsletters: Java Newsletters Archive: 5

Go to all tips in Java Newsletters

Hello dear subsribers!

We continue to send you our daily Java tips!

Today you receive our tips that will appear
on our site in October, 11 and following week.
This week they are really big and we can say more
than small tips!
Mini articles Smile .

This is not sent unsolicited. You are receiving this
newsletter because you signed up for it, or a friend has
forwarded it for you.
******************************************************
Please, if you like our tips, recommend us to your friends and colleagues.
******************************************************
Tip1

Question: Are not-initializable classes and methods like
System.out.println() and Math.random() "synchronized" ?
Answer: I think they are synchronized. Simple observation: did you ever
see once that printout was broken into two pices by another printout?
I mean for example if you do smth. like this:

In 1st thread:
System.out.println("1234567890");
And in 2nd thread:
System.out.println("something else here");

it never will be broken like:

12345
something else here
67890

Even if Sun didn't write about it explicitly, we can see that it is
synchronized or at least behaves like synchronized that is the same
for our real life.
......................................................................………………………..
Tip 2
How can I set a JFrame to be full screen at the start of a program? I want
no borders, no titles and I just want to use the entire screen.
A: Try using JWindow instead, that one can be customized to have no
borders or titles... as for size, I think you can use:
setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment().

getDefaultScreenDevice().getDefaultConfiguration().getBounds());
to fill out the entire screen.
......................................................................………………………..
Tip 3
Whats the difference between the two: System.err. and System.out?
When should we use System.err?
Answer 1: System.out leads the output to the standard output stream
(normally mapped to your console screen), System.err leads the
output to the standard error stream (by default the console, too).
The standard output should be used for regular program output,
the standard error for errormessages. If you start your console
program regularly both message types will appear on your screen.

But you may redirect both streams to different destinations (e.g. files),
e.g. if you want to create an error log file where you don't want to be
the regualr output in.

On an UNIX you may redirect the output as follows:

java yourprog.class >output.log 2>error.log

this causes your regular output (using System.out) to be stored in
output.log and your error messages (using System.err) to be stored
in error.log

Aanswer 2: System.err is a "special" pipe that usually is directed
to the standard consolle. You can redirect the System.out with the
normal pipe control (| or >), but System.err no. If you want to put
both the "normal" output and the "error" output to a file you must
use the special redirect 2>.

This allow you to send normal messages into a file or in the /null
black hole, but still receive the error messages on the console.
......................................................................………………………..

Tip 4
Why cannot I mix AWT and Swing?
Q: Recently, I have been hearing a lot of people from various
newsgroups and website saying, java swing and awt can't be in the
same application. They will not work together and they might
produce unexpected results. At the same time, i don't hear people
saying "why" you shouldn't use swing and awt together. Could
someone out there shed some light for me.
Is their any logical reason why we shouldn't mix Swing and AWT
in the same application/applet? If there is a problem mixing Swing
and AWT... what are the results, what can happen? I design using
IBM's Visual Age for Java 3.0, and I mix swing and awt in the
same application/applet, it works fine when testing in the IDE
(I haven't tested it outside of the IDE yet).
If you have tested application/applets outside of the IDE, please
let me know what happened?

A: There are fundamental incompatibilities in the way they
draw themselves. AWT java classes are not "pure" Java classes,
they use underlaying C/C++ native code (dependable on operation
system) that can cause different appearence in different OSs.
Swing is pure Java implementation and has no native code at all.
Swing applications look the same.

> If there is problems mixing swing and awt... what are the results,
> what can happen?

Some objects drawn on top of others are not properly occluded.
This is most obvious with drop down menus, which have a tendency
to stay visible even after you have selected a menu item. Another
problem is that if you use AWT components on a JtabbedPane
they will not disappear when you switch tabs. There are many
similar issues.
......................................................................………………………..
Tip 5

I will be thankful if anyone tells me why JVM is called virtual
machine.

A: JVM is called a virtual machine because there is no real
hardware, which interprets the byte code. If you have done
any assembly programming for any microprocessor/microcontroller
you will able to understand this. A microprocessor has builtin
instruction set to interpret the assemly code. Similarly the JVM
is similar to a microprocessor in the sense it has its own instruction
set but it implemented in software.
That is why it is called a virtual machine!

......................................................................………………………..
Tip 6

I am totally confused about the differences between the SDK, JDK
and IDE products…
Q: I am brand new to the world of Java and am most interested in learning
the language. However, I am confused about the differences between the
SDK, JDK and IDE products. I have gone to the Sun site and even some of
the IDE vendor sites and I have yet to find something that tells me
what the differences are between the three and which of the three
I need to program in Java.

I want to program using some form of IDE but do I need to separately
download and install a SDK and/or JDK? Sun needs to improve their
documentation for us new to the Java environment.

A1: IDE is an acronym for _I_ntegrated _D_evelopment _E_nvironment.
These products are the one-stop shops for coding, running and debugging
your code. Often these will include GUI based drag and drop form
designers and "wizards" for the shells of common forms of code
(Application, Applet, etc.) JBuilder is an IDE.

The IDE may stand on its own, or it may act as a front end for a JDK.

JDK is _J_ava _D_esign _K_it. A JDK is a command line based interface
to the JVM, plus the classes. You are responsible for your own editors,
creating code for GUI elements, and all code. All of the IDE's I have
reviewed personally come with JDK or their own vendor's equivalent
(JVM and class libraries). Some IDE's are capable of a sort of
"upgrading" by downloading the latest JDK from Sun (JBuilder for example).

A2: If you want to write a Java program using any editor and not an
IDE then you would want to download the JDK. It will let you compile
and run Java programs from the command line (like a DOS window).
JDK stands for Java Development Kit and SDK stands for Standard
Development Kit.

Java comes in three versions - Standard, Enterprise, and Micro editions.
JDK could be any one of the three. SDK is the standard one - this is the
one most people use. If you want an IDE they typically come with a
JDK so all you would need to do there is download the IDE and start using it. ......................................................................………………………..
Tip 7

How can I pass a string to the command line (DOS)? Also i want to
capture the output given by the command line in a string.

A1: Try this out:

// works for DOS
String cmds[] = new String[2];
cmds[0] = "dir"; // replace with "ls" on UNIX
cmds[1] = "c:"; // replace with "/" on UNIX

// execute the command
Process pro = Runtime.getRuntime().exec(cmds);
// wait until it's done executing
pro.waitFor();

// what did the process output from the Input pipe back to
// this process (okay, who named this stuff)?
InputStream out = pro.getInputStream();

// output it (really slowly)
int i;

while ((i = out.read()) != -1) System.out.println((char) i);


With best regards Java FAQ team, http://javafaq.nu/java
Free Daily Java Tips, free books, code examples and hand-selected links!
You can find our tips on site later also!
Recommend us to your friends!



9 comments | Printer Friendly Page  Send to a Friend | Java Newsletters | Score: 0
Posted by jalex on Wednesday, January 28, 2004 (16:00:09) (2519 reads)

Java Newsletters: Java Newsletters Archive: 4

Go to all tips in Java Newsletters

Hello dear subsribers!

We continue to send you our daily Java tips!
********************************************
Please, if you like our tips, recommend us to your friends
and colleagues.
***********************************************
Tip1

Hi there, does anybody know a good source of design patterns
written in JAVA ?

A: A pretty good (free to download) book.

http://www.patterndepot.com/put/8/JavaPatterns.htm

......................................................................………………….

Tip 2
How to set the default memory limit of java virtual machine
in a java application?
Answer1: java -Xms16m -Xmx32m MainClassName
here: -Xms16m => 16meg initial memory allocation
-Xmx32m => 32meg max memory allocation

Answer2: Run your Java program with -mx switch
like this:

java -mx128m ClassName

This for example will set maximum memory allocation pool
to 128MB.
......................................................................………………….
Tip 3

Q: Currently I'm running two operating systems on one machine,
Windows 2000 and Windows ME.
Windows ME runs on default, Windows 2000 is on D drive and
ME is on C drive. I tried to add the JDK directory to the
classpath but there isn't a autoexec.bat on the D directory, but
there is one for C. Should I just create a autoexec.bat for D?

Answer: In Windows2000/ME you should do it on Control Panel.
So, go to Settings/Control Panel/System/Advanced/Environment
Variables... and edit your CLASSPATH variable if exists
or add a new one.
......................................................................………………….
Tip 4
Q: I need to be able to run a shell script from java, by doing a
host call to UNIX. I am currently trying 'Runtime.exec' to do this.
It says it runs it, but doesn't actually successfully complete the
command (which is a file delete).
A few questions I have about this:

1. Can I trace it or something to see why it isn't working?
2. How can I get the 'return code' from the shell script?
3. Will the java procedure wait for the shell script to execute,
or does it run a seperate thread concurrently?

Answer: > 1. Can I trace it or something to see why it isn't
working?

Runtime.exec() returns a Process object that you can
get information from.

If the script is written with sh or some derivative there of you
can do 'set -x' in the script to trace its behaviour. For other
scripting languages there is likely a similar mechanism. To
see the trace in your Java program, read from the error or
output streams of the process:
Process.getErrorStream() or Process.getOutputStream();
It's possible that the script is already printing a message to
its error stream that you aren't seeing, indicating why it
isn't working.

A:> 2. How can I get the 'return code' from the shell script?

Call Process.exitValue().

A:> 3. Will the java procedure wait for the shell script to execute,
or does it run a seperate thread concurrently?

The program runs concurrently in a separate *process*, not
a thread within the JVM. To wait for the process to finish, call

Process.waitFor().
......................................................................………………….
Tip 5

Do the classes in java.util.zip handle password-encrypted
zip files? I've looked through the API, and I don't see
any mention of it.

Answer: No, they don't. But Zip's built-in encryption isn't
safe anyway by today's standards. Every hacker can find
within a day (computer time)

......................................................................………………….
Tip 6

How do I make java apllication instalable?
Q: I have written a Java application and have the .class files
in one location. How do I make it installable? Is it possible
to convert it to an executable file?

Answer1: just create a batchfile or a .sc file (if on unix)
Answer2: Or check http://installshield.com/
They have special edition for Java which lets to write
additional interface for configuring of program parameters
during instalation.
And check a couple of addresses on our page here:
http://javafaq.nu/java/tools/tools.shtml
......................................................................………………….
Tip 7

An idea for validating the phone number field on a form…
Q: Does anyone have an idea for validating the phone number
field on a form. I am looking for something that will basically
check this input mask

*111*111*1111*

Where the 1's are number's and the *'s are either - . spaces,
or any other character like (). Please advise.

Answer1: You could use a regular expression package. For
example, Jakarta ORO:
http://jakarta.apache.org/oro/

Answer2: I'm thinking regular expressions. See:
http://www.cacas.org/java/gnu/regexp/

http://www.crocodile.org/~sts/Rex/



5 comments | Printer Friendly Page  Send to a Friend | Java Newsletters | Score: 0
Posted by jalex on Wednesday, January 28, 2004 (15:58:06) (2468 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