Lecture 12: Reference Types Example
Reference Types problem statement
Declare a 'String' named 'mySignOff' initialized as 'Keep coding'
public class Lecture12 {
public static void main(String[] args) {
// TODO: Declare a 'String' named 'mySignOff' initialized as 'Keep coding'
}
}
Java Reference example solution
String mySignOff = "Keep coding";
Hi there, this is Marius from AlefTav Coding. In this lesson you will learn to declare and initialize a Java Reference type.
A reference holds the value of a storage location SOMEWHERE ELSE. This is the key with a reference. The location where the value is stored, is entirely different from the name of the storage area.
Let me briefly revise Primitives from the last few lectures. With a primitive the storage name and the value it holds are the SAME thing. When you type the declaration, int myNumber, Java sets aside a location of a certain size where only whole numbers can be stored. Java will remember this location by the name, myNumber. Then, when you assign the value, 700 to myNumber, Java stores the number, 700 at this location.
A reference is entirely different. Let me use the reference data type with the name, String. When you type the declaration, String mySignOff, Java sets aside a location which holds the value of an area SOMEWHERE ELSE. Java will remember that the name, mySignOff holds the value of this OTHER location. Then, when you assign mySignOff to the words, Keep coding, Java will store the words, Keep coding at this other location. We say that mySignOff is a reference to the location where Keep coding is being stored.
The example problem for this lecture states that you must declare a String 'named mySignOff' initialized as 'Keep coding'.
In my solution, again, I use the Java pattern, dataType storageName = value. The requirement says the data type must be String, uppercase first letter S. Then, the storage name must be mySignOff. Finally, the initialization or assignment must be the text, Keep coding. These words must appear in quotation marks (" "), because this is how Java knows I am working with text rather than numbers.
public class Lecture12 {
public static void main(String[] args) {
String mySignOff = "Keep coding";
}
}
Now, looking at my complete program. It has a class, Lecture12 with a method, main(). Then the solution code is added, by removing the TODO comment, String mySignOff = "Keep coding";
In this lecture you learned about the tricky concept of a Java reference type.
Now, you can practice what you have just learned by doing this coding exercise below:
Java Reference type coding exercise
Declare a String named ‘words’ initialized as ‘In the beginning’
public class Lecture13 {
public static void main(String[] args) {
// TODO: Declare a String named ‘words’ initialized as ‘In the beginning’
}
}
My name is Marius from AlefTav Coding. Until next time, KEEP CODING.
Please upvote, re-steem and share. For more of this you can find me at the following links.
https://busy.org/@mariusclaassen
https://steemit.com/@mariusclaassen
https://d.tube/#!/c/mariusclaassen
https://www.udemy.com/java-8-for-complete-beginners/learn/v4/overview