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 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Some clarifications

    Hey again, guys!

    I'm continuing my quest on learning java and I have a few ... missunderstandings. Perhaps you can enlighten me:

    Code:
    public class aclass {
       public static void main(String [] arg) {
               // an object of type string
               String s = "a string";
               // a Object type reference
               Object o = s;
    
    }
    
    }
    Now, in the book that I'm reading, as you can see from the code, the author describes s as an object of type string (i call it variable of type String - from C), and "O" an object type reference.

    What's the difference between them ? I mean, just as I would say String o = s;

    What does the Object keyword do in + or -.

    And another thing.

    What is the difference if I were to create it :

    Object o = new String(s);

    ?

    or


    Code:
    BufferedReader tastatura;
    tastatura = new BufferedReader(new InputStreamReader(System.in),1);
    I couldn't write the last(above) code as a variable type assignment.

    i'm really confused about some keywords "objects", "instances", "methods".

    Anyone got an Ideea of a quick - to - learn book, easily understandable with concrete examples (it's relative to each but...maybe I'll find some1 who understands them like me).

    Thanks you guys!

  • #2
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Hi, not sure about your second example - it looks like you have the syntax wrong for the BufferedReader thats why it doesnt work!

    Ok so Object is the base class for everything in java.

    Every object you make, if its a user defined one or a String, or a ArrayList or a LinkedList all extend from Object. That means EVERYTHING in java can be an object.

    Essentially:

    Code:
    Object anything = MyWeirdAssObject;
    That would be fine if I had a class called MyWeirdAssObject of which I had an instance of that class of the same name.

    Now:

    Instance is like a particular copy of an object.
    Method - is an operation on an object.

    So an example could be -

    An instance of my mug object.

    mug.drink() could be a method on the object.

    Get it!? These are simple Object Orientated principles you should probably just read wikipedia article for OO design


  •  

    Posting Permissions

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