Place your text ad here.
World class Hard Drive Recovery and renowned raid recovery services
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
This FAQ is part of the Code Style Help and FAQ section. Use the help request form below if your question is not answered here, but make sure you are asking the right question first.
println overloading or overriding?
== matter?
A: Java is an object oriented language, but there is no standard that defines a "fully" object oriented language, it is a matter of definition and opinion.
More details available to subscribers:
Is Java a fully object oriented language?
A: There are two main divisions of data types in Java: object and primitive types. Object types are declared according to their class.
Object object = new Object();
Objects can also be cast to a more general superclass or interface type. This is known as polymorphism, which means the object behaves like, and can be treated as, an instance of the more general type.
// Type cast
Object object = new StringBuffer("Example");
// Interface
Comparable object = new File("c:\Example.txt");
More details available to subscribers:
What are Java types are available?
A: The major release versions of the Sun Java Software Development Kit (SDK, also known as the JDK) include significant API changes that provide extra programming features built upon the core Java software platform. That means that the basic features of the Java language do not change from one release to the next, so most existing programs will run successfully when compiled with the new SDK.
Some core packages may gain additional features in new Java releases, but it is very rare for established API features to be removed, which would break backwards compatibility. Superseded or problematic API methods are usually marked deprecated before they are removed altogether, to give programmers the chance to upgrade their code to the new standard. Deprecated classes and methods can still be used to develop and run Java programs, but the compiler will issue warnings.
Sun publish a set of release notes for each SDK release with their archive of previous Java SDK releases.
A: All Java methods must declare a return type, which may be an object reference, primitive value or void. The void return declaration means that no value is returned, control is simply returned to the calling class. Methods with non-void return types must ensure that the appropriate object reference or primitive value is returned when the method completes. The method return value is like a message and often represents a property of the object, the product of a calculation or algorithm or a text output for instance.
A: A Java checked exception represents a problematic case that can be anticipated when one instantiates an object or calls a method. A typical example is when you attempt to create a file or open a URL connection, which may fail for many reasons. Checked exceptions must be declared in the throws statement of a method header, and any class that calls the method must ensure that it handles all checked exceptions that may occur.
More details available to subscribers:
What's the difference between checked and un-checked exceptions?
A: Dynamic method dispatch is the process the Java runtime system uses to determine which method implementation to call in an inheritance hierarchy. For example, the Object class has a toString() method that all subclasses inherit, but the String class overrides this method to return its string content. If a String or other object type is assigned to an Object reference using application logic, the Java compiler cannot know in advance where a call to the toString() method will be resolved, it must be determined dynamically at runtime.
More details available to subscribers:
What is dynamic method dispatch?
A: Abstraction and encapsulation are two quite separate concepts in Java. Abstraction is a technique that is used to represent common functionality amongst a set of classes. An analogy is to look at a set of vehicles: Car, Truck and Bus have some common features that all road vehicles share. From a Java perspective, the mechanics of turning four wheels, an engine, could be handled in abstract form through a common superclass. Abstraction allows subclasses to share common code without duplication.
More details available to subscribers:
What's the difference between abstraction and encapsulation?
A: The term static polymorphism is associated with overloaded methods because it gives the impression that a single named method will accept a number of different argument types. The System.out.println() method is an example that may take String or Object references, boolean and other primitive types as an argument. In fact, each overloaded method is separate and the compiler can see the difference between them. In this case, the argument types are fixed at compile time and are considered static. This has nothing to do with the Java keyword static.
Dynamic polymorphism is where a class overrides a superclass method or implements an interface. For example, any class may override the Object.toString() method and provide its own implementation, and this is known at compile time. However, for a simple Java program that instantiates a series of objects and calls their toString() method, the compiler does not consider the object references differently. Any differences in the objects' toString() implementations are only seen at runtime, so they are considered dynamic.
A: Java keywords are standard English words that have a special meaning in the Java programming language. Keywords include class, interface, abstract, public, static and final, which are used to declare the type and nature of Java compilation units. The statements used to define variables and method bodies include keywords new, return, if, while and throws. These words are interpreted by the Java compiler and used to produce byte code that can be run as a program.
More details available to subscribers:
What are keywords and reserved words?
println overloading or overriding?
A: The PrintWriter println() method is an example of overloading because several methods in the class have the same name and return the same type. In this case, println(boolean), println(int) and println(String) all have the same basic method name, "println", and all return void. The only part of the method signature that varies is the type of the argument (including none), which is enough for the Java interpreter to identify the appropriate method to call at runtime.
More details available to subscribers:
Is println overloading or overriding?
== matter?
A: With this sort of question it is often easiest to try it yourself and see. You will find that single boolean comparisons are equivalent whichever way round you have the values.
From a reader's point of view most people would put the "unknown" variable first because it is the subject of the comparison, but syntactically it does not matter.
if (a == 10) { }
A: There is no direct management of the storage size of code that is held in memory by the Java Virtual Machine (JVM). The virtual machine manages the allocation and de-allocation of storage through an integral garbage collection system. The JVM keeps track of all object references and periodically disposes of de-referenced objects to free memory.
It is possible to check the overall memory use of a Java application with methods in the java.lang.Runtime class. The freeMemory() method returns the spare memory available to the runtime system, the totalMemory() method reports the total memory allocated to the virtual machine.
A: Interfaces and abstract classes are part of the application programming interface for the Java language, but are never instantiated in their own right when a Java program is run. Only objects are instantiated in the Java Runtime Environment (JRE) and held in a division of the program's memory allocation called the heap. Stack memory holds primitive values and the memory addresses of objects on the heap.
A: A singleton class is one in which instantiation is restricted to ensure that only one instance is created for the current Java Virtual Machine. Singletons usually have a private default constructor, to prevent direct instantiation, and a static method to obtain a "new" reference to the single instance. On its first call, the static instance method creates the object using a private constructor and stores a static reference to it for all subsequent calls.
More details available to subscribers:
What is a singleton?
A: There is only one instance of a true singleton in a single virtual machine. If two virtual machines are running, two separate and independent instances of a singleton exist. If the singleton in question is governing access to the same system resource, there may be conflicts between the two systems, which is why the singleton design pattern is not ideal in this scenario.
A: Whenever you need to represent quantities that have specific formatting and equivalence requirements, it is best to use the Quantity design pattern. For a Money type, you can associate a Currency with the amount and can deal with all rounding issues in one class. Your money and currency types can then use generic rendering methods to show the amount however you choose.
More details available to subscribers:
How do I format my price correctly?
A: A factory method is typically used to obtain a new instance of a class, which may be one of several alternate implementations. The return type of a factory method is an interface or superclass type, which gives it the freedom to govern the actual class that is returned through polymorphism. This design pattern enables the factory to control and encapsulate the logic used to decide which instance to return.
More details available to subscribers:
What is a factory method?
A: An immutable class is one whose field values cannot be altered after instantiation, so all variable values must be assigned in the constructor and may therefore be declared final. By definition an immutable class should not have any modifier methods, but you must also be careful that the constructor and accessor methods do not expose mutable field references.
More details available to subscribers:
How should I create an immutable class?
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
See site help for questions about this site, our text ads and sponsored links services.