Jump to content
kyathil

90 views

Let's start with pretending it's Friday...

Sorry for the lateness of this post, life got in the way! Anywho, let's get into Java!

Java is a well-established (and sort of old) object-oriented programming language and widely used in software. I'm not going to explain in detail what "object-oriented" means, but you can think of it as basing the language around building pieces of code in small units and then assemble them together to create a full program. A little like bricks to build a house. If you're curious, I do recommend googling on the subject to deepen the understanding (it's a bit complex to explain in a blog post).

The reason why I chose Java to start with and not, for example, C is that the goal of my lessons is to teach you how to make simple android apps which happens to be using Java(or Kotlin) for the logic. The idea is that it's easier, and more fun, to learn something if you can at the same time put it in use!

In this post I will be using http://www.browxy.com/ for compiling code since it's such a handy thing to use, but will be using Eclipse in the future too! FYI, you can create an account on browxy, which is free, and that will make it possible for you to save projects and code for future use. Might be good to know! 😄 

Hello World!

We are going to start off with the classical "Hello World!" code base which is what you're usually greeted with if you create a new project in a development environment. So, first navigate to browxy (or any other online compiler) and hit try on "Hello World !", then press start to compile the program. After running the program you will see the text "Hello World" in the Console View.

helloworld1.thumb.png.43f18517c9c616047f713d070ecfc55b.png

What does all of these things mean? I'll do my best to explain the different parts briefly, feel free to comment if you need further explanations! 

public class HelloWorld {

}

public is one of two access level modifiers for classes, the other being package-private (rarely used). This means that the class is visible everywhere, while in the other case it would only be visible in the package the class is included in. A class is a user defined blueprint from which objects are created... Inside the class you will contain methods, variables and eventually constructors (which I will explain in a future post). HelloWorld is the name of our class, class names are always supposed to start with a big letter (note that you cannot use spaces in the names and the general naming convention is to use a big letter for where you would normally include a space: Hello World -> HelloWorld). In Java we are always starting and ending classes, methods and constructors with curly brackets { }. So... In this code example we have a class called HelloWorld which is visible everywhere.

public static void main(String[] args) {

}

Here we have our very first method, called main. A method is a piece of code which will perform an operation in the program. You can create your own methods, use built in methods and import classes with methods from other libraries. In this case it's our own constructed method. Once again we see a access level modifier, public, and methods can have a few more access levels (private - only visible within it's class, protected -visible within it's package). void refers to the return type and in this case the method will not return anything -> void, but in future posts we will be creting methods with other types of return types. Methods are always starting with lower letters and will always contain a parentheses which may or may not contain arguments. In this case we have an argument, String[] args. String is one of the common variable types in Java, will be going through a few further down in this post. String[] means that it's an array of strings, think of it as a list. args is the name of the argument to this method, just like method names we should always start argument names with a lower letter.

The so called main method is not just any method, it's the starting point for your Java application and will always follow the syntax above! You need to have at least one method like this in one of your classes in your Java application! Yes, you can have more than one class and it will be covered later on 🙂

Let's go through the last bit of our very first Java program:

System.out.println("Hello World");

This is a method not created by the user but already existing in the system. What we're doing is basically telling the program to print out "Hello World" to the console. You always need to use ; to call a method and "Hello World" is an argument to this specific method. Yes, you can sometimes use text as an argument to methods (or numbers...). I'm actually not going to explain this further because I think they did an excellent job in explaining everything at https://javapapers.com/core-java/system-out-println/ .

Great!

I will actually not cover anything else in this post, since it's quite a lot to take in, but in the next one I will be going through some standard types, how you can call on methods from other methods, what different types of variables exist and perhaps what the good old if/else case is! Expect another post soon!

h-he-hel-hello-world-omg-his-first-words-skill-3412731.png.cb6f448c9e58b29a2ac9bb6710990606.png



0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...