Week 2: Intro to Object Oriented Programming and Memory Usage

Tariq Hook Blog

Share

This was a special week for the zipcoders. Why? This week, they were formally introduced to the main focus of the course: Java.

No, I’m not referring to coffee. Rather, the programming language named after coffee. Java is one of the most important languages in the world. It is also widely used in schools across the world as an introductory language to programming in general; learning Java enables programmers with the ability to easily adapt to other languages in the future.

Today, Java is the language of choice inside large-scale banking applications, insurance exchanges, and even your local ATM. Java programs can run anywhere, on almost any piece of hardware.

One of the first Java concepts taught to zipcoders was how you can make languages like Java work with you, a human, by organizing your code into a structured and sensible format. This idea is known as Object-Oriented Programming (OOP).

Object-Oriented Programming (OOP)

As the name implies, objects are the centerpieces of Java. And similar to objects in the real world, objects in the abstract world of OOP also possess properties and can perform actions. Developers refer to these properties as fields and the actions as methods. Before we can utilize OOP let’s take a look at one of it’s building blocks.

Straight out of the box, Java comes equipped with many objects. Along with the objects come primitives which the objects are built on. You use primitives to specify simple properties such as a number. To mention a couple, you have the int primitive type which represents integers (1, 45, 1768) and a char which represents a single character (‘A’, ‘a’, ‘\u039A’). Let’s use these right now:

int number = 35;

// Latin small letter 'a' with a grave: à
// This is a unicode representation.
// Unicode is a universal way of representing different
// characters as they are not the same on keyboards around
// the world.
char accentedA = ‘\u039A’;

The rule for declaring a primitive is rather straight forward. The above corresponds to the following syntax:

 <type> <reference_name> = <value>

The can be anything your heart desires but there are conventions the Java community follows. I suggest you do so as well.

You probably noticed the lines that begin with //. This tells Java that you want to write a comment. A comment is a convenient way for developers to write notes about code or render some code useless. The way this works is that when Java sees // it knows to not include anything on the same line as part of the actual code that get executed.

Having gone over primitives, we can go back to OOP and objects. Remember objects have properties and actions or fields and methods. Let’s see what an object looks like:

public class Person {
private String name;
private int age;

// a “String” is simply made up of characters (chars) put together
// it is a built-in Java class
public Person(String name, int age) {
this.name = name;
this.age = age;
}

public void describeSelf() {
System.out.println(“My name is “ + this.name + “. I am “ + this.age + “ years old");
}
}

This might seem daunting. Take a breather and let us dissect this together. Here, I am creating a class object. Simply put, a class object is a blueprint. The concept of public and private is beyond this article; specifying an object as public allows us to use it anywhere in and out of our class file. private does the opposite, disallowing the use of objects from outside the class file. Our class, called Person, has two fields (name and age) and one method – (describeSelf()). Also included in our class is a constructor method, Person(String name, int age).

These kind of methods will allow us to set the name and age of a Person when we create one. Notice the name of this method is exactly the same as our class. This is always the case for constructor methods. To help explain further, let’s proceed to using our class:

Person person = new Person(“Bruce Wayne”, 35);

The syntax this line uses is the following:

<object_type> <reference_name> = new <object_constructor>;

The new keyword tells Java that you want to create a brand new copy of the object or an instance. When you use the new keyword, Java will allocate memory for your new object (more on that soon). The constructor method used, Person(“Bruce Wayne”, 35), takes two parameters or arguments. Remember our Person class constructor method:

public Person(String name, int age) {
this.name = name;
this.age = age;
}

When you call a method with values inserted, you are passing parameters to the method. So “Bruce” maps to String name and 35 maps to int age. Recalling that this constructor belongs to the Person class, the this keyword would refer to the class object followed by a period (.) and the name property of the class. This is known as dot syntax and it is a super convenient way of accessing fields and methods of classes.

We then set that property to the parameter we passed in, “Bruce Wayne”.Try not to be confused by the recurrence of name and age. The this.name variable refers to the class field name which you can always confirm from the presence of the this keyword behind it. The same applies for age.

Now that you understand this, you can easily grasp the concept of the describeSelf() method. First let’s call the method:

Person bruce = new Person(“Bruce Wayne”, 35);
person.describeSelf();

Those lines will cause your system to print out “My name is Bruce Wayne. I am 35 years old”. Although not difficult, understanding System.out.println() is outside the scope of this article but keep in mind that it prints a line out to your system. Recall our recent discussion on dot syntax above. Here we’re using it to call the describeSelf() method with our bruce object. And if you remember, bruce is an object of type Person which has a method called describeSelf(). This method uses the name and age fields of the class by printing out a string. These fields are initially set using the constructor method: Person bruce = new Person(“Bruce Wayne”, 35).

Memory

Could you imagine a book without a table of contents or an index? The presence of these pages in a book provide a way to plan your reading and quickly navigate to a topic very quickly. They list references to all notable topics in a book and in the case of the table of contents, allow one to take note of exactly how long each section of the book is. This is (vaguely) how memory in Java works. This week, zipcoders have moved on from getting their feet wet to jumping right into the pool… the memory pool.

Java’s memory pool is understood to be composed of two parts: the stack and the heap. The stack stores primitives and the references to the objects while the heap stores the actual objects. The stack is our table or contents or index and the heap is our actual content. The stack is efficient in that it is initialized with the exact amount of memory needed for the references it will hold. The heap has a bigger memory size because the size of objects can vary greatly.

Take a look at this method:

public void aMethod(int num) {
int aNum = num;
String aStr = “Hello, World”;
Object obj = new Object();
}

aMethod method is designated space in stack memory with the following references and objects contained:
⁃ aNum reference is stored in stack
⁃ aStr reference is stored in stack
⁃ “Hello, World” is stored in string pool in heap

Allow me to be the first to congratulate you on becoming a Java developer. Much like the drink, it’s not easy to give it up once you’ve started.

Want to learn more Java? Check out more about our curriculum and apply today!

Share