Introduction to Java language

In this tutorial we will learn how to program in the Java language. Java is a object oriented language developed by Sun Microsystems. The main idea behind Java is portability. I mean you can write a Java program in your x86 computer, and port easily to other platforms like a cell phone, or a microwave!

How does it works

In order to reach this high portability level, Java is based compile it's code into some intermediate instruction set called "bytecode" those instructions are interpreted by a virtual machine. This virtual machine interacts with the low level hardware (microwave). So if you write your program in your x86 and the microwave has a compatible Java Virtual machine, you only need to deliver this bytecode in the microwave, or other platform.

This intermediate code is our "MyProgram.class" in the picture above.

Building our Hello World program!

Let's see in this video how to create a simple project in NetBeans.

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Language Basics

Variables and Datatypes

Variables are named memory locations used to store data. Variables has a name and a datatype wich defines the type of values that this variable will store. Before use all variables must be declared:

Datatype <variable_name>;

int var_age;

String var_name;

Datatypes

As we said every variable must have a datatype, a datatype can be the following:

Storing values in variables

In order to store a value in some variable you use the operator "="

var_name = "Leonardo Araujo";

var_age = 18;

Comments

Comments are very usefull for code documentation. For exemple if you write a routine a long time ago and want to remember what it does:

// Very simple just sum two numbers a and b    
/*    
  Multiple (With very long description)
  lines comment
*/ 
int VeryDificultRoutine(int a, int b);

Implementing in Java a simple flowchart

Let's learn a little bit more dificult sample based in this flowchart.

 

What we're going to learn

In this tutorial we're going to learn the following things: