Results 1 to 10 of 10

Thread: Java "Type"

  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Java "Type"

    What does type mean in java. Other than data type (primitive data type), what does it mean. I find it everywhere in every second sentence in every article, tutorial, book. I think it is used in different contexts. But I really need to know what exactly it means? Please reply asap.

    Thank you in advance.
    Don't forget to smile :-)

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default Re: Java "Type"

    Essentially Class Reference.

  3. #3
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Java "Type"

    For example, in the sentence "You can refer to an object that implements an interface by the type of the interface." , does " the type of the interface" mean the class which implements that interface?
    Don't forget to smile :-)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Java "Type"

    Quote Originally Posted by Nazneen Ali View Post
    "You can refer to an object that implements an interface by the type of the interface."
    That says that for any class that implements an interface, you may assign a instance of that class to a type of that interface.

    For example:

    Assume a class Foo that implements interfaces One, Two, and Three.

    You may do the following:
    Java Code:
    Foo  foo = new Foo();
    One one =  foo;
    Two two = foo;
    Three three = foo;
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Java "Type"

    That says that for any class that implements an interface, you may assign a instance of that class to a type of that interface.
    type of that interface
    You used the word "type". :s That's my question, what does type of an interface mean? Is it the class of the interface i.e. the class which is implementing the interface?
    Don't forget to smile :-)

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default Re: Java "Type"

    Quote Originally Posted by Nazneen Ali View Post
    You used the word "type". :s That's my question, what does type of an interface mean? Is it the class of the interface i.e. the class which is implementing the interface?
    Every Class definition and every Interface definition is a type. Whatever you declare a variable as, whether using a Classname or an Interfacename, is that reference variable's Type.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: Java "Type"

    No, it's the interface.
    In jim's example that would be One, Two or Three...Foo is not a type of interface, it's a type of class.

    A Java variable is either a primitive type or an Object type. An Object can be multiple types, depending on its hierarchy (an ArrayList is an AbstractList is a List is a Collection etc) and implemented interfaces.
    Please do not ask for code as refusal often offends.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default Re: Java "Type"

    As already said, every interface and every class IS a type. And whatever a reference is declared as, THAT is that references type. The object BEHIND the reference will consist of multiple types, but the reference has the type it is declared as. I.E. when you declare a reference using an interface then try to assign that value to a reference declared using a class, then you need to cast the value to that class type. The other way around you do NOT as the class type will contain the interface type and so the inference is already there, but not the other way around. IOW, the type of the reference is whatever it is declared as, that that also includes every type (i.e. class/interface) upon which that class/interface is based (i.e. extends or implements) is secondary.

    It is said that a reference is either "primitive" or "object" type as "object" is the "base" non-primitive reference type.

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: Java "Type"

    I agree.
    (I was replying to Nazneen question about "Is it the class of the interface i.e. the class which is implementing the interface?", in case we have some cross-posting confusion going on).

    But as they said in the OP, "I think it is used in different contexts", which sort of makes it difficult to narrow down which definition they want...:)

    You have the type of the reference, but you also have the type(s) of the object.
    Please do not ask for code as refusal often offends.

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 1
    Last Post: 11-02-2012, 11:38 PM
  3. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  4. Replies: 17
    Last Post: 05-28-2012, 08:30 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •