Put your text ad here
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
25% off cpanel web hosting and reseller hosting deals. Promo: codestyle25off
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs and more.
Get the latest answers in this FAQ
Follow the Code Style Twitter feed for free one-to-one help sessions by instant messenger: MSN, AIM and Yahoo! Messenger. Sessions are held most Saturdays. Join the Twitter feed then check your direct messages for details.
hashCode() used?
NullPointerException in Java?
System.out?
Rectangle on a Graphics instance?
new operator?
Class.forName() method and the new operator?
Class.forName(String) method?
Class.forName() scheme?
newInstance()?
Class.forName() runtime polymorphism?
Class.forName(dbDriver) work with DriverManager.getConnection?
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.
More details available to premium content service subscribers:
When is hashCode() used?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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.
More details available to premium content service subscribers:
What is a NullPointerException in Java?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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.
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.
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.
More details available to premium content service subscribers:
How can I create a constructor to equate two other objects?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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.
More details available to premium content service subscribers:
How do I draw a Rectangle on a Graphics instance?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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.
More details available to premium content service subscribers:
What is the difference between the JVM, JRE and JDK?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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.
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.
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.
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.
More details available to premium content service subscribers:
What is the difference between heavyweight and lightweight components?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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.
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.
More details available to premium content service subscribers:
How can I get a class reference without using new operator?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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.
More details available to premium content service subscribers:
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.
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.
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.
More details available to premium content service subscribers:
How can I implement the Class.forName() scheme?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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...
More details available to premium content service subscribers:
How can I cast an unknown sub-class returned by newInstance()?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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.
More details available to premium content service subscribers:
Is Class.forName() runtime polymorphism?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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.
More details available to premium content service subscribers:
How does Class.forName(dbDriver) work with DriverManager.getConnection?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
About us: site help, text ads, sponsored links and premium content FAQs.