Lecture 4 – First Coding Solution

First Coding Exercise

Write a statement to print the text, ‘Hi there’ in Java.

First solution code

System.out.println(“Hi there”);

First solution output

Hi there

Hi there, This is Marius from AlefTav Coding.  In today’s lecture you will learn the solution to the first coding exercise.

public class Lecture4 {
    public static void main(String[] args) {
        // TODO: Write a statement to print the text, ‘Hi there’
    }
}

The homework from the previous tutorial was,  Write a statement to print the text, ‘Hi there’ in Java.  I gave you starting code with the class as Lecture4, the main() method, and a TODO comment. You were asked to write your solution code at the line where the TODO comment appears.

Solution code

System.out.println(“Hi there”);

In Java you use the statement, println() to check if your code works correctly.  You read this as, print line or print lin.  What you do here is to tell Java to display the specified text and then move the cursor to the beginning of the next line.  The format of this statement is, System.out.println(). The word, System is a Java class.  Therefore it starts with a capital letter, S.  The words, out and println() are all small letters.

The word, println() is a Java method.  So, it must be followed by two round brackets, ( ). Inside these brackets is where you type the text that must be displayed.  Also, the text to be displayed must appear between a set of quotation marks, ” “.  Finally, to end a Java statement you add a semi-colon, ; .

When this code is executed, the words, Hi, there are displayed on your computer screen. Please note that your code needs to be EXACTLY like my solution below for it to work.

The output will be as follows:

In this lecture you learnt the solution to the first coding exercise. Next time I will look at Primitive data types in Java.

I am Marius from AlefTav Coding.  Until next time, KEEP CODING.

If you want to earn free cryptocurrency (around USD 10,000), use the following referral URL (available only for the next 3 days):

https://initiativeq.com/invite/BfvINpq8X 

2 votes, average: 4.00 out of 52 votes, average: 4.00 out of 52 votes, average: 4.00 out of 52 votes, average: 4.00 out of 52 votes, average: 4.00 out of 5 (2 votes, average: 4.00 out of 5)
You need to be a registered member to rate this.
(11 total tokens earned)
Loading...

Responses