Quick start with Java: from the setup to the first application
As you know, Java is one of the most popular programming languages in the world and its knowledge will significantly increase your importance as a programmer. So you decided to start writing in this language. You will need to install the JDK in order to write and run Java programs. JDK is a set of software developed by Oracle, containing a compiler (javac), a runtime environment (Java Runtime Environment), a standard language library, examples and documentation.
After reading this article, you will learn how to install and configure the JDK on your system, what the development environment is and what IDE options exist for Java. You will also write your first Java program.
Install Java Development Kit
-
Go to the Oracle webpage and download the JDK for your stage.
-
After downloading, extract the resulting archive and run the extracted application.
-
During the installation process, select the “Development Tool” option and click “Next”.
-
After some time the installation is complete.
-
So, you installed the Java Development Kit, but that’s not all. You need to configure it for your system.
JDK configuration using Windows as an example
-
Navigate to the% ProgramFiles% \ Java \% folder of the jdk% \ bin installed by you, click on the properties of any file in this folder and copy the path to it.
-
Go to the properties of your computer, open the Advanced tab, click “Environment Variables …”. In the window that opens, create a new variable, name it Path, paste the path that you copied earlier into its value.
-
Now the most important. Open a command prompt and type javac to verify that the JRE is installed and configured. If you give a list of arguments to the javac command, then congratulations, you have successfully installed and configured everything you need to use Java!
Once you have installed both JDK and JRE, you need to install one of the IDEs.
IDE installation
To begin with, we should take a gander at what an IDE is.
IDE (Integrated Development Environment) is a lot of programming devices utilized by developers to create programming. IDE makes it simple to compose, run, troubleshoot, and test code.
For composing complex projects it is fitting to utilize IDE. We consider the most prominent.
NotePad
Yes, and you can write code in notepad! For development, you just need to install the JDK and specify the path to it. Write code in a notebook, compile using the command line. However, for the development of complex programs is not the best option due to the lack of any additional features that are present in the advanced IDE.
Netbeans
NetBeans is a selection of professional Java developers. It has unique features and tools that will allow you to make your program cross-platform, and the code readable. NetBeans not only supports Java, but also other programming languages for desktop and web development. It is completely free, you can download it from the official site. Here are just some of its features:
-
code formatting;
-
installation of third-party libraries;
-
simple graphical interface;
-
and many many others…
Eclipse
Eclipse, like Netbeans, is one of the most popular IDEs. It provides an impressive, intuitive interface and a productive development environment that allows you to comfortably develop Java applications. Download Eclipse for free from the official site. Benefits:
-
the ability to format the code as you like;
-
support for breaking code into modules;
-
ease of using the same code in different projects;
-
drag and drop;
-
view library contents;
-
convenient interface.
IntelliJ IDEA
IntelliJ IDEA is a well-known IDE for Java, written, oddly enough, in Java. It is equipped with unique tools and allows you to easily navigate the program. Finding bugs and debugging code has never been easier than with IntelliJ IDEA.
Jcreator
JCreator is the most advanced and fastest Java IDE written in C++.
Creating our first application
So, you have installed and configured JDK, JRE and IDE for Java. What is the next step? Of course, write a program to finally make sure that everything works and you are ready to learn a language. You will get acquainted with the basic structure of Java code and create your first program! It should be noted that before learning Java you should familiarize yourself with at least the simplest principles of object-oriented programming.
The source code file contains several classes – these are parts of the program that have certain functions. A good practice is to split the program into several files with source code, each with its own purpose. Classes contain methods – actions that can be performed by objects of this class. The method contains commands with which you can get the desired result.
Before you start creating a program, you need to create a project, and in it a file that will contain your code. Consider creating a project on the Eclipse IDE, but in the other IDEs the process is not much different. From the top, select “File”, then move the cursor to “New”, in the opened menu select “Java Project”. In the window that appears, enter the name of the project and other settings you need (if you are not sure what to do, then you can just leave everything as it is) and click “Next”.
Done, you’ve created a project! It remains only to create a class in it in which you will write your first program. Right-click on your project (it should appear on the right) and select “New” → “Class”. Give the new class a name (in this example it is first) and click “Finish”.
Let’s start writing your first application. By tradition, this is a program that displays “Hello, world!”.
public class first{
public static void main(String args[])
{
System.out.println("Hello, world!");
}
}
Give us a chance to examine parts the mentioned above code block:
-
public is an access modifier that determines which part of the program our class can be used from. In your example, public is all the code;
-
class is a keyword indicating that you are declaring a class and not anything else;
-
first is the name of your class. Parentheses define the beginning and end of the class code;
-
public static void main (String args []) – declaration of a public static method (that is, which can be called without creating a class object). The method in the example returns nothing and takes an array of strings as arguments. The only command in this method writes the message “Hello, world!” To the console. Notice that instead of println, you can write print, the only difference is that in the case of println, the line break character will also be displayed.
Well, you created your first application. Now it must be run. To do this, simply click the white arrow in the green circle on the top panel (when you hover the cursor on it, “Run” should be highlighted). After clicking below the console will open, in which you will see the message “Hello, world!” Congratulations, you wrote your first Java program and are ready to dive into the fascinating world of this programming language!