Java tutorial

<< Prev
Next >>

Introduction

Java is an object-oriented programming language. The great advantage of java programs is that, once compiled, the program can run on any platform that has a Java virtual machine (JVM) installed.


The java programs work on millions of devices: laptops, tablets, phones, under Windows, Linux, Solaris or iOS.
A java platform contains the virtual machine (JVM), a compiler and a set of libraries for application development. Java is available in 2 versions:


- Java Runtime Environment (JRE) - which contains the virtual machine in which the programs run.
- JDK (Java Development Kit), which besides the virtual machine also contains the compiler and other tools for creating and executing programs and applications.


Schematically, the two java distributions are represented below:


JDK

Installing JDK in windows

After downloading the JDK installation file, we will start installing it:


JDK Install

Press the Next button to continue the installation:


JDK Install

Follow the instructions and end by clicking the Close button.


JDK Install

JDK Install

JDK Install

Dupa instalare, directorul jdk va avea urmatoarea structura:


JDK Install

The bin directory contains the compiler (javac.exe) and the execution program (java.exe).


JDK Install

If we want to compile or run programs or applications from any directory, without writing the entire directory path in the command, we need to set the PATH environment variable.
If we do not set the PATH variable, we'll need to specify for each command the complete path to our file, for example:


C:\Program Files\Java\jdk10.0.2\bin\javac Hello.java


Set PATH variable in Windows 10

Open Computer Properties (right click on the computer icon). The window opens:


Java Path

Click Advanced system settings:


Java Path

Then click on Environment Variables:


Java Path

Go to the User variables section and click New. A small window opens. In this window, enter "Java_Home" for the variable name and the address of the JDK directory (path to it) for the value of the variable (for example: "C:\Program Files\Java\jdk-10.2.0\bin").
Then OK in all open windows.
To check if everything is OK, open the command prompt, type "java" and enter Enter. There will be something like this in the command prompt:


Java Path

Our first Java program

We write the program in the Notepad editor and save the file as "Hello.java".


Hello

To compile the program, we use the javac command. We open Command Prompt and type:


javac Hello.java


Hello

Following compilation, the compiled "Hello.class" file is generated. Now we will run this file using the "java" command as follows:


java Hello


Hello

It is noticed that following the execution of the program, the text was displayed: "Hello!".


Run java programs more simple

If we do not want to set the PATH variable, we can install Textpad or Eclipse, which are free applications.
After installing the Textpad editor, we write the Hello.java program:


Textpad

To compile the program, select: Tools -> External Tools -> Compile Java:


Textpad

Will result the compiled file: "Hello.class". To run the program, select: Tools -> External Tools -> Run Java Application:


Textpad

The program runs and the result is shown in the following figure:


Textpad

A more advanced professional program for programming in several languages, including Java, is Eclipse.


Eclipse

Java packages

A Java package organizes Java classes into namespaces. A package can contain the following kinds of types: classes, interfaces, enumerations and annotation types.
Core package in Java:


java.lang - basic language functionality and fundamental types

java.util - collection data structure classes

java.io - file operations

java.math - multiprecision arithmetics

java.nio - the Non-blocking I/O framework for Java

java.net - networking operations, sockets, DNS lookups

java.security - key generation, encryption and decryption

java.sql - Java Database Connectivity (JDBC) to access databases

java.awt - basic hierarchy of packages for native GUI components

java.text - provides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages

java.rmi - provides the RMI package

java.time - the main API for dates, times, instants, and durations

java.beans - the java.beans package contains classes and interfaces related to JavaBeans components

To use a package's classes inside a Java source file, it is convenient to import the classes from the package with an import declaration:


import java.awt.event*;


Working on examples:


Java create directory:

Java dir

Java create file:

Java file

Java delete file:

Java delete

Java reading from text file:

Java read

Java write to text file:

Java write

Java JFrame:

JFrame

Java JLabel and JButton:

JLabel
<< Prev
Next >>

Visit my websites:

https://www.jwebsaints.com
https://www.jwebplants.com