The Java language
Java is not just a programming language. It's an entire software platform with extensive capabilities. The main components of the platform are:- Basic tools for writing and running Java programs.
- Libraries and classes (the core of the language). They provide Java's basic programming capabilities: exception handling, multithreading, collections, logging, reflection, security, networking, XML processing, serialization, regular expressions.
- Tools for deploying and automatically launching applications.
- Tools for creating a frontend (GUI, user interface). These are found in the classes of the JavaFX, Swing, and Java2D libraries.
- Libraries for working with databases remotely over the network, such as JDBC, JNDI, RMI and Java RMI-IIOP.
Low barrier to entry.
Learning Java is easier than most languages with a C-like syntax.Object orientation.
Programs in Java are built based on objects and interactions between objects. This lets you enjoy all the advantages of OOP.Portability.
Because an interpreter (the Java virtual machine) is used, programs can be run on various platforms and devices.Platform independence
A Java program written for one platform is compiled into intermediate byte code that can be run on other platforms, because it is interpreted by a JVM for each specific platform.Advanced multithreading.
Java tools let you control the execution of multiple threads, which means you can create multithreaded applications.Security.
Because the JVM has built-in bytecode verification, and Java has no manual memory management, tracks stack overflows, and has various APIs that let you control security, you can create really safe applications in Java.Fault tolerance.
The exception mechanism increases programs' fault tolerance and reduces the number of errors, both at compile time and run time.Interpretability.
The Java interpreter can execute Java bytecode on any machine that has a JVM and JRE.Distributability.
Java has tools for creating distributed applications.Performance.
A JIT (just-in-time) compiler provides high speed performance comparable to C and C++.
How to start programming in Java?
To start learning Java from scratch, you should dig into some basic concepts: what's included in the Java language, what's a Java program, and how does it run? Then move on to the language's syntax and basics, and study libraries. After reading a couple of articles about Java, you can tackle the basics. The following flowchart clearly demonstrates the sequence of steps:What do you need to program in Java?
First, you need to install software for developing and running programs — the Java Development Kit (JDK). After that, configure the JDK on your computer, download and install an integrated development environment (IDE), which is an environment for software development. The most popular IDE is IntelliJ IDEA. Alternatives are Eclipse, NetBeans, JCreator, and even an ordinary text editor.
Installing Java on your computer
As we've already seen, when we learn Java from scratch, the first step is to install the JDK. For this, you need to perform a few simple operations:- Go to the Oracle website.
- Select and download the installation file for your operating system.
- Perform the installation, following the installer's recommendations.
- Set an environment variable if you're using Windows.
Basic definitions
If you're just starting to learn Java, you will certainly encounter the following terms: JVM stands for Java virtual machine. This is a platform-dependent software module that serves to interpret the source bytecode into machine code and executes it. JRE stands for Java Runtime Environment. It includes the JVM implementation for a specific platform and a set of libraries needed to run Java programs. JDK stands for Java Development Kit, which is a set of developer tools needed to write Java programs. It includes a compiler, JRE, standard Java libraries, documentation, and various utilities. Source code is found in a text file written in the Java language with the .java extension. Bytecode is machine-independent low-level code that consists of a set of instructions for the JVM. Machine code is binary machine instructions that are executed directly by the processor. Compile means to convert source code to bytecode. Interpret means to convert bytecode to machine code. A platform is a software and hardware environment for running programs. The most popular platforms are Microsoft Windows, Linux, Solaris OS and Mac OS. This diagram will help you better understand the concepts of JVM, JRE and JDK:Program lifecycle
A Java program's life begins when source code is written in a text file. Usually, this is done in a special programming environment called an integrated development environment (IDE), but simple programs can be typed into a text editor, even Notepad, which comes with any edition of Windows. The source code must be saved in a file with the .java extension. Example program: HelloWorld.java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("This is my first program");
}
}
Before this source code is executed, it must be compiled it into bytecode by a compiler. A compiler is a utility that is part of the JDK. It produces a file with the .class extension. This file contains bytecode, which are instructions for the JVM. Their format resembles assembly language. Our HelloWorld.java program will be compiled into a HelloWorld.class file. The Java platform does not provide tools for editing bytecode, but you can view it. To view the bytecode of a Java program, you can use the javap disassembler utility, which is included in the JDK. HelloWorld.class will contain the following bytecode:
Compiled from "HelloWorld.java"
public class HelloWorld {
public HelloWorld();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #2 // Fieldjava/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String This is my first program
5:invokevirtual #4// Methodjava/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
Now our program is stored in a compiled form in the HelloWorld.class file. To run it on any platform, the JRE must be installed. The JVM provides the ability to port Java programs to any platform.
Execution means execution of the bytecode by the Java virtual machine. Programs are executed using the java utility. You need to specify the name of the compiled file. Execution happens as follows:
- The JVM is loaded into the computer's memory. Basically, this is a program that serves to run the Java programs we write.
- Using the bootstrap classloader, the JVM loads and initializes our class in memory. In our example, this is the HelloWorld class.
- Next, the JVM looks for a public static void main(String []) method in our class.
- The code of the main method is executed. If execution of the program requires other classes, they are loaded and initialized.
- After the code is executed, garbage collection is performed. This involves clearing memory and closing the JVM program.
Choosing and installing a development environment
To quickly and efficiently program in Java, you need a development environment — an application for writing Java programs. Among Java developers, the most popular IDEs are:- IntelliJ IDEA
- Eclipse
- NetBeans
How long does it take to learn Java?
You can probably learn the basics of Java and develop programming skills in 6 to 12 months, depending on how intensely you study. Take a structured approach: make a study plan, gather the necessary sources, and set aside a few hours a day for your studies. Don't forget that the key to learning how to program is practice.Conclusion
Studying Java on your own is actually easier than you might think. You only need basic computer skills. To start learning Java effectively, follow a few simple steps:- Install Java on your computer
- Learn the basic concepts
- Install a development environment
- Write and run your first program
More reading: |
---|
GO TO FULL VERSION