Interface instanceof

G

gk

interface MyInterface {

}

public class MyInstanceTest implements MyInterface {

static String s;

public static void main(String args[]) {

MyInstanceTest t = new MyInstanceTest();

if (t instanceof MyInterface) {

System.out.println("I am true interface");

} else {

System.out.println("I am false interface");

}

if (s instanceof String) {

System.out.println("I am true String");

} else {

System.out.println("I am false String");

}

}

}



output:

I am true interface
I am false String


this is quite confusing.

see the code...
if (t instanceof MyInterface)
t is not polymorphically created even .....its a damn instance of
MyInstanceTest class .
i dont understand how its going to become an instance of MyInterface
and the output "I am true interface"

this would have been if we had the code like this..

MyInterface t = new MyInstanceTest(); // but its not here



And about the String class see the code...
if (s instanceof String)

is this if block is false because of s is not created via new operator
?


Please explain
 
O

Oliver Wong

gk said:
interface MyInterface {

}

public class MyInstanceTest implements MyInterface {

static String s;

public static void main(String args[]) {

MyInstanceTest t = new MyInstanceTest();

if (t instanceof MyInterface) {

System.out.println("I am true interface");

} else {

System.out.println("I am false interface");

}

if (s instanceof String) {

System.out.println("I am true String");

} else {

System.out.println("I am false String");

}

}

}



output:

I am true interface
I am false String


this is quite confusing.
[...]

is this if block is false because of s is not created via new operator
?

It's false because s is null, and null is considered to not be an
instanceof anything (not even Object).

- Oliver
 
G

gk

what about t ?




Oliver said:
gk said:
interface MyInterface {

}

public class MyInstanceTest implements MyInterface {

static String s;

public static void main(String args[]) {

MyInstanceTest t = new MyInstanceTest();

if (t instanceof MyInterface) {

System.out.println("I am true interface");

} else {

System.out.println("I am false interface");

}

if (s instanceof String) {

System.out.println("I am true String");

} else {

System.out.println("I am false String");

}

}

}



output:

I am true interface
I am false String


this is quite confusing.
[...]

is this if block is false because of s is not created via new operator
?

It's false because s is null, and null is considered to not be an
instanceof anything (not even Object).

- Oliver
 
G

gk

what about t ?




Oliver said:
gk said:
interface MyInterface {

}

public class MyInstanceTest implements MyInterface {

static String s;

public static void main(String args[]) {

MyInstanceTest t = new MyInstanceTest();

if (t instanceof MyInterface) {

System.out.println("I am true interface");

} else {

System.out.println("I am false interface");

}

if (s instanceof String) {

System.out.println("I am true String");

} else {

System.out.println("I am false String");

}

}

}



output:

I am true interface
I am false String


this is quite confusing.
[...]

is this if block is false because of s is not created via new operator
?

It's false because s is null, and null is considered to not be an
instanceof anything (not even Object).

- Oliver
 
M

Matt Humphrey

gk said:
what about t ?

<snip code>

t points to an object of class MyInstanceTest which implements MyInterface
which means t's object is also an instance of MyInterface. This is how Java
gets its multiple inheritance. Like Oliver pointed out with "s" if t were
null it wouldn't be an instance of either MyInstanceTest or MyInterface.

Instance of is determined by looking at the class of the object at
runtime--not by looking at the type represented by the variable.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
P

Patricia Shanahan

gk said:
interface MyInterface {
}
public class MyInstanceTest implements MyInterface {
static String s;
public static void main(String args[]) {
MyInstanceTest t = new MyInstanceTest();
if (t instanceof MyInterface) {
System.out.println("I am true interface");
} else {
System.out.println("I am false interface");
}
if (s instanceof String) {
System.out.println("I am true String");
} else {
System.out.println("I am false String");
}
}
}

To understand the output, see the Java Language Specification, 15.20.2
Type Comparison Operator instanceof,
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#80289

"At run time, the result of the instanceof operator is true if the value
of the RelationalExpression is not null and the reference could be cast
(§15.16) to the ReferenceType without raising a ClassCastException.
Otherwise the result is false."

(x instanceof Y) effectively asks "Does x currently point to an object
that could also be pointed to by a reference expression of type Y?"

(t intanceof MyInterface) is true because t points to an object, an
instance of MyInstanceTest, that could be pointed to by a reference
expression of type MyInterface.

(s instanceof String) is false because s does not point to any object at
all.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,529
Members
45,001
Latest member
Kendra00E1

Latest Threads

Top