• Post Reply Bookmark Topic Watch Topic
  • New Topic

Run - Compile Time Error ? Upcasting Downcasting  RSS feed

 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question:   What are run time and compile Time Error .
Question2: Please Explain Upcasting and DownCasting Variables in simple words .
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such questions come from to avoid copyright problems.
There is no such thing as upcasting and downcasting. They are called widening conversion and narrowing conversion respectively, I know you will see upcasting and downcasting written, but those aren't the right terms. You can read about conversions here, but that may be difficult to read.
What do you think the difference is between a compile‑time error and a runtime error? And what is the most serious kind of error?
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source : http://courses.caveofprogramming.com/courses/2081/lectures/38475


Got no problem in casting numerical values .But in polymorphism there is a upcasting variables like and also downCasting .. its Confusing i.e
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saad Zahoor wrote:. . .
//Upcasting
. . .
There is no casting either way in your program.
Please tell us what they said in that lecture; I can't see it without paying. So I can't tell whether you are getting value for money or the opposite.
Do you know about casting primitives? What about this?
System.out.println((int)12345678910L);
What will that print?
That long value is 0x0000_0002_dfdc_1c3e. In binary that is 0000_0000_0000_0000_0000_0000_0000_0010*1101_1111_1101_1100_0001_1100_0011_1110
I have put the red * instead of a plain underscore at the midpoint of that binary String to make it a bit easier for you.
What will happen if you write this code?
System.out.println((String)Integer.valueOf(123));
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Gives me Error Integer can't be converted into String !!
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to know why you got that compiler error. It is impossible to cast a String to an Integer, because they do not share the same inheritance hierarchy. But what about this?The literal 123 can be boxed into an Integer which is a subtype of Number. Double is also a subtype of Number. What will happen if you change the code slightly?
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is Number !! As far as i know Number is not a DATA type .. Well it converts int into double !
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope! Number is a data type,though it is a abstract data type(you can see the abstract modifier in it's class declaration).any type(handling some kind of data) is a data type but what actually classify them is what kind of a data they are handling.for example String is a data type to handle text,List<T> is a data type to collectively handle items of type T and so on.
what actually you could say is Number is not a primitive data type.in java we have 2 kinds of types namely the reference type and the primitive type.the former is used to reference objects but the latter one directly points the value.in a oracle tutorial you would see the line Primitive values do not share state with other primitive values,it means suppose you have 2 int variables as
they both are pointing to the same value,later if you will change the value of i to 1 it is not going to change the state of j(it would remain 0→java is pass by value).their are 8 primitive types in java(for getting more on it kindly go through the tutorial link).
But java has also some boxed version of these primitive types in order to handle them as objects like Integer,Double,Float.because sometimes you may require the primitive type as Objects.one of the example is the List<T>,here you can only have a List of any type of object(primitive will not going to work here but the latter one does).
Specifically Number can be used to reference Data types like Integer,Double,BigInteger,AtomicInteger and so on you can see its sub classes in the API.

Praveen
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!