Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    What does Object as a class means?

    Recently, I have come across a method with a parameter of "Object".

    *Name of function*(Object obj){

    //Code here//

    }

    What does Object in this case means and how can I manipulate it code-wise? I have read the official java manual and still have no idea what that exactly mean, so I would appreciate some insight regarding how this class can be used.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Object is the datatype. Object is the supertype of any object implicitly, so it will accept any object. Erasure when using generics with collections won't work quite the same way for which you would then use a type of extension to Object if you want to allow anything other an an absolute Object type, but that's out of scope for what you have here now.
    PHP Code:
    public class T
    {
        public static 
    void printObject(Object o)
        {
            
    System.out.println(o.toString());
        }

        public static 
    void main(String[] argv)
        {
            
    T.printObject(new Animal());
        }
    }

    class 
    Animal
    {
        public 
    String toString()
        {
            return 
    "Animal[I'm an animal]";
        }

    The datatype is the instance of an object of that type. This differs from Class<?> which is the class template for an object and not an object in itself.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    PaulAsbome (06-14-2013)

  • #3
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Object is the datatype. Object is the supertype of any object implicitly, so it will accept any object. Erasure when using generics with collections won't work quite the same way for which you would then use a type of extension to Object if you want to allow anything other an an absolute Object type, but that's out of scope for what you have here now.
    PHP Code:
    public class T
    {
        public static 
    void printObject(Object o)
        {
            
    System.out.println(o.toString());
        }

        public static 
    void main(String[] argv)
        {
            
    T.printObject(new Animal());
        }
    }

    class 
    Animal
    {
        public 
    String toString()
        {
            return 
    "Animal[I'm an animal]";
        }

    The datatype is the instance of an object of that type. This differs from Class<?> which is the class template for an object and not an object in itself.
    If that's the case, does that mean in layman terms that object is just something that is actually everything even classes? Like String, Int, etc.

    However, if that's the case though, won't object work if it is actually an arraylist(in that example)? And if it is, how do we manipulate that?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    No. An object is a complex type that is an instance. A String is an object (in Java, this isn't the case in every language). Primitives like int, char, double, boolean are not objects, although all do have wrappers Integer, Character, Double, Boolean which are objects. The idea is the same, in that these are datatypes, but differ in that primitives are not the same as objects.

    A class (ie: Class<?>) is actually an object type, but not of the type defined. As in, String.class isn't an object of type String, its an object of type Class<String>. Sorry my previous statement wasn't quite clear on that; I meant that its not an instance of its defined type when referring to it as being an object.
    The easiest way to think of it is simply as a datatype. The datatypes you have are primitive or complex, where complex are things like arrays and objects, and primitives are everything else. An object is simply an instance of a class constructed typically with the new keyword. The Object datatype is the implicit supertype of every complex datatype.
    So the easiest thing to do is simply identify the primitives in Java: byte, short, int, long, float, double, boolean, and char. Every other datatype is an object (this does include enums and arrays in java).
    Classes / templates shouldn't be considered as an object type; only instances of said template are an object type. Since a class itself cannot be used as a variable of its class type (if that makes any sense :/), it cannot possibly be defined as a type of object despite having implicit static methods: .class.
    So I cannot do this:
    PHP Code:
    class T
    {
        public static 
    void main(String[] argv)
        {
            
    T o T;
        }

    o cannot be assigned a datatype of T and be given an "object" that is in itself T (not an instance of T, but T in itself). Such behaviour really doesn't make any sense anyway. Typically, anything you'd do that would require an "instance" of a class (which isn't possible in the thought path we're on here since an instance of a class would be invoked with typically the new keyword), is used via reflection of the class type. That is, in java, you simply call ClassName.class and it gives you a Class<ClassName> back. You can operate on this to perform tasks like calling methods, changing visibility of methods and properties, and invoking new instances.

    Class: the blueprint in which complex types are manufactured
    Object: the result of construction off of the template defined by a class
    primitive: explicit memory size and compiler knowledge of what to do with it. I can easily recast any primitive to anything I want. Nothing stops me from turning the char 'A' into the number 65 (since they are identical and a cast would show that). Character cannot be cast to an Integer though, since a Character is not a subclass of Integer.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    PaulAsbome (06-15-2013)

  • #5
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    No. An object is a complex type that is an instance. A String is an object (in Java, this isn't the case in every language). Primitives like int, char, double, boolean are not objects, although all do have wrappers Integer, Character, Double, Boolean which are objects. The idea is the same, in that these are datatypes, but differ in that primitives are not the same as objects.

    A class (ie: Class<?>) is actually an object type, but not of the type defined. As in, String.class isn't an object of type String, its an object of type Class<String>. Sorry my previous statement wasn't quite clear on that; I meant that its not an instance of its defined type when referring to it as being an object.
    The easiest way to think of it is simply as a datatype. The datatypes you have are primitive or complex, where complex are things like arrays and objects, and primitives are everything else. An object is simply an instance of a class constructed typically with the new keyword. The Object datatype is the implicit supertype of every complex datatype.
    So the easiest thing to do is simply identify the primitives in Java: byte, short, int, long, float, double, boolean, and char. Every other datatype is an object (this does include enums and arrays in java).
    Classes / templates shouldn't be considered as an object type; only instances of said template are an object type. Since a class itself cannot be used as a variable of its class type (if that makes any sense :/), it cannot possibly be defined as a type of object despite having implicit static methods: .class.
    So I cannot do this:
    PHP Code:
    class T
    {
        public static 
    void main(String[] argv)
        {
            
    T o T;
        }

    o cannot be assigned a datatype of T and be given an "object" that is in itself T (not an instance of T, but T in itself). Such behaviour really doesn't make any sense anyway. Typically, anything you'd do that would require an "instance" of a class (which isn't possible in the thought path we're on here since an instance of a class would be invoked with typically the new keyword), is used via reflection of the class type. That is, in java, you simply call ClassName.class and it gives you a Class<ClassName> back. You can operate on this to perform tasks like calling methods, changing visibility of methods and properties, and invoking new instances.

    Class: the blueprint in which complex types are manufactured
    Object: the result of construction off of the template defined by a class
    primitive: explicit memory size and compiler knowledge of what to do with it. I can easily recast any primitive to anything I want. Nothing stops me from turning the char 'A' into the number 65 (since they are identical and a cast would show that). Character cannot be cast to an Integer though, since a Character is not a subclass of Integer.
    In short, do you mean that every data types are from a class by itself. However, in this case Object, the only Classes we are only able to use are complex method(including array and enum) and not primitive ones. Am I right?

    If that's the case, to manipulate say a string arraylist where it accepts an object, would it be something like this

    [PHP]
    public class item{

    public string item(Object obj){

    for(str sbj){
    System.out.println(s);
    }
    }
    }

    [PHP]


  •  

    Posting Permissions

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