File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes instance of operator example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instance of operator example" Watch "instance of operator example" New topic

instance of operator example

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
class Vehicle {

String name;
Vehicle() {
name = "Vehicle";

class HeavyVehicle extends Vehicle {

HeavyVehicle() {
name = "HeavyVehicle";

class Truck extends HeavyVehicle {

Truck() {
name = "Truck";

class LightVehicle extends Vehicle {

LightVehicle() {
name = "LightVehicle";

public class InstanceOfExample {

static boolean result;
static HeavyVehicle hV = new HeavyVehicle();
static Truck T = new Truck();
static HeavyVehicle hv2 = null;
public static void main(String[] args) {
result = hV instanceof HeavyVehicle;
System.out.print("hV is an HeavyVehicle: " + result + "\n");
result = T instanceof HeavyVehicle;
System.out.print("T is an HeavyVehicle: " + result + "\n");
result = hV instanceof Truck;
System.out.print("hV is a Truck: " + result + "\n");
result = hv2 instanceof HeavyVehicle;
System.out.print("hv2 is an HeavyVehicle: " + result + "\n");
hV = T; //Sucessful Cast form child to parent
T = (Truck) hV; //Sucessful Explicit Cast form parent to child
I was going through above example from link

got output like
hV is an HeavyVehicle: true
T is an HeavyVehicle: true
hV is a Truck: false
hv2 is an HeavyVehicle: false

I have not understood the output clearly.

Any ideas, resources,sample code,links, highly appreciated. thanks in advance.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14798

Please ShowSomeEffort.

Explain in more detail why you don't understand this code and its output.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And UseCodeTags.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
I agree. Here's the link:
subject: instance of operator example