answersLogoWhite

0


Best Answer

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

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Casting is essentially converting one data type to another, such as:

double d = 4.0;

int c = d; //will give an error

Whereas code like this:

double d = 4.0;

int c = (int)d; //no error, 4 assigned to c

because you told Java you know you may lose precision (if d was equal to 4.9, c would still equal 4) but that you wish to simply truncate the other value, Java is happy to cast the double to an int.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

Casting is the conversion of one type to another type, better known as a type cast. A class constructor that accepts one argument is a conversion constructor because it converts its argument to an object of the class. However, if the argument is of the same type as the class then it is known as a copy constructor.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is casting in java and c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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 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 java use extern key word which is used in C plus plus?

No extern keyword in Java.

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++.


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

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


Difference between java and C plus plus?

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


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.


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

Of course not.


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


Can a c plus plus class be derived from a Java class?

No.