About Question enthuware.ocpjp.v7.2.1418 :
Moderator: admin
-
- Posts: 20
- Joined: Tue Nov 04, 2014 1:13 am
- Contact:
About Question enthuware.ocpjp.v7.2.1418 :
How do I know when to use Switch.OFF or just OFF?
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1418 :
You just have to remember that within a switch you have to use OFF. Everywhere else you have to use Switch.OFF.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Tue Nov 04, 2014 1:13 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1418 :
Ok. Thanks!
-
- Posts: 8
- Joined: Sun Dec 20, 2015 6:28 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1418 :
In the expanaition it says:
But wouldn't it be better to say:... A reference of type I can be cast to any class at compile time. ...
... A reference of type I can be cast to any non-final class at compile time. ...
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1418 :
Not necessarily. What if a final class or any of its super classes implements the interface I?
But I see what you are getting at. The problem is if you try to make such statements too precise, they will boil down to the Java langauge specification. So we try to explain the general principle and then cover the finer points in different question separately if required.
HTH,
Paul.
But I see what you are getting at. The problem is if you try to make such statements too precise, they will boil down to the Java langauge specification. So we try to explain the general principle and then cover the finer points in different question separately if required.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Thu Feb 19, 2015 8:25 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1418 :
I believe, that enum types like a String types should be compared by using equals, why it is possible to use both options, and what is the defference?
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1418 :
Enums are objects and every object in Java ultimately extends from Object class. Since Object class has a public equals method, you can use equals method on an enum as well.
The JVM ensures that there is exactly one instance for each member of the enumeration so it is OK (and, in fact, even recommended) to compare the enums using ==. That is why enums are allowed in switch statements as well. Object class's equals method also compares the references using == internally. So there is no difference unless, of course, the enum overrides the equals method, in which case, the result depends on how the equals method is implemented in the enum class.
HTH,
Paul.
The JVM ensures that there is exactly one instance for each member of the enumeration so it is OK (and, in fact, even recommended) to compare the enums using ==. That is why enums are allowed in switch statements as well. Object class's equals method also compares the references using == internally. So there is no difference unless, of course, the enum overrides the equals method, in which case, the result depends on how the equals method is implemented in the enum class.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Thu Feb 19, 2015 8:25 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1418 :
Thanks, very clear explanation.
-
- Posts: 10
- Joined: Fri Nov 18, 2016 5:48 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1418 :
I tryed with
public class TestClass {
enum Switch {
ON,
OFF
}
public static void main(String[] args) {
Switch s = Switch.OFF;
if (s == Switch.OFF) {
System.out.println("It is off!");
}
switch (s) {
case OFF.toString():
System.out.println("It is off!");
break;
}
}
}
But I got error on case OFF.toString()
Incompatible types. Found: 'java.lang.String', required: 'com.testInterfaces.dos.TestClass.Switch'
public class TestClass {
enum Switch {
ON,
OFF
}
public static void main(String[] args) {
Switch s = Switch.OFF;
if (s == Switch.OFF) {
System.out.println("It is off!");
}
switch (s) {
case OFF.toString():
System.out.println("It is off!");
break;
}
}
}
But I got error on case OFF.toString()
Incompatible types. Found: 'java.lang.String', required: 'com.testInterfaces.dos.TestClass.Switch'
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1418 :
If you are switching on an enum, each case must be a value in that enum, right? Why are you using case OFF.toString()?
I would suggest you to through a good book to learn the theory before attempting mock exams.
I would suggest you to through a good book to learn the theory before attempting mock exams.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 22 guests