Blog

Java basics explained

java basics

If you are new to coding, Java basics are for you. For beginner programmers and professional programmers and web developers alike, choosing a new programming language to learn is usually a difficult decision to make, because there seems to be a certain pressure on being fluent in many programming languages.

Learning several programming languages is something that most programmers end up doing. However, it doesn’t need to be all at once, as it usually happens as you go, that is, you face a certain problem, or you want to do something, and you learn a programming language that is well suited for that task.

But sometimes, this decision is more conscious, and we make it in order to get a valuable skill or to level up our possibilities of landing a new job by knowing an in-demand technology, and this is the case when you want to learn the Java programming language.

Why learning Java?

Let’s start with the most common reason to learn Java, and this is that Java is still the most used and most in-demand programming language. Also, the number of jobs requiring knowledge of this language keeps growing.

But Java is not only a good way to land a job, because this programming language is suitable for pretty much anything, and thanks to the Java Virtual Machine it can run pretty much everywhere. There are videogames successfully made in Java, and if you want to make Android apps, Java is the way to go.

In the realm of web development, Java has been one of the most popular languages, due to its robustness and scalability, which makes it highly used in companies for their backend. This makes it a great language to land jobs in web/server-side development.

Start learning Java basics now

Learning Java is exciting, because it has a very big community, it’s a more than solid language, and it uses Object Oriented Programming at its core, which is a very important programming paradigm you’ll eventually want to learn, and Java is a very good way to understand it.

The first thing you’ll want to do is pick a free Java IDE (Integrated Development Environment). There are a few options, but let’s use IntelliJ IDEA Community Version, which is free, complete and modern (good-looking UI also matters).

While installing IntelliJ, let’s download the JDK (Java Development Kit), which allows us to write programs with the .java file extension. Currently, the stable version is JDK 12, and you’d want to use this one.

Now, let’s create a new Java project on IntelliJ, in the list of project SDK, if it’s empty, click new and add the path of Java in your computer (where you unzipped or installed the JDK). Next, we want to create a project from scratch, without using templates or libraries. Name it something like HelloWorld.

In the project, in the src folder, let’s create a new class by right-clicking and going to new. When using java programming, there’s a convention of creating packages, and they have an at-first-particular naming convention. In the Java Class name, we’ll say something like: com.example.helloworld.HelloWorld.

Let’s dissect this:

  • com: A convention, good to follow it.
  • example: The name of the company. Of course, we don’t need to have a company to program in Java. Simply use a name that encompasses the whole program. If your program is a Tic-Tac-Toe game, then we’d say tictactoe.
  • helloworld: The name of the package. We can see packages as groups of classes that make sense together. If making a calculator, we might want a package for math operations, so we could say calculator.operations.
  • HelloWorld: The name of the class, in this case, accurately called HelloWorld. If making a Pac-Man clone, this might be pacman.logic.ghost.

Finally, let’s write some code and dissect it as well.

By default, IntelliJ will output something like:

 

package com.example.helloworld;

public class HelloWorld {

}

 

We have the route of our package without the class name, which is right below.

But, depending on how familiar you are with programming and Object Oriented Programming you might wonder what does public class HelloWorld mean, anyway?

Knowing what everything is, is hugely important. However, to get started with Java, we can start by doing certain things out of convention, and eventually learn more in depth what each of these keywords does.

Public is an access modifier, and in Java there are two more (private and protected). We use Public in classes and methods. Public means that other classes of the package can access this HelloWorld class. If you don’t add Public, and just say class HelloWorld, the class will be public by default.

The next keyword tells Java that we are making a class, and the next is a name of your own for the class. For classes, the convention is using PascalCase, that is, no spaces, dashes or underscores, and capitalizing the first letter of each word.

Let’s now complete this example:

package com.example.helloworld;

 

public class HelloWorld {

public static void main(String[] args) {

System.out.println(“Meow, world!”);

}

}

 

Now we added some code into that HelloWorld class. This is called a method, that is, a function inside of a class. This one, main, is especially verbose. Don’t worry, usually only one main method is expected, and it’s written this way by design. Let’s dissect it:

  • public: As classes does, methods also use access modifiers. The main method is always public.
  • static: We haven’t learnt about instantiation, but this means that this method can be used from other classes without instantiating this classes. The main method is always static.
  • void: The return type of the method. This method doesn’t return anything, so it is set to void.
  • main: The name of the method. For main, the Java compiler will always expect it to be called that way.
  • (String[] args): The arguments of the method. String[] means that this is an array (a collection) of strings, and args is the name of the argument. This is also by default for the main method. In a method of our own, we would use something else, or nothing, depending on what we need.

Great! Remember that such – a bit scary – way for writing that method is just a design decision of the language creators, and the Java Compiler simply wants to see that somewhere in the program. Usually, it’ll go in a class called Main.java, but it’s not compulsory.

Finally, we can print our important message to the world! And we do so with this line: System.out.println(“Meow, world!”); But, what does that mean?

Compared to other languages such as JavaScript, where the function for printing is console.log(), or Python, where it is print(); this might look a bit intimidating. Fear not, this is just the way it was made, and it is not so terrible.

If we get rid of the System.out part, we get to println, which is actually the name of the printing function  in some programming languages. Now we can see System.out as the origin of println, the classes where this method we are using comes from.

 

Once we’ve overcome this first sometimes not very friendly encounter with Java, we are free to explore a really powerful, popular, in demand and useful programming language. With it, we can practice OOP, learn about how to organize our programs, and make a lot of things! You might want to explore the Swing and JavaFX GUI libraries for making cross-platform desktop programs with Java!

Our locations