← All posts

Java. Our First Program

java

A continuation of the article “Java. Installing the Development Environment and Eclipse IDE”, in which we will write our first program in Java.

In Java, object references are used to work with objects. Objects are stored on the heap (a general-purpose memory pool, slower than the stack), while data of primitive types is stored on the stack (a data storage area in RAM where data is accessed via the stack pointer).


String x = "test";

x stores a reference to a String object with the value “test”.

Objects are removed from the heap automatically by the garbage collector, so there is no need to do this explicitly.

Data Types

Java also has primitive data types: boolean, char, byte, short, int, float, long, double, and void. Data of these types is stored on the stack. All numeric values are signed!

The other data types are classes (String, Character, etc.), and objects created from them are stored on the heap.

Classes and Class Members

A class name should begin with an uppercase letter. Method and property (class variable) names should begin with a lowercase letter, except for the class constructor, which has the same name as the class.

UPDATE: Everything said above concerns style; no one will stop you from starting a class name with a capital letter…


public class TestClass
{
    public static void main(String[] args)
    {
        testMethod();
    }

    public static void testMethod ()
    {
        System.out.print("Date: " + new Date());
    }

    int x = 1;
    double y;
}

public class TestClass2
{
    public TestClass2() {
        // Конструктор
    }
}

The static method main runs first; it is the so-called entry point of the program.

A constructor never returns anything! It is needed to initialize an object.

In Java, it is customary to access static class members like this:

Class.function() и Class.var

Non-static class members, however, cannot be accessed by the class name; they can only be accessed through an object:


Class var = new Class();
Class.method(); // не доступно
var.method(); // доступно

Encapsulation

Data hiding is accomplished using three keywords:

  1. public - the class member is accessible to everyone
  2. private - only members of the class have access to the member
  3. protected - both members of the class and descendants of that class have access (see inheritance)

An Example of Our Program

Since we need to include packages (class libraries) in order to work, for example when working with dates, we need to use the import keyword.


import java.util.*;

/**
 * Класс вывода текущей даты
 * @author adw0rd
 */
public class JavaTest
{

    /**
     * @param args
     * @return Date Stamp
     */
    public static void main(String[] args)
    {
        System.out.print("Date: " + new Date());
    }

}

In Eclipse, press Ctrl+F11 (or Run->Run) to run the program; the current date and time will appear in the Console window. When you save the file, Eclipse will compile it again, provided there are no errors in the code.

You can also compile it using javac.exe and run it using java.exe. It is up to you, but I find it more convenient to work in an IDE.

Comments

Two types of comments are available in Java:

// однострочный

/*
многострочный
*/

Also, to document classes and class members, you should use the Javadoc format. Many commands are available; here are some of them:


/**
 * @author ИмяАвтора (И дополнительная информация, например e-mail)
 * @see ИмяКласса (Ссылка на другие классы)
 * @version Версия
 * @param Входные параметры метода
 * @return Описание того, что возвращает метод
 */

That is all for today. Next time, we will look at basic and conditional operators, as well as loops in Java.

Comments are closed. Read the original discussion in Russian →