Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| |||||||||||
Core Java - Interview Questions and Answers for 'Casting' - 7 question(s) found - Order By Rating | |||||||||||
| |||||||||||
Ans. Yes | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  type casting in typescript | |||||||||||
| |||||||||||
Ans. Assigning a value of one type to a variable of another type is known as Type Casting. Example : int x = 10; byte y = (byte)x; In Java, type casting is classified into two types, Widening Casting(Implicit) widening-type-conversion and Narrowing Casting (Explicitly done) narrowing-type-conversion. Widening or Automatic type converion - Automatic Type casting take place when,the two types are compatible and the target type is larger than the source type Example : public class Test { public static void main(String[] args) { int i = 100; long l = i; //no explicit type casting required float f = l;//no explicit type casting required System.out.println("Int value " i); System.out.println("Long value " l); System.out.println("Float value " f); } } Narrowing or Explicit type conversion - When you are assigning a larger type value to a variable of smaller type, then you need to perform explicit type casting. Example : public class Test{ public static void main(String[] args) { double d = 100.04; long l = (long)d; //explicit type casting required int i = (int)l;//explicit type casting required System.out.println("Double value " d); System.out.println("Long value " l); System.out.println("Int value " i); } } | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  typecasting  type casting Asked in 23 Companies | |||||||||||
| |||||||||||
Ans. Interfacing Car extends Vehicle Car car = new Car(); // now you need to call an API changeTires() which can change tires for all Vehicle Types and accepts argument of type Vehicle and hence you will have to upcast it changeTires((Vehicle)car); | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  upcasting  type casting | |||||||||||
| |||||||||||
Ans. 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. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   data types   casting  type casting   basic interview question Asked in 8 Companies basic   frequent | |||||||||||
| |||||||||||
Ans. An explicit conversion is where you use some syntax to tell the program to do a conversion whereas in case of implicit type casting you need not provide the data type. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   casting | |||||||||||
| |||||||||||
Ans. As we only downcast class in the hierarchy, The ClassCastException is thrown to indicate that code has attempted to cast an object to a subclass of which it is not an instance. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   class casting   classcastexception   exception   error Asked in 4 Companies intermediate   frequent | |||||||||||
Try 2 Question(s) Test | |||||||||||
| |||||||||||
Ans. Generalization or UpCasting is a phenomenon where a sub class is prompted to a super class, and hence becomes more general. Generalization needs widening or up-casting. Specialization or DownCasting is a phenomenon where a super class is narrowed down to a sub class. Specialization needs narrowing or down-casting. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   data types   casting  type casting   up casting  type casting   downcasting  type casting   generalization   specialization Asked in 2 Companies | |||||||||||
| |||||||||||