Reference types problem statement
Implement a ‘String’ data type, ‘myBusiness’, initialized as ‘AlefTav Coding’
Reference types solution code
String myBusiness = "AlefTav Coding";
Reference types solution output
AlefTav Coding
Lecture starting code
public class Lecture7 {
public static void main(String[] args) {
// TODO: Implement a ‘String’ data type, ‘myBusiness’, initialized as ‘AlefTav Coding’
System.out.println(myBusiness);
}
}
Hi there, I am Marius from AlefTav Coding. In this lecture I will take a look at an example of Java's Reference data type. You will learn how to 'Implement a ‘String’ data type, ‘myBusiness’, initialized as ‘AlefTav Coding’ '
In Java the sizes of primitive types are known at the time when those types are declared. However, with reference data types, the size of those locations are not known. Therefore, a reference is a computer memory address which holds the value of a storage location somewhere else in memory. In other words, the location where the value is actually stored, is entirely different from the location of the storage name.
String myBusiness = "AlefTav Coding";
The reference type of today's lesson is the data type named, String. When I type the declaration, String myBusiness, Java sets aside a location which points to the actual value. This value is stored at some other part in the computer's memory. Java will remember that the name, myBusiness, holds the value of this other memory location.
Then, when I assign myBusiness to the words, AlefTav Coding, Java will store the words, AlefTav Coding at this other location. We say that the text, myBusiness is a reference to the location where the words, AlefTav Coding is stored.
In my class, Lecture7 there is the method, main(), the starting point of a Java program. Then follows, a TODO comment, and I do a printout of the value pointed to by myBusiness.
Replacing the solution code with the TODO statement my complete program looks as follows:
public class Lecture7 {
public static void main(String[] args) {
String myBusiness = "AlefTav Coding";
System.out.println(myBusiness);
}
}
When my code is correct code I can run the program. The output of this program is the text, AlefTav Coding. See below.
AlefTav Coding
Many beginners, at first, have difficulty understanding the concept of a reference. It is important that you understand it well, because most of Java is about references.
Coding exercise
In this lecture you learnt to implement a reference in Java. Now, onto this lecture's coding exercise for you to practise implementing references. Here is the homework instruction:
public class Lecture8 {
public static void main(String[] args) {
// TODO: Implement a ‘String’ data type, ‘someText’, initialized as ‘I am a student’
System.out.println(myBusiness);
}
}
My name is Marius from AlefTav Coding. Till next time, KEEP CODING.
If you want to earn tokens on EVERY post you create use the referral URL below:
This post was made from https://ulogs.org