[Tech.][Struts.][Servlet.][JSP.][EJB.][JNDI-JMS][SQL.][JDBC.]
CORE-JAVA: [OOP][CLASS][ABSTRACT][EXCEPTIONS][THREADS][UTILS][PACKAGES][JVM][CASTING][NETWORKING][RMI-XML]
What is casting? (casting) (Primitive Conversion)
There are two types of casting, casting between primitive NUMERIC types and casting between OBJECT references.
Casting between numeric types is used to convert larger values, such as DOUBLE values, to smaller values, such as BYTE values. This is Explicit Casting (Narrowing). Which needs conversion. Where as Implicit casting is direct assigning from one form to another.
(Ex:) The WIDENING primitive conversions: FROM (byte) TO (short, int, long, float, or double) do not lose any information, the numeric value is preserved exactly in direct convertion (strictfp) (implicit).
Lets consider: int in = 1234567890; float fl = in;
POSSIBLE: (fl - in = 0) OR (fl = in) (this is strictfp with precision) (implicit)
POSSIBLE: "in" is NOT EQUAL to "(int)fl" ( this is explicit casting, lose precision)
* The NARROWING primitive conversions: FROM (double) TO (byte, short, char, int, long, or float) may lose information and precision.
float fmin = Float.NEGATIVE_INFINITY; float fmax = Float.POSITIVE_INFINITY;
POSSIBLE: long lmax = (long)fmin; // (this is explicit casting) and will compile, BUT lose precision.
NOT-POSSIBLE: long lmax = fmin; // (this is strictfp) (implicit) will NOT compile.
* Casting between OBJECT references is used to refer to an object by a compatible class, interface, or array type reference. Also see: Object Casting.
MountainBike is descended from Bicycle and Object. Thus MountainBike can be assigned to Object or Bicycle. (Ex:)
POSSIBLE: Object obj = new MountainBike(); This is called implicit casting. (Sub-class to super class)
POSSIBLE: MountainBike myBike = (MountainBike)obj; This is explicit casting. (Super class to sub-class)
When can an object reference be cast to an interface reference? (casting)
An object reference be cast to an interface reference when the object implements the referenced interface.
Can a Byte object be cast to a double value? (casting)
Yes, a byte object can be cast to a double value explicitly but may lose precision. The correct way of conversion is directly assigning the byte to double implicitly. As this is a Widening primitive conversion.
Can a double value be cast to a byte? (casting)
Yes, a double value can be cast to a byte explicitly. As it cannot be done implicitly. As this is a Narrowing primitive conversion.
What is numeric promotion? (casting)
Numeric promotion is the Implicit conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required.
What is Downcasting? (casting)
Downcasting is the Explicit casting from a general to a more specific type, i.e. casting down the hierarchy.
What is the difference between instanceof and isInstance? (casting)
instanceof is a keyword operator where as isInstance() is a method in class "Class".
instanceof compares "object" with "Class Name". isInstance compares "object of Class" with "object of Class Name".
isInstance needs to be in try catch blocks and needs an instance of "Class" to be created with Class.forName("Class").
instanceof is used to check to see if an object can be cast into a specified type without throwing a cast class exception. (Ex:) (Ex:)
if (obj instanceof MountainBike) { MountainBike myBike = (MountainBike)obj; } (Explicit Casting)
isInstance() Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.
(Ex:) class Parent{}
class Child extends Parent implements MyInterface{}
interface MyInterface{}
Parent obj1 = new Parent(); Parent obj2 = new Child();
try { Class cls = Class.forName("Parent");
cls.isInstance(obj2); // true } catch (Throwable e) { }
obj1 instanceof Parent; // true
obj1 instanceof Child; // false
obj1 instanceof MyInterface; // false
obj2 instanceof Parent; // true
obj2 instanceof Child; // true
obj2 instanceof MyInterface; // true
What are wrapper classes? (casting)
A: Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes. They are e.g. Integer, Character, Double etc.
Why do we need wrapper classes? (casting)
A: It is sometimes easier to deal with primitives as objects. Moreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. Because of these reasons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an object.
What happens when you add a double value to a String?
The result is a String object.
What are wrapped classes?
Wrapped classes are classes that allow primitive types to be accessed as objects.
[Read:Realizations-In-Real-Life.][Interview-With-God!][He-is-the-one!][Convert-Bad-to-Good.][Good-Wishes.][River-Crossing-Puzzles.][Amazing-Vibrations.][Creative-Personality.][Religion.][Happy-Kingdom.][Significance-of-Nava-Ratna.][Story:The-Monkey’s-Justice.]
Realizations. Realizations In Real Life. Amazing Constructive Vibrations. Astrology. Creating Leaders. How ideas are concluded and decisions are materialized. Relationships & Partnerships. The path of victory. An attempt for definition. Speak with a heart. These are contagious. Happy kingdom. MIRACLES. Better to create one ! Cast, God and religion ! Things become inevitable ! We are all looking for! Phase of Life. Destiny, Karma and God. Struggle, perfection and Money. Independence and Freedom. Relationships and Happiness.
No comments:
Post a Comment