Java: Object References (My Hero Academia Edition)

Today I’ll be explaining object references in Java programming through the Feynman Method with references from the popular Shonen Jump anime “My Hero Academia”. This is part of my Feynman/20-hr Method series. For more information, please head here.

So What Are Objects in Java?

Objects in Java are the actual value you’re using to describe everything in your class. If we were the exposé photographer “Taneo Tokuda” in “My Hero Academia” and we needed to figure out who All-Might’s successor was, the objects, ie. candidates would be the students in class 1-A. The objects would be the students and their individual stats and the class would be what category they fall into. To explain further, let’s talk about classes and reference types.

Taneo Tokuda and Midoria

What are Classes in Java?

If you’ve done any kind of programming with CSS, HTML, or JavaScript, you may have heard of the term “class”. In HTML and CSS, classes are an attribute. In our current programming universe, JavaScript functions are most akin to Java classes. Although there are functions also in Java strings, you can consider those more of a sub-category.

Just a short refresher: in JavaScript, a function is a list of instructions. In Java, it’s pretty close to the same thing, but in Java, the entire file is a class with subclasses.

my hero academia class 1-A

Some Reference before we move further

If you’re not familiar with My Hero Academia (MHA), it’s an anime about students who are training to become professional superheroes. Below is a breakdown of what I’ve compiled of class 1-A. Some of the information, such as stat rating and stat ranking I pulled from various MHA wikis. The “provisional license number” is fictitious to justify the use of the “long” class type. The full compiled list of the students in class 1-A is below:

Java Object Reference Coding Anatomy

Outside of the main file/class name, when we write reference and objects in code, this is the general formatting:

className variableName = value OR new className();
  • className – This can either be a Java programming basic built-in reference type or a user-created class name (this would have needed to be declared before use).
    We will not be covering user-made class names in this post.
  • variableName – This would be the user issued name for value in the object.
  • value Or new className( ); – This would be the actual value in the object.

The “reference” for the above refers to the “className” with the “variableName”. The object consists of the “variableName” tied to the “value/new className()”. Visually, it looks like this:

Created ReferenceCreated Object
className variableNamevariableName = value/new className()
Classes/References can have multiple objects. Objects cannot have multiple classes/references.

Yes, the “variableName” is mentioned twice, but in our examples, we’ll use it once. Think of it as the link that ties objects and class together. In Java, your class can have multiple objects, however, since an object is considered an instance (a pre-recorded moment in time) there cannot be more than one class/reference to an object.

I’ll explain more about this at the end of this post.

Let’s put this to work.

User Created Class

If I were to use a UA student as an example, say Tenya Ida, and added his hero name into code, it would look something like this:

uaStudent tenyaIda = ingenium;
  • className – uaStudent (a user created class)
  • variableName – tenyaIda (in camel casing)
  • value OR new className(” “); – ingenium; (the object value)

User Created Class with new className Object

shoto todoroki

If we were to use the “new className( );” output for Shoto Todoroki, while using a user-created class, it would look like this:

uaStudent shotoTodoroki= new uaStudent("shoto");

In the final “value OR new className(” “);” object value, we use “shoto” as the parameter.

Java Built-In Class with new className Object

If we decided to use a built-in Java object reference, it would look like this:

string heroName = new String("Uravity");
  • className – string (built-in Java object reference)
  • variableName – heroName (user created variable reference name for value)
  • value OR new className(” “); – new String (“uravity”); (this is the new value, which is declared as a string with “uravity” as the parameter.

Data Types vs Reference Types in Java

Previously, I discussed what primitive numeric data types were in Java. Reference types share some of the same built-in classes as data types. There are only three differences: Integer, Character, and String classes. Here are a list of the baked-in Java object reference class names:

NumberDecimalsSingle letterMultiple Letters
ByteFloatBooleanString (obj ref only)
ShortDoubleCharacter (not Char)
Integer (not Int)
Long

In object references, integer and character are written in full rather than their shorthand counterparts in primitive data types. There’s also a new “string” data type, which are sentences or words within double-quotes. Examples of this are listed in the samples of Java code above for “Uravity” and “Shoto”.

String Functions

Mei Hatsume

With the introduction of strings, we now have the ability to add some functionality to our coding. For those MHA fans out there, think of this as the support course’s area. Here are some string functions:

.length().compareTo().concat()
.isEmpty().trim().toLowerCase()
.toUpper().valueOf().replace()
.contains().equals/.equalsIgnoreCase().toCharArray()
.getBytes().isEmpty().endsWith()

These are usually added to the end of a string variableName after it’s been declared. Occasionally they will be added in the output line in code. If a new value is needed to be recalled more than once, then the changes can happen during the establishment of the variableName and new value. The information added within the “()” is part of the string function plus the associated variableName to alter the final variable value.

If you’re confused, this is what it would look like:

variableName.function(parameters/arguments);

For you MHA fans, think of it as Mei Hatsume adding support gear to one of the students to enhance/change/or upgrade their abilities.

Examples of Functions in Action

Yuga Aoyama
String yugaAoyama = "Can't Stop Twinking";
System.out.println(yugaAoyama.toLowerCase();

The application of the “.toLowerCase();” function is added immediately to the variableName, like an attachment to symbolize that the value inside this variable is to go through this function.

The example above returns the value as “can’t stop twinkling”, changing all uppercase letters to lower case. In this example, there is no parameter/argument needed for the “()”.

Here’s an example of a function that requires parameters/arguements:

1  String katskiBakugo = "Lord Explosion Murder";
2  String katskiBakugoHeroName = katskiBakugo.replace("Lord", "Great").concat(" God Dynamight");
3  System.out.println(katskiBakugoHeroName);
  • In line 1 I establish the value of katskiBakugo as “Lord Explosion Murder”. Yeah, not very “hero-y”, we know.
  • In line 2, I create a new variableName called katskiBakugoHeroName. since there’s a possibility I’ll need the previous value of katskiBakugo. Line 2 goes through the following changes:
    • Function 1 – .replace “Lord” with “Great” in “katskiBakugo” value.
    • Function 2 – .concat ” God Dynamite” after.
  • For line 3 we call out the final output of “katskiBakugoHeroName”.

Our output returns “Great Explosion Murder God Dynamight”, which is indeed his hero name. (Yeah, he’s pretty violent)

Built-In Object References Practical Application

If we were to apply the built-in references, plus a few string functions, this is what the code would look like from top to bottom:

1  public class UAHigh{
2
3      public static void main(String[] args) {
4
5          Byte seat = 18;
6          Short height = 166;
7          Integer popPoll5 = 12373;
8          Long proLicenseNo = 654125412L;
9
10         Float statRating = 104.5F;
11         Double ranking = 0.633;
12
13         Boolean passedFinal = true;
14         Character provisionalLicense = 'P';
15
16         String student = "Izuku Midoriya";
17         String heroName = "Deku";
18         String quirk = new String( "One For All");
19
20         System.out.println("Seat Number: " + seat);
21         System.out.println("Height: " + height + "cm");
22         System.out.println("Pop Poll Votes: " + popPoll5);
23         System.out.println("Provisional Hero License Number: " + proLicenseNo);
24         System.out.println("Status Rating: " + statRating + "/110");
25         System.out.println("Status Ranking: " + ranking);
26         System.out.println("Passed Final Exam: " + passedFinal);
27         System.out.println("Passed Provisional License Exam: " + provisionalLicense);
28         System.out.println("Student Name: " + student);
29         System.out.println("Hero Name: " + heroName.toUpperCase());
30         System.out.println("Quirk: " + quirk);
31         System.out.println("student variable equals to heroName: " + student.equals(heroName));
32     }
33  
34  }

The final output for the code above is as follows (starting from line 20 output call:

20   Seat Number: 18
21   Height: 166cm
22   Pop Poll Votes: 12373
23   Provisional Hero License Number: 654125412
24   Status Rating: 104.5/110
25   Status Ranking: 0.633
26   Passed Final Exam: true
27   Passed Provisional License Exam: P
28   Student Name: Izuku Midoriya
29   Hero Name: DEKU
30   Quirk: One For All
31   student variable equals to heroName: false

For the most part, this would be a 1:1 comparison to primitive numeric data types. However, lines 7 and 14 have the full Integer and Character classes listed, and lines 16-18 have the new “String” class added. In this, we added the “.toUpperCase()” function the value in line 29 and line 31 has the “.equals()” function, creating a boolean.

…and that entire block of code entitled “UAHigh” is actually a class.

Classes Can Have More Than One Object

Just to touch back on what I mentioned earlier when I spoke about object coding anatomy. Classes can have more than one object, however, an object cannot have more than one class.

If I used the entire class file “UAHigh” and used all the class data, I could add another student and call out another output. This would be akin to what I did in my Java Variables post. I can use the exact same reference (variableName), but completely assign it a new object. Since I’ve already declared the object reference (className variableName), I only have to use the variableName for this. Our first student data ends in line 31. Our next student data starts off at line 33.

Now the whole thing looks like this:

1  public class UAHigh {
2
3      public static void main(String[] args) {
4
5          Byte seat = 18;
6          Short height = 166;
7          Integer popPoll5 = 12373;
8          Long proLicenseNo = 654125412L;
9
10         Float statRating = 104.5F;
11         Double ranking = 0.633;
12
13         Boolean passedFinal = true;
14         Character provisionalLicense = 'P';
15 
16         String student = "Izuku Midoriya";
17         String heroName = "Deku";
18         String quirk = new String( "One For All");
19 
20         System.out.println("Seat Number: " + seat);
21         System.out.println("Height: " + height + "cm");
22         System.out.println("Pop Poll Votes: " + popPoll5);
23         System.out.println("Provisional Hero License Number: " + proLicenseNo);
24         System.out.println("Status Rating: " + statRating + "/110");
25         System.out.println("Status Ranking: " + ranking);
26         System.out.println("Passed Final Exam: " + passedFinal);
27         System.out.println("Passed Provisional License Exam: " + provisionalLicense);
28         System.out.println("Student Name: " + student);
29         System.out.println("Hero Name: " + heroName.toUpperCase());
30         System.out.println("Quirk: " + quirk);
31         System.out.println("student variable equals to heroName: " + student.equals(heroName));
32
33         seat = 8;
34         height = 170;
35         popPoll5 = 3374;
36         proLicenseNo = 868748512L;
37 
38         statRating = 69.0F;
39         ranking = 0.688;
40 
41         passedFinal = false;
42         provisionalLicense = 'P';
43 
44         student = "Eijiro Kirishima";
45         heroName = "Red Riot";
46 
47         quirk = new String( "Hardening");
48
49         System.out.println("Seat Number: " + seat);
50         System.out.println("Height: " + height + "cm");
51         System.out.println("Pop Poll Votes: " + popPoll5);
52         System.out.println("Provisional Hero License Number: " + proLicenseNo);
53         System.out.println("Status Rating: " + statRating + "/110");
54         System.out.println("Status Ranking: " + ranking);
55         System.out.println("Passed Final Exam: " + passedFinal);
56         System.out.println("Passed Provisional License Exam: " + provisionalLicense);
57         System.out.println("Student Name: " + student);
58         System.out.println("Hero Name: " + heroName.toUpperCase());
59         System.out.println("Quirk: " + quirk);
60         System.out.println("student variable equals to heroName: " + student.equals(heroName));
61 
62     }
63 
64  }

Because didn’t change the classes for the new set of objects, we were able to just cut and paste the previous output call into lines 49-60. Even though it seems redundant to repeat all the “system.out” calls, for every new object, to establish that in our final work, a system call must be done to observe the established changes.

Below you can see that our process worked.

20  Seat Number: 18
21  Height: 166cm
22  Pop Poll Votes: 12373
23  Provisional Hero License Number: 654125412
24  Status Rating: 104.5/110
25  Status Ranking: 0.633
26  Passed Final Exam: true
27  Passed Provisional License Exam: P
28  Student Name: Izuku Midoriya
29  Hero Name: DEKU
30  Quirk: One For All
31  student variable equals to heroName: false

49  Seat Number: 8
50  Height: 170cm
51  Pop Poll Votes: 3374
52  Provisional Hero License Number: 868748512
53  Status Rating: 69.0/110
54  Status Ranking: 0.688
55  Passed Final Exam: false
56  Passed Provisional License Exam: P
57  Student Name: Eijiro Kirishima
58  Hero Name: RED RIOT
59  Quirk: Hardening
60  student variable equals to heroName: false

Even though we established a new object for the class, we were able to get both outputs without impeding the other.

With the established items, we can continually replace objects using the references as long as the value matches the class we’re using. And as long as we output call each change, the object swaps will be noted in our final work.

Just a reminder: the last object is what the variable will be tied to.

Thanks for reading!

For more information on Object References, please check out the links below:

My Hero Academia References Links Used for this Data Set: