CSS font stacks, developer FAQs & web standards

Your banner ad here

WestNIC provides reliable web hosting services

Top Canadian Hotels no booking fees from Victoria BC to Nova Scotia

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.

The Java API

Q: When is hashCode() used?

A: The hashCode() method is used in hash-based data stores to get a "nearly unique" identifier for each object. The hash code is used to speed up the search process, so should have a high probability of being different from any other instance. It is possible for two hash codes to be the same, so the equals method is used to make an exact match.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
When is hashCode() used?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What is a NullPointerException in Java?

A: A NullPointerException is a runtime exception that indicates the Java Virtual Machine attempted to call a method on an object, but the object reference was null. This case may occur when a reference is assigned from another method call that may return a null value, such as a Hashtable look-up.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
What is a NullPointerException in Java?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What is System.out?

A: System.out is a static variable that refers to the console output. The object type of System.out is PrintStream, not PrintWriter, but it has similar overloaded println methods. These print methods can be called by any class using the static System.out reference.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Are the Java console streams subclasses of System?

A: System.out is a static variable of the System class that holds a reference to a PrintStream used to channel output to the host system's console. System.err is also a PrintStream and System.in is an InputStream. When the Java runtime system starts up it automatically hooks-up these object references to the host system's output, error and input streams.

The PrintStream and InputStream types used to interface with the host system are not subclasses of the System class, they are a subclasses of the java.io.OutputStream and InputStream hierarchy. Notably the PrintStream class never throws IOException, though you can use the checkError() method to discover any problems.

The common technique of making print calls with System.out.println() gives the impression that println() may be a method of the System class but it is not, it is a method of the PrintStream class.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Where can I find out more about the Java API?

A: You should download the Java SDK API documentation or read online. The standard Java documentation gives full details of all public methods and usage guidelines for all classes and is an excellent place to start, and for quick reference.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I create a constructor to equate two other objects?

A: Normally when we compare two objects we use the fundamental equals(Object) method. Conceptually, the equals method "belongs" to the objects you are comparing and returns a boolean to indicate whether they are passed a reference to themselves.

Its not obvious why you would choose to make that comparison in the constructor of another class, which may only return a reference to the new instance, not a simple boolean response. In any case, the example below shows how you can compare method arguments and return a boolean, which could be refactored for use in a constructor.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I create a constructor to equate two other objects?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How do I draw a Rectangle on a Graphics instance?

A: When you have a method that takes an object as an argument, you must pass an object of the specified type in the method call. There is no Graphics method that accepts a Rectangle to draw, nor a Rectangle method that accepts a Graphics object to render to.

Since Graphics is an abstract class, you must obtain a concrete implementation. You can then use its drawRect(int x, int y, int width, int height) method to draw an outline of your rectangle, as below.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How do I draw a Rectangle on a Graphics instance?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Java jargon explained

Q: What is the difference between the JVM, JRE and JDK?

A: The JDK, also called the Java Software Development Kit (SDK), is the full suite of tools required to develop, package and publish Java applications. The Sun Java SDK includes the full Java class library, with a compiler, decompiler, profiler, JAR signer, key signing tool and many other tools.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
What is the difference between the JVM, JRE and JDK?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What's the difference between JVM and JIT?

A: The Java Virtual Machine (JVM) is a program that runs on a computer operating system and executes Java program code. The JVM takes the Java code and compiles it to a format that can be run directly on the computer's processor. The JVM controls the interface between the Java code and the computer like audio software enables us to play the same CD on a Windows, Mac or GNU Linux computer.

In early versions of the JVM, all the Java code for a program had to be loaded and compiled before the program could be run. More recent versions are optimised so that the Java code is compiled "Just In Time"; just before it is due to be executed on the processor. This approach accelerates program start-up, and overall performance of the Java program, but requires an extra level of coordination within the virtual machine.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What's the difference between an instance and an object?

A: When you create a Java object by calling its constructor, the object reference that is returned is called an instance. This means there is little difference between the two terms. The word instance is usually used when we talk about the process of object creation or instantiation, it is a single reference to an object. The word object can also be used to talk collectively about the general properties and behaviour of a Java object.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What is the difference betwen invoke and call?

A: There is no practical difference in the meaning of "invoke" versus "call" in Java programming, both mean that one class executes a method or constructor on itself or another class. In general terms the word invoke means to call upon an agent for help or guidance, or appeal for confirmation or corroboration, or summon an entity. Java programs can also be thought of as a sequence or network of messages that are sent between classes to trigger behaviour and get a response. The metaphor is basically the same in both cases.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What is the difference between heavyweight and lightweight components?

A: Java Graphical User Interface (GUI) components are called heavyweight when they rely on components from the underlying operating system to be displayed. This is because these native "peers" have a relatively heavy processing demand to render on screen and maintain their state representation. Thus applications composed of many heavyweight components can become slow to render on screen.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
What is the difference between heavyweight and lightweight components?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What are legacy classes?

A: The term legacy has a similar meaning to its everyday usage; something that is left behind from an earlier time or passed down from one's ancestors.

In Java, legacy classes are system components that we continue to work with, even though they may be deprecated, use deprecated methods or sub-optimal programming techniques, for example. Systems may use legacy classes from third party suppliers that are no longer maintained, for which no source code is available.

Ideally, one would replace such legacy classes with contemporary implementations, but sometimes the extent of a system's dependence on legacy classes makes them difficult or un-economical to replace. In this case, it is often necessary to use adapter classes that enable legacy classes to work with new programming interfaces.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Class loading

Q: How can I get a class reference without using new operator?

A: There are two questions here, first why would you want to get a class reference without using the new operator? Often this is the case when you need to load a class at runtime, but you do not know in advance what class it is. In this case, you might configure the class name in a properties file and load it using the static Class.forName(String) method, as below.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I get a class reference without using new operator?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What's the difference between the Class.forName() method and the new operator?

A: The Class.forName() method is different from invoking the new operator for a class because it does not return an instance of the class. The forName() method initialises a class and returns a Class object.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
What's the difference between the Class.forName() method and the new operator?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What is the return type for the Class.forName(String) method?

A: The return type for the static Class.forName(String) method is a Class object reference for the class named in the string argument. This is not the same as an instance of the class itself. You must use the newInstance() method on the class reference to obtain an instance of the named class. You can also create an instance indirectly by obtaining a Constructor reference from the class and call its newInstance(Object[]) method.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I implement the Class.forName() scheme?

A: To use this method of class loading, a class only requires a default constructor, with no arguments. Once the class is loaded, then use the Class object's newInstance method to get a new object reference for the class. You must also handle a number of exceptions that may be thrown if the class is not found, cannot be loaded, etc.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I implement the Class.forName() scheme?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I cast an unknown sub-class returned by newInstance()?

A: When you want to instantiate one of several possible sub-classes at runtime it is best to create an interface type that defines the common behaviour of those classes. For instance, you might define a Processor interface with one common method, like so...

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I cast an unknown sub-class returned by newInstance()?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Is Class.forName() runtime polymorphism?

A: The Class.forName() scheme could be used to exploit the polymorphic nature of a classes, but this approach would have to be designed into an application. This method of class loading is often used to enable runtime configuration of applications, through property files, servlet configurations and suchlike. However, this scheme has a "blind" reliance upon the type of the specified class.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
Is Class.forName() runtime polymorphism?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How does Class.forName(dbDriver) work with DriverManager.getConnection?

A: The static forName method is a way to instantiate a class that minimises hard coded dependencies in your Java applications. You may well know the database driver you intend to use when you first write your code, but if you use a String variable for your class name you can re-configure for a different database product without re-writing your client application.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How does Class.forName(dbDriver) work with DriverManager.getConnection?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Add this page to your chosen social bookmarking service

Style warning, please read

Home · CSS · Java · Javascript · HTML · Help · Log