About Question enthuware.ocajp.i.v7.2.1372
Moderator: admin
-
- Posts: 2
- Joined: Wed Dec 31, 2014 8:01 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1372
Assume that it is called with a very large integer.
void printMe(Object[] oa){
for(int i=0; i<=oa.length; i++)
System.out.println(oa);
}
I think the right raised exception is ArrayIndexOutOfBoundException
but the question shows that NullPointerException is the right one. I think this is wrong. ??!!!
void printMe(Object[] oa){
for(int i=0; i<=oa.length; i++)
System.out.println(oa);
}
I think the right raised exception is ArrayIndexOutOfBoundException
but the question shows that NullPointerException is the right one. I think this is wrong. ??!!!
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
The comment, "Assume that it is called with a very large integer. " is for the previous code snippet. It is written above the number 2.
For the code that you've pasted, the comment is, "Assume that it is called as such: printMe(null);".
For the code that you've pasted, the comment is, "Assume that it is called as such: printMe(null);".
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Wed Dec 31, 2014 8:01 am
- Contact:
[RESOLVED] Re: About Question enthuware.ocajp.i.v7.2.1372
You are totally right.
Im sorry for the confusion.
Im sorry for the confusion.
-
- Posts: 6
- Joined: Mon Feb 23, 2015 9:37 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
I don't really understand why should this piece of code throw ClassCastException :
Object m1(){
return new Object();
}
void m2(){
String s = (String) m1();
}
I mean , should it be an object obj of the class XXX that wrapps these methods , a call to obj.m2() , would throw a ClassCastEXxception , but literally this piece of code doesn't throw any exception.
IMHO you should mark
StackOverflowError
NullPointerException
No Exception Will Be Thrown
as the correct option
Object m1(){
return new Object();
}
void m2(){
String s = (String) m1();
}
I mean , should it be an object obj of the class XXX that wrapps these methods , a call to obj.m2() , would throw a ClassCastEXxception , but literally this piece of code doesn't throw any exception.
IMHO you should mark
StackOverflowError
NullPointerException
No Exception Will Be Thrown
as the correct option
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
Hello,
I am sorry but I'm not sure I understand your question. The given code does throw ClassCastException. You may want to try it out.
HTH,
Paul.
I am sorry but I'm not sure I understand your question. The given code does throw ClassCastException. You may want to try it out.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Mon Feb 23, 2015 9:37 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
Ok maybe I didn't express myself well enoughadmin wrote:Hello,
I am sorry but I'm not sure I understand your question. The given code does throw ClassCastException. You may want to try it out.
HTH,
Paul.
Class Test
{
Object m1(){
return new Object();
}
void m2(){
String s = (String) m1();
}
public static void main(String[] args)
{
Test obj = new Test();
obj.m2();// only at this point does the ClassCastException occur
}
}
The way is presented ( without the call obj.m2() ) does not throw any exception ,hence my doubt ...
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
Ok, I see your point. These are just code snippets and not complete executable classes. Since the question explicitly says, "...when the following code snippets are executed", you have to assume that the given code is somehow executed. This format is used in the question in real exam as well.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Apr 05, 2015 11:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
Hi Paul,
In the explanation section it says:
Is it in the book - Appendix B- Study Tips? Or on the EnthuWare website?
Thanks
In the explanation section it says:
Can you please tell me where can I find the Study References section?Please read ExceptionClassSummary document in the "Study References" section.
Is it in the book - Appendix B- Study Tips? Or on the EnthuWare website?
Thanks
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
It is in ETS Viewer itself. In the tree view, click on the "Study View" tab. There you will see Study References node. Expand that.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Fri Oct 16, 2015 1:25 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
Hello Paul,
I am a bit confused with point 3:
Object m1(){
return new Object();
}
void m2(){
String s = (String) m1();
}
I know that ClassCastException is thrown when an object fails an IS-A test with the class type to
which it’s being cast. But all Java Classes inherit from Object class. I guest this means that String and Object have a IS-A relationship.
but when I try to do a instanceof check I receive false.
public class Test78
{
Object m1(){
return new Object();
}
void m2(){
String s = (String) m1();
}
public static void main(String[] args)
{
Test78 obj = new Test78();
System.out.println(obj.m1() instanceof String);
}
}
Many Thanks,
Regards,
Efim
I am a bit confused with point 3:
Object m1(){
return new Object();
}
void m2(){
String s = (String) m1();
}
I know that ClassCastException is thrown when an object fails an IS-A test with the class type to
which it’s being cast. But all Java Classes inherit from Object class. I guest this means that String and Object have a IS-A relationship.
but when I try to do a instanceof check I receive false.
public class Test78
{
Object m1(){
return new Object();
}
void m2(){
String s = (String) m1();
}
public static void main(String[] args)
{
Test78 obj = new Test78();
System.out.println(obj.m1() instanceof String);
}
}
Many Thanks,
Regards,
Efim
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
You have not understood the meaning of IS-A test. Yes, String and Object do have a IS-A relationship, but what you need to ask yourself is which one of the two is-a the other one. Think about it like this. Animal and Dog have an IS-A relationship, but is every Animal a Dog? or is every Dog an Animal? This is what is really important.
Here, Object is not a String, so you cannot cast an Object object to a String. But every String object is an Object, therefore you can cast any String object to Object.
HTH,
Paul.
Here, Object is not a String, so you cannot cast an Object object to a String. But every String object is an Object, therefore you can cast any String object to Object.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Fri Oct 16, 2015 1:25 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
Hello Paul,
Now I understand,
Many Thanks,
Efim
Now I understand,
Many Thanks,
Efim
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
Referencing the "Casting Objects" section from the JLS: https://docs.oracle.com/javase/tutorial ... asses.html
We have:
So in this question's case, casting from Object to String is not allowed because we are casting the object itself (from the return of m1() ) rather than a reference that actually points to the type being casted to (i.e. String).
Is that correct? You can't cast objects "downward" (but you can "upward") and that casting downward only compiles if
1) you are casting a reference, and
2) the reference is pointing to the object of the same type to which you are casting
We have:
Code: Select all
Object obj = new String();
String myString = obj; // will not compile
String myString = (String) obj; // will compile
Is that correct? You can't cast objects "downward" (but you can "upward") and that casting downward only compiles if
1) you are casting a reference, and
2) the reference is pointing to the object of the same type to which you are casting
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
Your question indicates that you have not understood several fundamental concepts about objects, references, variables, and types. I will try to answer as best as I can in a post but I will suggest you to go through a good book to first get your fundamentals right and then attempt the questions.
Second, the run time i.e. the JVM checks whether the actual object that is being referred to by the variable is actually of the target type. If it is not, then it will cause a ClassCastException to be thrown.
These checks are nothing but the test of is-a relationship. An object of a type is always also of its super type. For example, an object of type String is always of type Object. Thus, you can always cast a String variable to Object. ie. String str = ""; Object obj = (String) str; is always valid and is also redundant.
The reverse i.e. Object obj =""; String str = (String) obj; has to go through a two step checking process.
1. Is it possible for obj to refer to an object of type String. Yes, it is. So the compiler is happy.
2. Is obj actually referring to an object of type String. Yes, it is. So the JVM is happy.
An unrelated cast such as Integer i = 10; String s = (String) i; will fail at compile time because it is not at all possible for i to point to an object of type String.
HTH,
Paul.
There are no two things like "casting an object" and "casting a reference". There is only one thing, "casting". You cast a variable of one type to another type. You don't cast objects. Objects don't change their types at all. This is a two step process - first, the compiler checks whether it is possible for a variable of a type to refer to an object of the required target type. (This is a complicated statement, so you might want to read it multiple time until you learn by heart). If it finds out that it is not possible, then the compiler raises a compilation error.jamesmccreary wrote: So in this question's case, casting from Object to String is not allowed because we are casting the object itself (from the return of m1() ) rather than a reference that actually points to the type being casted to (i.e. String).
Second, the run time i.e. the JVM checks whether the actual object that is being referred to by the variable is actually of the target type. If it is not, then it will cause a ClassCastException to be thrown.
These checks are nothing but the test of is-a relationship. An object of a type is always also of its super type. For example, an object of type String is always of type Object. Thus, you can always cast a String variable to Object. ie. String str = ""; Object obj = (String) str; is always valid and is also redundant.
The reverse i.e. Object obj =""; String str = (String) obj; has to go through a two step checking process.
1. Is it possible for obj to refer to an object of type String. Yes, it is. So the compiler is happy.
2. Is obj actually referring to an object of type String. Yes, it is. So the JVM is happy.
An unrelated cast such as Integer i = 10; String s = (String) i; will fail at compile time because it is not at all possible for i to point to an object of type String.
The words downward and upward tend to get confusing. The right terminology is "more specific" and "less specific" - you can always assign and cast a variable of one type to a type that is less specific (e.g. String to Object). The reverse i.e. cast a variable of one type to a type that is more specific (e.g. Object to String) is possible only if it passes the two checks mentioned above.Is that correct? You can't cast objects "downward" (but you can "upward") and that casting downward only compiles if
1) you are casting a reference, and
2) the reference is pointing to the object of the same type to which you are casting
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372
Paul, thank you so much for your patient and thoughtful response. I now fully understand casting; I had never come across that two-step process before in my study preparation. I will add this to my notes and this will be extremely helpful for anyone else as well who is confused about the casting mechanism.
Whenever I contribute on this forum to Casting questions, I can now refer to your response (and give you due credit!).
Whenever I contribute on this forum to Casting questions, I can now refer to your response (and give you due credit!).
Who is online
Users browsing this forum: Bing [Bot] and 55 guests