Programming Basics 1 – MyFirstProgram

Introduction

This article is the intro to a series intended to teach programming in an easy and fun way. The examples in this series might be written in different programming languages, because I’m not trying to teach you a particular language, but the basics of programming.

This series is for people who have absolutely no experience in programming, so there is nothing needed to start this series. Please don’t get me wrong: You won’t be able to program a new AAA game after this tutorial series, but you’ll be able to write simple programs and games. But be warned: You’ll need to practice A LOT if you want to develop something larger. I might however make another series later with advanced programming concepts. Anyways remember one thing: even if it is a lot of work to learn the concept of programming, it will be a lot of fun after you understood it.

Something theoretical before we start

Before you start programming a new project, you should think about what programming language to use. This strongly depends on the requirements you have to fulfill. There are languages which are faster and there are languages which have more default libraries you can use. However you can say that there are some programming paradigms you have to consider. As this is an article about the very basics I won’t discuss them here, I just want to tell you that we’ll use object oriented and mixed-types int this article. However it’s not bad if you know that there is something like programming paradigms. If you wish to read about programming paradigms in detail, you can do so here.

Preparation

After you thought about what language to use, you’ll need to get either just a compiler for this language or an IDE. I’ll use an IDE for this series, because it simply is the state of the art today. However you can use the compiler only on let’s say headless systems or cross-compile if you wish to, but I’ll stick to IntelliJ IDEA for this series.

You can download it here.

However if you want a more light-weight IDE, you could use BlueJ for Java.

Let’s get started

I’ll start with a very simple example that just prints out something into the console. We’ll discuss the code snippet later. When you first start IntelliJ IDEA, you’ll be welcomed with this start-up screen:

Bildschirmfoto 2016-08-23 um 13.27.48
Figure 1: Start-up screen of IntelliJ

In this screen simply select ‘Create new project’:

Bildschirmfoto 2016-08-23 um 13.30.09
Figure 2: Create a new project

Just select ‘Java’ and no further libraries or frameworks. Click on “next” twice. You’ll be asked for a project name and a location:

Bildschirmfoto 2016-08-23 um 13.33.45
Figure 3

After you entered all the details, the code-editor loads up and you can start programming (finally)! Just set up a new class. If you don’t know how to do that, here is a short video that explains you how to create a new class:

After you have created your first class, copy and paste this code into the code editor:

public class MyFirstClass
{
    private static String yourName = "nerdhut";

    public static void main(String[] args)
    {
        System.out.println("Hello " + yourName);
    }
}

Now you are ready to run the code. To do so, simply right click on the freshly created class in the file browser on the left hand side of the screen and select ‘Run MyFirstClass.main()’:

After you have executed the program for the first time, you can quickly execute it by clicking on the green ‘play’ button on the upper right corner of the screen.

In your console (which popped up after you started the program) you can see the words ‘Hello nerdhut’. Now try to play around with the code to make it print your name onto the console.

What’s the code about?

If you are really new to programming, this code might mean nothing to you when you read it. But it’s not very complicated. Basically it’s the simplest lines of code I could think of to demonstrate the very basic constructs of a program:

Classes
The first line (public class MyFirstClass) defines a class (well obviously). A class in object oriented languages is a place to group methods and variables together. We’ll cover classes and objects in a later part of the series, so for now you can think of it as a drawer where you put everything in that belongs together.

Variables
If we now go on to the next line (private static String yourName = “nerdhut”;) you can see the declaration and initialization of a variable. Variables are spaces in memory that you can assign a value to. Because it’s hard to remember memory addresses, you assign a name to them. Like the classes, we’ll discuss this topic in more detail in a later part of the series. You can think of variables the same way you’d in maths. If you write:

x = 20

You made a new variable with a value of 20. You can play around with the yourName string’s value to change what the application will output to the console.

Methods
Now go and look at the next line of code. You’ll see the so called main method. This is a special method that is the main entry point of an application. The program’s execution will start here. You have to define it in order to make anything in your program. Of course you can create your own methods with a unique name:

public static void myUniqueMethod() { /* Code */ }

In the main method we call a pre-defined method that comes with java:

System.out.println("Hello " + yourName);

This call will print out the text given to it to the console.

Like variables and classes, we’ll discuss this topic in another part of the series. For now you can think of methods like closed set of instructions, that can be called from the outside. This is very handy to keep your code organized. Also you don’t have to know about the method’s internals. You just call a method and give it it’s parameters (like we did with the text when calling the println method). The method will then do everything else for you.

Exercise

The best way to learn programming is by writing a program. So try to write your own method that takes a text as parameter and prints this text. Then call your custom method in the main method, instead of calling the println-method directly! The solution to this exercise will be presented in the next part of the series.

Table of contents

Part 1 – MyFirstProgram (You are here)
Part 2 – Access Modifiers and Variables
Part 3 – Methods
Part 4 – Arrays
Part 5 – Control flow
Part 6 – Classes and Objects
Part 7 – Exceptions and exception handling

comment-banner

Advertisements

13 thoughts on “Programming Basics 1 – MyFirstProgram

  1. Howdy! I coսld have sworn I’ve been to tҺis website Ьefore but after browsing through
    many of thᥱ articles І resalized it’s nnew tto me.
    Nonetһeless, I’mdefinitely happү I dikscovered іt aand I’ll be book-marking іt aand checking Ьack regularly!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s