Hello world for example rectangle box box new 30 40 to

This preview shows page 2 - 4 out of 5 pages.

("Hello World"); For example: Rectangle box; box = new Rectangle (10, 20, 30, 40); To initialise an object variable, we must construct a new object using the keyword, new . The general form of this is: int i = 10 9 Constructing objects When we construct an object: • we must use the keyword new, 10 20 30 40 10001 (address in memory) 10001 box Rectangle box; box = new Rectangle( 10, 20, 30, 40 ) ; • the new ClassName() expression creates a new object and returns the memory address of this object, • we sometimes have to pass information ( arguments ) about the new object to initialise it, • the variable to which we assign the object stores the memory address of the object. 10 Visualising objects 10 20 30 40 10001 10001 box It is common to visualise the object variable as pointing to the actual object in memory. In the diagram above, the variable contains a memory address, 10001 . The actual object is stored at this address. 11 Object vs. primitive variables firstName Primitive variables "Adriana" Object variables price 1500 int price = 1500; 1010011 1010011 String firstName = new String("Adriana"); 12 Assigning to object variables word Primitive variables "dog" Object variables b 10 int a = 10; a 10 animal 1010011 1010011 1010011 int b = a; String animal = word; String word = new String("dog");
Image of page 2

Subscribe to view the full document.

CS101 Lecture 6 3 13 Strings and the keyword, new We have seen String literals in System.out.println() statements, e.g., Strings are special objects in Java. They are the only type of object that can be used without using "new". They are treated in this special way because they are such common objects. [we now know Strings are objects but we didn't use the keyword, new, to create the String object] System.out.println("Hi World"); 14 String objects and literals When the compiler comes across a String literal for the first time, it automatically constructs a new String object: "Hello" new String("Hello") automatically constructed So the code: String s = "Interesting"; System.out.println("Hi World"); String s = new String("Interesting"); System.out.println(new String("Hi World")); is exactly the same as: 15 Dot notation – instance methods What can we do with our objects once we have constructed them?
Image of page 3
Image of page 4
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern