wood burning stoves
The moose likes Beginning Java and the fly likes what the mean of this line of code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what the mean of this line of code" Watch "what the mean of this line of code" New topic

what the mean of this line of code

vinaykumar singh

Joined: Jan 14, 2006
Posts: 13
hello all...
i m facing problem in this line of code ...whats the exactly mean of that.

ToHeader toHeader=(ToHeader)request.getHeader(args);

Class classObject = (class)classObject.methodmane(arg);

main prob is that i can not understand why (ToHeader)placed before request.getHeader(args) and whats that mean.

please help me...
Thanks in advance
vinay kumar singh
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Welcome to JavaRanch.

In order to answer the question one would need to know:
  • What kind of object is request?
  • What kind of class is ToHeader?
  • What is getHeader supposed to do?
  • What kind of object is classObject?
  • What is methodMane supposed to do?
  • How are the two lines interrelated?
  • Is it correct that a new object called 'classObject' is declared, which gets its value from a method call to another object also called 'classObject'?

  • If it's just the placing of '(ToHeader)' that confuses you, it means that the getHeader method is (probably) declared to return 'Object', and that -in order to assign it to a variable of type ToHeader- it needs to be cast to ToHeader. This casting will only succeed if the object returned by the method is in fact of type ToHeader. Since that is not checked here, one must assume that getHeader only ever returns objects of type ToHeader.
    [ January 14, 2006: Message edited by: Ulf Dittmer ]
    Edwin Dalorzo
    Ranch Hand

    Joined: Dec 31, 2004
    Posts: 961
    Hi, vinaykumar, comrade.

    Your question is a typical case of casting.

    Let's explan it first with primitive values. First you must understand that the hierachy of primitive values is like this...


    ...meaning with this that you can always assign a byte variable to a short one, a short one to an int one, and so forth.

    The contrary is not always true. Why? Well, for example, a byte varible could contain values from -128 up to 127, and an int variable can easily hold that, but an integer variable could contain -2^31 up to (2^31)-1, so a byte variable cannot always contain an int.

    As the compiler cannot know what real values of the variables are going to be at runtime, every time you want to store a wider data type into a narrower one, you will have to explicitly say so to the compiler by means of casting...

    That way the compiler makes sure that you know what you're doing, because this conversion might cause loosing some data.

    It is the same with Objects. Suppose that...


    ...is a class hierachy.

    This means you can assing to a Mammal object reference to a Lion object reference, like this:

    Now, you can do this without casting, because all Lions are Mammals, right? But the contrary is not necesarily true. I mean, not all the Mammals are Lions, correct? It could be a Fox, or any other kind of Mammal.

    So, when you want to do downcasting, that means assing an object reference from the parent class to an object reference of the child class you have to explicitly say so by means of the casting operator, like this:

    If asLan were not really a Lion, but a Fox, a ClassCastException will be thrown.

    I hope all this helps to understand casting. Now give a look at the method signature you're trying to understand and you'll see what I mean.

    Edwin Dalorzo

    [ January 14, 2006: Message edited by: Edwin Dalorzo ]
    [ January 14, 2006: Message edited by: Edwin Dalorzo ]
    jQuery in Action, 3rd edition
    subject: what the mean of this line of code
    It's not a secret anymore!