Site hosted by Angelfire.com: Build your free website today!


Java programming FAQ for beginners

How do I generate a random number?
How can I get system information like screen size, RAM size, color depth, browser specifics etc using Java?
How do I read info into a java application from a text file using command line arguments?
How do I access environment variables?
What are the differences between the == operator and the equals() method?
How can I force garbage collection to take place?
When should I use an interface instead of an abstract class?



How do I generate a random number?
There are two ways to do this. You can use the Math.random() method which returns a random real number greater than or equal to 0.0 and less than 1.0. You can also use the Random class in java.util package.
Back to top

How can I get system information like screen size, RAM size, color depth, browser specifics etc using Java?
The core Java API unfortunately does not have methods that can allow you to access such information. But there is a way without using native language programming. Try JConfig. It is a cross-platform library which supplements the core Java API. It is available for download here.
Back to top

How do I read info into a java application from a text file using command line arguments?
Pass the file name as a command line argument. Use the BufferedReader class to read the text file in your program:
FileInputStream istream = new FileInputStream(args[0]);
BufferedReader reader = new BufferedReader(istream);
String line;
while((line = reader.readLine()) != null)
{
   // whatever you want to do with the text
}
Back to top

How do I access environment variables?
You can't! Plus, not all platforms have environments to get variables from. Instead of using the environment to get something, consider using either resource files or System properties. Don't even consider using the getenv() method of the System class. It was deprecated with the Java 1.0 release. Not only is it deprecated, but it doesn't get anything.
Back to top

What are the differences between the == operator and the equals() method?
Well, first off, == is a fundamental operator in the language. The result type of the expression is a boolean. For comparing boolean types, it compares the operands for the same truth value. For comparing reference types, it compares the operands for the same reference value (i.e., refer to the same object or are both null). For numeric types, it compares the operands for the same integer value or equivalent floating point values.

In contrast, equals() is an instance method which is fundamentally defined by the java.lang.Object class. This method, by convention, indicates whether the receiver object is "equal to" the passed in object. The base implementation of this method in the Object class checks for reference equality. Other classes, including those you write, may override this method to perform more specialized equivalence testing.

The typical mistake that most people do is in using == to compare two strings when they really should be using the String class's equals() method. From above, you know that the operator will only return "true" when both of the references refer to the same actual object. But, with strings, most uses want to know whether or not the value of the two strings are the same -- since two different String objects may both have the same (or different) values.
Back to top


How can I force garbage collection to take place?
You can use the System.gc() method to indicate to the JVM that it can run the garbage collector.
Back to top

When should I use an interface instead of an abstract class?

Back to top