Put your text ad here
WestNIC provides reliable web hosting services
Web hosting directory, find affordable web hosting
Fastwebhost offers cheap web hosting & reseller hosting services
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.
println() overloading or overriding?
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.
… full answer hidden, click for full answers
Premium members click below for full answer
Is Java a fully object oriented language?
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.
… full answer hidden, click for full answers
Premium members click below for full answer
What are keywords and reserved words?
A: Java programming language keywords must be used literally in their standard English form, there are no alternative language equivalents for the new, extends and public keywords, for example. Primitive numeric values must be represented by standard ASCII numeric characters and syntax elements like comment markers and curly braces must be represented by the conventional ASCII characters.
Beyond such fundamental syntax constraints, Java class files can be created with alternative character encodings so you can type comments, class and variable names and String literals directly in your preferred language. Java also supports UTF-8 class file names.
… full answer hidden, click for full answers
Premium members click below for full answer
How can I develop in my local language in Java?
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. Java type cast behaviour is part of a general scheme of object substitution called polymorphism, in which an object “behaves like” and can be “treated as” an instance of another type.
// Implicit cast to object typeObject object =newStringBuffer("Example");// Implicit cast to interface typeComparable object =newFile("c:\Example.txt");
… full answer hidden, click for full answers
Premium members click below for full answer
What Java types are available?
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: Primitive data types are containers that hold references to fundamental numeric, logical, byte and character literal data and are built-in to the Java language and runtime system. They are called primitive because they have a relatively simple physical storage format and size. Primitives are not Java objects and objects cannot be cast to a type that could be stored in a primitive variable.
// incompatible types, does not compilelongl =newObject();
… full answer hidden, click for full answers
Premium members click below for full answer
What's the difference between primitive types and arrays?
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.
… full answer hidden, click for full answers
Premium members click below for full answer
What's the difference between abstraction and encapsulation?
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.
… full answer hidden, click for full answers
Premium members click below for full answer
Is println() overloading or overriding?
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.
… full answer hidden, click for full answers
Premium members click below for full answer
What is dynamic method dispatch?
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: The key to understanding the principle of polymorphism is the phrase “behaves like”. In Java you can say that a FileInputStream class behaves like an InputStream. It means that wherever you require an InputStream you can use a FileInputStream.
Java defines the “behaves like” relationship in a formal way with the extends and implements keywords. This technical detail doesn't alter the nature of the relationship, it just lays down the rules to follow when you write a program.
The “poly” aspect of the name polymorphism means that a class may behave like more than one thing, which makes it more versatile. First, a class behaves like itself. It must also behave like at least one superclass, the Object class. If a class extends another class it will behave like at least three things: itself, Object and its superclass.
A Java class may also implement an interface, which is another way to say “behaves like”. When an interface has a super-interface a class implementation behaves like both types.
The great advantage of polymorphism is that it allows you to create flexible, well structured Java programs where the concrete behaviour of the system is generic at a high level and specific at appropriate levels lower down. If an application is coded at the top level using interfaces and generic superclasses like InputStream, polymorphism allows you to use an input stream from a URL connection, a FileInputStream, or StringBufferInputStream to handle different input sources, for example.
The Java API is the ultimate example of the strength and necessity of polymorphism. The API defines many high level generic interfaces and classes for application programmers to build upon. Polymorphism is what makes the system extensible, modular and versatile.
A: Method overload is a design pattern that creates the illusion of a general purpose method that will accept a range of argument types. For example, the java.io.PrintStream attached to System.out has a range of overloaded print() and println() methods that each take a single argument, a primitive type, char[], Object or String. Since the overloaded methods have the same void return type and method name, they give the appearance that you can pass almost any variable type to the print() method.
voidprint(booleanb);voidprint(charc);voidprint(char[]c);voidprint(Object o);
Overloading gives the appearance of handling method arguments in a polymorphic way, including primitives, but in fact each overloaded method is type-specific and its implementation is separate and different. The print(Object) method can be truly polymorphic because any object type can be passed and implicitly cast. Overloading makes methods easier to learn and avoids the need to cast or convert variables to a particular type before they are passed as arguments. However, it can also make code difficult to read because it is less clear which overloaded method is called.
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.
| Front-end FAQs | Back-end FAQs | Learn Java |
|---|---|---|
About us: site help, text ads and premium content FAQs.