Why is Java different from other programming languages? Why should you learn it? And what are object oriented programming, classes and its other features?

Today we’re going to see the main feature of Java and some basic syntax. For example what objects and classes are.

Therefore, today’s post is all about theory and preparation for further exercises.

Note: this guide is for Java beginners, not for absolute beginners to programming. To get started in this field, check this: Learn C

Main Features

Although C’s and Java’s main concepts are the same, Java has some key features that make it quite different from its predecessor.

Those features are what allow us to decide which programming language to use to make a certain program.

Programmers use C when they need more control over the machine (in particular its memory), but they tend to prefer Java-like languages when they need the following features:

  • Object Oriented

    this is the most important feature and the reason why I choose to start this guide instead of continuing the one on C.

    An object-oriented programming language is a language in which everything is an object, therefore is a lot easier to extend and create complex interactions between different files, data types, functions and so on.

    Later in this post you’ll read what an object is, because it’s the only way to really understand what OOP (object-oriented programming) means.

  • Portable

    portable means that it doesn’t matter on which OS or machine you code your program, it’ll work everywhere (apart from very specific cases).

    A C program made on Windows most certainly won’t work on MacOS, while a Java program will.

    This is made possible by using the Java Virtual Machine (JVM). In fact when a Java program is compiled, it isn’t compiled for a specific compuiter, but into a byte code readable by the JVM.

    What is the JVM? It is a software which translates platform independent Java code into binary code specific for the the machine on which is running. All of this on runtime ( while the program is executed ).

    More on JVM in this article by JavaWorld

    This “translation” is called interpretation.

    To cut it short, when a user has installed the right version of JVM for their computer, they can run every Java program, it doesn’t matter if it was made on Windows, MacOs or by a friend with a Linux machine.

  • Multithreaded

    while you are browsing the Internet on your computer, you can also listen to music or watch a video. All at the same time.

    This is made possible by the abilty of your operative system to manage your computer’s resources in order to run all those programs simultanously.

    Java supports multithreading, thanks to which it can execute multiple tasks at the same time.

  • Simple

    Java is simple to learn, you just need two things: the main concepts of programming – we did these in the previous guide – and the main concepts of object-oriented programming – this guide’s objective.

Objects, Classes, Methods and Instance Variables

Example of objects with cars

Let’s see what these terms mean:

  • Class: a class is a template for a type of objects and it describes its behaviour – all variables it can contain, all functions it can use.
  • Object: an object is an instance of a class. All objects of the same type ( class ) share the same template, but since each of them is a different instance, they may contain different values.
  • Method: a method is simply a function defined in a class.
  • Instance Variables: I said that different objects may contain different values because they are different instances of the same class. But why? Where are those values saved? Classes define the variables that an object contains and its objects contains an independent copy of those variables. Those copies are called instance variables.
    If an object is an instance of a class, instance variables are instances of that class’s variables.

Basic Syntax

There are some rules of syntax to follow when writing a program in Java – many of these are shared with C:

  • Case Sensitivity: uppercase and lowercase are different in Java. HELLO and hello are two different variables.
  • Class Names: the first letter of class names should be in uppercase.

    If they contain more than one word, the first lettere of each of them should be uppercase.

    Not class myclass
    but class MyClass

  • Method Names: all method names shoud start with a lowercase letter.

    The first letter of all other words in the name should be uppercase.

    Not public void MyMethod()
    but public void myMethod()

  • Program Files Name: the name of a program file has to match the name of the class it contains.
  • public static void main(String args[]): this is the starting point of Java programs and it’s mandatory.
  • All names have to start with a letter, currency character or underscore.
  • After the first character, you can use any combination of characters.
  • You can’t use keywords – those words reserved to Java, such as for and while – as names.

List of all Java keywords

abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceof int interface long native
new package private protected public
return short static strictfp super
switch synchronized this throw throws
transient try void volatile while

I took this table from CodeJava


Today you’ve learnt what features Java has and how they make it different from C, so that you can choose the right language for your project.

You’ve learnt what object oriented programming is and what its elements are.

Finally, you’ve also learnt some basic rules of syntax, which will surely save you some time when you’ll try to figure out why your program doesn’t compile.

For some of you this may have been a boring read, for others it may have been interesting. Share your opinion or your questions in the comments.

If you’ve liked this post make sure of sharing it and if you liked it much, remember to subscribe to the newsletter and follow me on Twitter, where I publish all updates about this blog.

Anyway, the post is finished, have a good day and we will see next week!

From Zephyro it’s all, Bye!

Categories: Learn Java

Leave a Reply

%d bloggers like this: