Reference types coding exercise
Implement a ‘String’ data type, ‘someText’, initialized as ‘I am a student’
Reference types exercise solution
String someText = “I am a student”;
Reference types solution output
I am a student
Hi there, I am Marius from AlefTav Coding. Today, I will again look at Java's Reference data type. The coding exercise homework was to Implement a ‘String’ data type, ‘someText’, initialized as ‘I am a student’.
The solution code is:
String someText = "I am a student";
As always, when making a declaration in Java, you start with the data type, which is String in this case. Then, the storage name as someText. This is assigned to the words, I am a student, between quotation marks, " ".
String is Java's built-in data type for working with text. Also, note that String is a Java class, and therefore the first letter, S must be a capital letter.
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(someText);
}
}
My class, here, is Lecture8 with the method, main(). This is followed by the TODO comment. Then, I do a printout of the value that the storage location, someText points to.
public class Lecture8 {
public static void main(String[] args) {
String someText = "I am a student";
System.out.println(someText);
}
}
With the TODO comment replaced by the solution code, String someText = "I am a student"; I can run my program. The output will be the text, I am a student. (See below)
In this lecture you learnt the solution to the coding exercise, implementing the String reference type in Java. Next time I will show a program where Java asks you a question.
My name is Marius, from AlefTav Coding. Until 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