answersLogoWhite

0

Implicit casting is done automatically by the compiler and virtual machine.

Explicit casting is needed to convert types of data when Java is not sure if the result will be valid. There are two times when you will need to perform explicit casts: casting between primitives and casting between objects.

  1. Between primitives: Primitive types come in different sizes. For example, an int is 32 bit, whereas a long is 64. If you are sticking a smaller type into a larger type (eg. an int into a long), there is no need to cast since there is no risk that the value will be larger than its container. However, when you go in the opposite direction (eg. a long into an int), a compilation error will occur since Java is afraid the value will be too large for its type. To "silence" the compiler, you can cast the value to the intended type. In doing so, you risk the value overflowing its container. (Example below)
  2. Between objects: There are two types of object to object casts: upcasts and downcasts. Upcasting occurs when you take a subclass and stick it in a more general type (Object o = new String();). Upcasting is always implicit. Downcasting is going in the opposite direction. You always need to explicitly cast when downcasting, and Java will only allow you to do so if the conversion is possible. (Example below).

Examples between primitives:

byte b; short s;

s = b; //valid

b = s; //invalid

b = (byte)s; //valid

b = (byte)Short.MAX_VALUE; //valid, but overflow will occur.

Examples between objects:

Object o = s; //upcast - no explicit casting required

String s = (String)new Integer(); //always a compile error

String s = (String)getObject(); //assume that getObject() returns type Object. This will only work if the value stored in getObject() is a String or a subclass of String. Otherwise a runtime error will occur.

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

Which is easier 'C plus plus' or 'Java'?

Java is considerably easier than C++.


Which is more popular c plus plus or java?

Java


What is the difference between c plus plus and java programming?

Java doesn't have pointers. C++ has pointers.


What is feature that supported in c c plus plus and java but not in net?

C, C++ and Java are cross-platform languages. NET is for Windows-only.


Which one has more jobs java or c plus plus?

C++.


Difference between java and C plus plus?

java is an advanced object oriented programming language than c++


Should you learn c plus plus before you start learning java?

No!!!! You do not need to learn c++ for learning java!


Is it necessary to know c and c plus plus to learn java?

Of course not.


Advantages of C over C plus plus and java?

C can be faster than C++ programs, and definitely faster than Java, since Java is primarily interpreted. C is also somewhat less rigid in definitions as well, not as tightly structured as either C++ or Java can be.


Difference between procedure and function in C or C plus plus or Java language?

In C there are functions only, In Java methodsonly (static methods as well), in C++ both.


Why is java's portability better than c plus plus?

As we know that java is a plateform independent language and the main advantage of java is that it can support to any operating system and can be executed to any machines without any modifications.Due to the use of class in java it has become more easier to understand the program compared to c plus plus.Hence,java is portable than c plus plus


How do you build c plus plus compiler to java?

Not possible.