Hello friends! Today I want to explain to you why do you need java technology on a computer and give clear examples of its use by web application developers, we will also learn how to install the Java platform on Windows 8, if you don’t know one secret, then you won’t install Java on eight. To make it clear to you what we are talking about, I am publishing these three letters that came to us in the mail.

Why do you need Java?

Friends, undoubtedly you need Java technology, but only the latest version and downloaded from the official website www.java.com/ru. Why? I'll show you a simple example and explain everything.

What is Java technology?

Java is object-oriented programming language developed by Sun Microsystems in 1995. A huge number of websites, games and various applications contain various elements developed using Java.

For example, you have installed the operating system and decided to install the driver on your video card, to do this, go to the video card website, for example http://www.nvidia.ru, click Drivers. We don’t want to search for our drivers manually and choose

Option 2: Automatically find NVIDIA drivers and press the button Graphics drivers

And we get this answer:

NVIDIA Smart Scan requires the latest version of Java software.Click on the "Java" icon to install.

If you click on the Java button, you will be redirected to the site www.java.com/ru and to install the Java platform you just need to click on the button Download Java for free.

The same situation can happen with many online games, if you want to play this online game,

The game will immediately redirect you to the Java platform installation page.

Here, friends, is a simple example of use. object-oriented language Java programming on the website of your video card and Online Games, if we do not install the Java platform on our operating system or have outdated version Java, then we won’t be able to automatically select drivers for the video card and won’t be able to play. There are many such examples that can be given. I think that I answered your question why you need Java.

Yes, just recently virus writers found a vulnerability in updates to the Java platform and many users were affected; to the credit of Java developer Sun Microsystems, a patch was released very quickly and to this day this has not happened again. By the way, such a story can happen with any application.

How to check if it is installed on your computer latest version Java platforms

Go to the website www.java.com/ru and click on the button Do I have Java software installed on my computer??

Check your Java version.

Eloquently. Java is not installed at all.

Let's install Java. Let's go back to home page www.java.com/ru and click

Java is a programming language developed by SunMicrosystems. Java applications are typically compiled to custom bytecode so they can run on any Java virtual machine (JVM), regardless of computer architecture. Official release date: May 23, 1995. Today, Java technology provides tools for turning static Web pages into interactive, dynamic documents and for creating distributed, platform-independent applications.

Java programs are translated into bytecode, which is executed by the Java Virtual Machine (JVM), a program that processes the bytecode and passes instructions to the hardware as an interpreter.

The advantage of this method of executing programs is the complete independence of the bytecode from the operating system and hardware, which allows you to run Java applications on any device for which there is a corresponding virtual machine. Another important feature of Java technology is flexible system security due to the fact that program execution is completely controlled by the virtual machine. Any operation that exceeds the program's established permissions (for example, an attempt to unauthorizedly access data or connect to another computer) causes an immediate interruption.

Often, the disadvantages of the virtual machine concept include the fact that the execution of bytecode by the virtual machine can reduce the performance of the program of algorithms implemented in the Java language. Recently, a number of improvements have been made that have slightly increased the execution speed of Java programs:

Application of technology for translating bytecode into machine code directly while the program is running (JIT technology) with the ability to save class versions in machine code,

Widespread use of platform-oriented code (native code) in standard libraries,

Hardware that provides accelerated bytecode processing (for example, Jazelle technology, supported by some ARM processors).



Key language features:

Automatic memory management;

Expanded capabilities for handling exceptional situations;

Rich set of I/O filtering tools;

A set of standard collections, such as array, list, stack, etc.;

Availability of simple tools for creating network applications (including using the RMI protocol);

Availability of classes that allow you to make HTTP requests and process responses;

Built-in language tools for creating multi-threaded applications;

Unified access to databases:

At the level of individual SQL queries - based on JDBC, SQLJ;

At the level of the concept of objects that have the ability to be stored in a database - based on Java Data Objects and Java Persistence API;

Template support (starting from version 1.5);

Parallel execution of programs.

1.4.3 C# programming language

In June 2000, it became known about a new programming language born in the depths of Microsoft. He became part new technology Microsoft, called .NET (read "Dot Net"). This technology provides a unified execution environment for programs (Common Language Runtime, CLR) written in different languages programming. One of these languages, the main one in this environment, is C# (C#, read “C sharp”). The name of the language, of course, wanted to emphasize its relationship with C++, because # is two intersecting pluses. But most of all new language similar to Java. And there is no doubt that one of the reasons for its appearance was Microsoft's desire to answer the challenge of Sun.

Although the authors of C# are not officially named, the title page of one of the preliminary editions of the language reference book identifies Anders Hejlsberg, the creator of Turbo Pascal and Delphi, who moved to Microsoft in 1996, and Scott Wiltamuth.

The unified program execution environment is based on the use of the intermediate language IL (Intermediate Language), which plays almost the same role as the bytecode of the Java virtual machine. Compilers used within the .NET technology from various languages ​​translate programs into IL code. Just like Java bytecode, IL code represents instructions on a hypothetical stack computer. But there is also a difference in the design and use of IL.

First, unlike the JVM, IL is not tied to a single programming language. Pre-release versions of Microsoft.NET include compilers for C++, C#, and Visual Basic. Independent developers can add other languages ​​by creating compilers from those languages ​​to IL code.

Secondly, IL is not intended for program interpretation, but for subsequent compilation into machine code. This allows you to achieve significantly higher program performance. The files containing IL code contain enough information for the optimizing compiler to work.

“C# is a simple, modern, object-oriented, type-safe language descended from C and C++. C# will be convenient and understandable for programmers who know C and C++. C# combines the productivity of Visual Basic with the power of C++." These words begin the description of C#.

Let's look at the technical features of the language:

The unit of compilation is a file (as in C, C++, Java). The file may contain one or more descriptions of types: classes (class), interfaces (interface), structures (struct), enumerations (enum), delegate types (delegate) with (or without) indication of their distribution across namespaces;

Namespaces regulate the visibility of program objects (as in C++). Namespaces can be nested. It is allowed to use program objects without explicitly specifying the namespace to which this object belongs. Just a general mention of the use of this namespace in the using directive (as in Turbo Pascal) is enough. There are aliases for namespace names in the using directive (as in the Oberon language);

Elementary data types: 8-bit (sbyte, byte), 16-bit (short, ushort), 32-bit (int, uint) and 64-bit (long, ulong) signed and unsigned integers, float ) and double precision, Unicode characters (char), Boolean type (bool, not compatible with integers), decimal type providing a precision of 28 significant digits (decimal);

Structured types: classes and interfaces (as in Java), one-dimensional and multidimensional (unlike Java) arrays, strings, structures (almost the same as classes, but allocated not on the heap and without inheritance), enumerations incompatible with integers (as in Pascal);

Delegate types or simply “delegates” (similar to procedural types in Modula-2 and Oberon, function pointers in C and C++);

Types are divided into reference types (classes, interfaces, arrays, delegates) and value types (elementary types, enumerations, structures). Objects of reference types are located in dynamic memory(heap), and variables of reference types are, in fact, pointers to these objects. With value types, variables are not pointers, but the values ​​themselves. Implicit type conversions are allowed only when they do not violate type safety or lead to loss of information. All types, including primitive types, are compatible with the object type, which is the base class of all other types. There is an implicit conversion of value types to the object type, called boxing, and an explicit inverse conversion- unboxing;

Automatic garbage collection (as in Oberon and Java);

Extensive set of operations with 14 priority levels. Redefining operations (as in ALGOL-68, Ada, C++). The checked and unchecked operators can be used to control overflow control when performing operations on integers;

Methods with value parameters, reference parameters (ref) and output parameters (out). The words ref and out must be written before the parameter not only in the method description, but also when calling. The presence of output parameters allows you to control the execution of defining assignments. According to the rules of the language, any variable must be guaranteed to have a value before an attempt is made to use it;

Control statements: if, switch, while, do, for, break, continue (as in C, C++ and Java). The foreach statement, which loops through each element of the “collection,” several variations of the goto operator;

Exception handling (as in Java);

Properties are elements of classes (objects), which are accessed in the same way as fields (you can assign or get a value), but are implemented by implicitly called get and set routines (as in Object Pascal, the input language of the Delphi system);

Indexers are elements of classes (objects) that allow you to access objects in the same way as arrays (by specifying the index in square brackets). Implemented by implicitly called get and set routines. For example, characters in a string can be accessed (read) as if they were elements of an array, due to the fact that for the standard class string indexer implemented;

Events are elements of classes (fields or properties) of a procedural type (delegates), to which, outside the class where they are defined, only the operations += and –= are applicable, allowing you to add or remove event handler methods for objects of this class;

Unsafe code that uses pointers and address arithmetic is localized in parts of the program marked with the unsafe modifier;

A preprocessor that, unlike C and C++, provides only conditional compilation tools.

Of course, the discussed disadvantages of C# do not at all deprive the language of its prospects. It is preferable to C++ in many respects. The general dissatisfaction with the C++ language, the recognition of which is the very appearance of the new language, is one of the main prerequisites for the success of C#.

Comparing C# with Java, you can see many similarities. True, if Java systems are multi-platform, then the C# implementation exists only for the operating system. Windows systems and only one. But, despite the heaviness, we can expect that the language will be implemented for other systems. Moreover, she herself Microsoft platform.NET with a single program execution environment can be promoted to alternative architectures, primarily UNIX systems.

C# seems to be a more realistic language than Java. Unlike Java, it is self-sufficient. That is, you can write any program in C# without resorting to other languages. This is possible due to the presence of “unsafe” code blocks that provide access directly to the hardware. In Java, to access the facilities low level“native methods” must be used, which must be programmed in other languages.

And, of course, the prospects for C# are primarily related to the efforts that Microsoft, of course, will make to promote it.

A Brief Course in the History of the Java Language

This section briefly describes the history of the Java language. This section is based on various published materials (mostly interviews with the creators of the Java language in the July 1995 issue of SunWorld).

Java's history goes back to 1991, when a group of engineers at Sun, led by Patrick Naughton and board member (and all-around computer wizard) James Gosling, set out to develop a small language that could be used to program everyday devices. devices, for example, controllers for switching cable television channels (cable TV switchboxes).

Since such devices do not consume much power and do not have large memory chips, I had to be small and generate very compact programs. Additionally, since different manufacturers may choose different Central Processor Units (CPUs), it was important not to get bogged down by any one computer architecture. The project was codenamed "Green".

In an effort to invent small, compact and machine-independent code, the developers revived the model used to implement the first versions Pascal language the dawn of the personal computer era. Niklaus Wirth, the creator of the Pascal language, once developed a machine-independent language that generates intermediate code for a certain hypothetical machine. This language became a commercial product under the name UCSD Pascal. (Such hypothetical machines are often called virtual machines—for example, the Java virtual machine, or JVM.)
This intermediate code can be executed on any machine that has an appropriate interpreter. The engineers working on Project Green also used a virtual machine, which solved their main problem.

However, most Sun employees had experience with the UNIX operating system, so the language they developed was based on C++, not Pascal. In particular, they made the language object-oriented rather than procedure-oriented.

As Gosling said in his interview: " Language is always a means, not an end"At first, Gosling decided to call it "Oak." (Perhaps because he liked to look at the oak tree growing right outside the windows of his office at Sun.) Then Sun employees learned that the word Oak was already used in as the name of a previously created programming language, and changed the name Java.

In 1992, the first products were released as part of the Green project, called
"*7". It was a vehicle for extremely intellectual remote control. (It had the power of a SPARK workstation while fitting into a 6x4x4-inch box.) Unfortunately, none of the manufacturing companies electronic engineering was not interested in this invention.

The group then began developing a cable television device that could provide new types of services, such as video on demand. And again they did not receive a single contract. (It’s funny that one of the companies that refused to sign a contract with them was led by Jim Clark, the founder of Netscape, which later did a lot for the success of the Java language.)

Throughout 1993 and half of 1994, an unsuccessful search for buyers of products developed within the framework of the Green project (under the new name “First Person, Inc.”) continued. (Patrick Naughton, one of the group's founders who later worked primarily in marketing, flew more than 300,000 miles in total trying to sell the technology.) First Person, Inc. was discontinued in 1994.

Meanwhile, within the Internet, a network was growing World Wide Web. The key to this network is the browser, which turns hypertext into an image on the screen.
In 1994, most people were using the Mosaic browser, a non-profit Web browser developed at the University of Illinois Supercomputing Center in 1993. (This browser was written in part by Mark Andreessen for $6.85 an hour. Mark was finishing college at the time and the browser was his diploma work. He then became one of the founders and chief programmer of Netscape, and fame and fortune came to him.)

In an interview with Sun World magazine, Gosling said that in mid-1994, the language's developers realized: "We need to create a really cool browser. This browser should be one of the few applications of fashionable client-server technology, in which exactly what we did would be vital: architectural independence, real-time execution, reliability, security - issues that were not extremely important for workstations. And we created such a browser."

In fact, the browser was developed by Patrick Naughton and Jonathan Payne. It later evolved into the modern HotJava browser. This browser was written in Java to demonstrate its full power. However, developers did not forget about the powerful tools now called applets, giving their browser the ability to execute code inside Web pages. The "technology demo" was presented at Sun World '95 on May 23, 1995, and sparked a Java craze that continues to this day.

Sun released the first version of the Java language in early 1996. A few months after it, Java version 1.02 appeared. People quickly realized that Java 1.02 was not suitable for developing serious applications. Of course, this version can be used to develop Web pages with dancing men, but in Java 1.02 you can't even print anything.

To be honest, Java 1.02 was still raw. Its successor, Java 1.1, filled in most of the gaping holes, greatly improving reflection capabilities and adding new model events for graphical user interface programming. Despite this, it was still quite limited.

The release of Java 1.2 was the main news at the JavaOne conference in 1998. IN new version weak tools for creating graphical user interfaces and graphical applications have been replaced by complex and large-scale tooling. This was a step forward towards the implementation of the slogan "Write Once, Run Anywhere" ™, put forward during the development of previous versions.

In December 1998, three days (!) after its release, the name of the new version was changed to the cumbersome phrase Java 2 Standart Edition Software Development Kit Version 1.2 (Standard Edition of the package tools for software development in Java 2 language, version 1.2).

In addition to the standard edition of the package ("Standard Edition"), two more options were offered: a "micro edition" ("Micro Edition") for portable devices, for example, for mobile phones, And. "Industrial Edition" ("Enterprise Edition") for creating server-based applications. Our book focuses on the standard edition.

Versions 1.3 and 1.4 of the standard edition of the toolkit are much more advanced than the original release of the Java 2 language. They have new features and, of course, contain many fewer bugs. In table Figure 1 1 shows the rapid growth of the API library as new versions of the standard edition SDK become available.

Table 1.1. The growth of the API library from the Java Standard Edition package

Number of classes and interfaces

Number of methods and fields

Common Misconceptions About the Java Language

This section lists and comments on some of the common misconceptions about Java.

Java language is an extension HTML language.

Java is a programming language, and HTML is a way of describing the structure of Web pages. There is nothing in common between them, with the exception of HTML language extensions that allow applets written in Java to be placed on Web pages.

I'm working in XML, so I don't need Java.

Java is a programming language, and XML is simply a way to describe data. Data described using XML can be processed by programs written in any programming language, but only the Java API library provides excellent support for processing such data. In addition, Java implements many of the features of XML. They are described in more detail in the second volume.

The Java language is easy to learn.

There is no programming language as powerful as Java that can be learned easily. It's easy to write toy programs, but doing serious work is always difficult. Also, note that this book only devotes four chapters to discussing the Java language itself. The remaining chapters explain how to work with its libraries, which contain thousands of classes and interfaces, as well as many thousands of functions. Fortunately, you don't need to know every one of them, but doing a real project requires a surprising amount of knowledge from the user.

Java is easy to program.

The Java SDK is not easy to use for anyone except programmers accustomed to the command line. There are integrated programming environments that include text editors, compilers, and drag-and-drop form creation tools, but they seem too complex and intimidating for a beginner. In addition, they often generate programs consisting of hundreds of lines. It seems to us that starting to learn the Java language using long programs, automatically generated and filled with comments like “DO NOT TOUCH YOUR HANDS!” is not a very good idea. We believe that the best way to learn Java is through familiar text editor. This is exactly what we will do.

The Java language will eventually become a universal programming language for all platforms.

Theoretically this is possible. This is what all software manufacturers dream of, except Microsoft. However, there are many applications that work great on personal computers, which will not work as well on other devices or browsers. In addition, these applications are written to take maximum advantage of the processors and machine-dependent libraries. In any case, they have already been ported to all important platforms. These applications include text and graphic editor, as well as Web browsers. Typically these applications are written in C or C++, and the user will gain nothing by rewriting them in Java. Additionally, rewriting them in Java will make these programs slower and less efficient, at least in the near future.

The Java language is just another programming language.

Java is a wonderful programming language. Many programmers prefer it over the C or C++ languages. However, there are hundreds of great programming languages ​​around the world that have never achieved widespread adoption, while languages ​​with obvious shortcomings, such as C++ and Visual Basic, have achieved stunning success.

Why? The success of any programming language is determined primarily by the practicality of its support system rather than the elegance of its syntax. Are there useful, convenient and standard libraries that allow you to do exactly what a programmer needs? Has a convenient environment for creating and debugging programs been developed? Are the language and its tools integrated into the computer infrastructure? The Java language has achieved success in the field server applications, because its class libraries make it easy to do things that were previously difficult, such as supporting networking and multithreading. The fact that Java has reduced the number of pointer bugs is also in its favor. Thanks to this, the productivity of programmers has increased. However, this is not the reason for his success.

Java programs are interpreted, meaning serious applications will run too slowly on a particular platform.

Many programs spend most of their time interacting with the user interface or waiting for data from the network. All programs, no matter what language they are written in, must respond to a mouse click within a certain time. Of course, you should not perform tasks that require high productivity central processor, using the Java language interpreter. However, on platforms that allow synchronous compilation, you just need to run the bytecodes for execution, and most issues will simply disappear.

After all, Java is great for developing network programs. Experience shows that it easily supports high speed transmissions over the network even during intensive calculations such as encryption. Since Java is faster than internet speeds, it doesn't matter that C++ programs can run even faster. Java is easier to program and programs written in it are machine-independent.

All Java programs run under Web browsers.

All applets written in Java actually run under the control of Web browsers. Actually, this is the definition of an applet - a program executed by a Web browser. However, it is entirely possible and appropriate to create stand-alone Java programs that run independently of the Web browser.

These programs (usually called applications) are completely machine independent. Just take the program and run it on another machine! Because Java is more user-friendly and less error-prone than C++, it is a good choice. When combined with database access tools such as the Java Database Connectivity package, the Java language becomes irresistible. It is especially convenient to learn it as a first programming language.
Most of the programs in this book are completely self-contained. Of course, applets are a lot of fun. However, Java applications are more important and useful in practice.

Java applets pose a great threat to security systems.

Several security bug reports have been published in the Java language. Most of them concerned the implementation of the Java language using a specific browser. Researchers took this as a challenge and began looking for security loopholes in the Java language to overcome the strength and complexity of the applet security model. The technical errors discovered were soon corrected, and as far as we know, no real system has ever been compromised. To appreciate the importance of this fact, remember the millions of viruses on the executable files of the Windows operating system and Word processor macros, which indeed caused a lot of trouble, and the surprisingly toothless criticism of the weaknesses of the attacked platform. In addition, the ActiveX mechanism in the browser Internet Explorer could cause a lot of criticism, but the methods of hacking it are so obvious that only a few experts bothered to publish their research.

Some system administrators They even started turning off Java language security in their browsers so that users could continue to download executable files, ActiveX controls, and documents created using the word processor Word. It's funny that nowadays the probability of overcoming the security system of Java applets is comparable to the probability of dying in a plane crash, while the risk of infecting a computer by opening a document created by a text Word processor, comparable to the risk of dying under the wheels of a car while crossing the road during rush hour.

The JavaScript language is a simplified version of the Java language.

JavaScript is a scripting language that can be used in Web pages. It was developed by Netscape and was first called LiveScript. The syntax of the JavaScript language is similar to the syntax of the Java language, but that is where the similarities end (except for the name, of course). A subset of the JavaScript language has been standardized under the name ECMA-262, but its extensions required for real work, were not standardized.

13K

In this guide, we'll cover everything you need to know before you start studying. programming in Java. You will learn about the capabilities of the platform, its application, and how to start learning Java correctly.

What is the Java programming language?

In 1991, the "Green Team", a division of Sun Microsystems, led by James Gosling, created a language for programming household electronic devices. At that time it was called Oak (“Oak”). Why "Oak"? Simply because this tree grew outside the window of Gosling’s office.

The Green Team demonstrated the use of Oak in an interactive TV. But for digital cable television of those years, this technology was too advanced. At the same time, the Internet was gaining popularity, for which the new programming language was best suited.

After some time, the new language was renamed Green, and after that - Java, in honor of coffee from the island of Java. That's why the Java logo features a coffee mug.

During the development of Java, C/C++ was popular, so Gosling made the language's syntax similar to C/C++ and implemented the " write once - run anywhere" In 1995, Sun Microsystems released the first official version Java. And at the same time it was announced that Java would be included in the Netscape Navigator browser.

In 2010, Sun Microsystems, along with the Java programming language, was acquired by Oracle Corporation.

Java version history

  1. June 1991 – start of the development project programming language Java.
  2. JDK 1.0 – January 1996.
  3. JDK 1.1 – February 1997.
  4. J2SE 1.2 – December 1998.
  5. J2SE 1.3 – May 2000.
  6. J2SE 1.4 – February 2002.
  7. J2SE 5.0 – September 2004.
  8. Java SE 6 - December 2006.
  9. Java SE 7 – July 2011.
  10. Java SE 8 – March 18, 2014.
  11. Java SE 9 – September 21, 2017.

Java Programming Language Features

Java is a cross-platform language

Java code written on one platform ( that is, the operating system), can be run without modification on other platforms.

Java is used to run Java ( Java Virtual Machine, JVM). The JVM processes the byte code, after which the processor processes the code received from the JVM. All virtual machines work similarly, so the same code runs the same on all operating systems, which is what makes Java a cross-platform programming language.

Object-oriented programming language

Exist various styles programming, and one of the most popular is object-oriented programming. With this approach complex problem is broken down into smaller ones by creating objects. Thanks to this, the code can be reused.

Object-oriented functions are found in many programming languages, including Java, Python, and C++. If you're serious about learning to program, an object-oriented approach is worth incorporating into your learning plan.

Java is fast

Early versions programming language Java has often been criticized for being slow. But today the situation has changed dramatically. New versions of the JVM run much faster, and the processors that interpret them are becoming faster and faster.

Today Java is one of the fastest programming languages. Well-optimized Java code runs almost as fast as low-level programming languages ​​such as C/C++ and much faster than Python, PHP, etc.

Java is a secure platform

Java is:

  • a secure platform for developing and launching applications;
  • provides tools for automatic memory management, which reduces code vulnerability;
  • ensures secure data transfer.

Extensive core library

One of the reasons why Java is so widespread is its huge standard library. It contains hundreds of classes and methods from various packages that make life easier for developers. Eg,

java.lang is advanced functions for strings, arrays, etc.

java.util – library for working with data structures, regular expressions, date and time, etc.

kava.io - library for file input/output, exception handling, etc.

Application of the Java Platform

Before learning Java programming from scratch, you need to know that more than 3 billion devices around the world work on this platform. What exactly can it be used for:

  1. Android Applications - To develop Android applications, the Java programming language is often used in combination with the Android SDK ( from English software development kit - software development kit).
  2. Web Applications - Java is used to create web applications using server programs, Struts framework and JSP. Some popular web applications written in Java are: Google.com, Facebook.com, eBay.com, LinkedIn.com.

It's worth noting that these sites are not necessarily written exclusively in Java, and may use other programming languages ​​as well.

  1. Software development– such programs as Eclipse, OpenOffice, Vuze, MATLAB and many others are written in Java.
  2. Big Data processing – to process “big data” you can use the Hadoop framework written in Java.
  3. Trading systems– using the platform Oracle Extreme Java Trading Platform, you can write programs for trading.
  4. Embedded devices– today billions of devices, such as TVs, SIM cards, Blu-ray players, etc., are based on Java Embedded technology from Oracle.

Also programming in Java is used to develop games, scientific applications ( for example, for processing natural language ) and in many other areas.

Java Terminology You Should Know

Java is a set of technologies ( programming language and computer platform), designed to create and run software. However, the term Java often refers to the programming language itself.

Programming language Java is a cross-platform object-oriented programming language general purpose with wide possibilities.

Java 9 is the latest version of Java at the time of this writing.

Java EE, Java Me and Java SE - these names stand for Java Enterprise Edition, Micro Edition and Standard Edition, respectively.

Java EE is aimed at applications that run on servers. Java ME is designed for power-constrained devices such as embedded devices. Java SE is the standard edition of Java for writing general programs.

If you are new to Java programming, we recommend starting with Java SE.

JVM - Java Virtual Machine (" Java virtual machine") is a program that allows a computer to run programs written in Java.

JRE - Java Runtime Environment(" Java runtime") includes the JVM, associated libraries, and other components needed to run programs. But the JRE does not have a compiler, debugger, or other development tools.

JDK – Java Development Kit Java developer) contains the JRE and other development tools such as compilers, debuggers, etc.

How to run Java on your operating system

How to Run Java on Mac OS

Here's what to do for Java programming from scratch and installing the platform on Mac OS X or macOS:

  1. Download the latest version of Java ( JDK) with Java SE download pages.
  2. Double-click on the downloaded DMG file and follow the instructions of the installer.
  3. To verify the installation, open a terminal and enter the following command:

javac –version

If Java is installed correctly, the program version will be displayed on the screen ( for example javac 1.8.0_60).

The next step is to install the IDE ( integrated development environment) for writing and running Java code. We will install free version IntelliJ IDEA and run Java on it. Here's what you need to do to do this:

  1. Go to IntelliJ download page and download the free Community Edition.
  1. Open the downloaded DMG file and follow the installation instructions. For quick access You can move IntelliJ IDEA to your Applications folder.
  2. Open IntelliJ IDEA. Select the option “Don’t import settings” (“ Don't import settings") and click "Ok". After this, accept the Jetbrains privacy policy by clicking on the “Accept” button.
  3. Now you can customize the interface for yourself. You can also skip this step and leave everything as default. If you are not sure, just skip this step by clicking the “Skip All and Set Defaults” button (“ Skip everything and set to default settings»).
  1. The program will show you a welcome page. Click on the button “Create New Project" (“ Create a new project»).
  2. In the next window, select "Java" in the left pane and click "New" at the top of the program window to select "JDK". Here you need to select the location where you installed the JDK, and then click Next.
  1. You will have the option to create a project from template (“Create project from template”). We ignore it and click the “Next” button.
  2. In the next installation step programming language Java enter the project name and click the "Finish" button.
  3. In the left panel you will see your project. If the panel is not visible, go to the menu Views > Tool Windows>Project.
  4. Go to Hello > New > Java and give the class a name. We called it First.
  1. To run the program you just wrote, go to Run > Run... Click on First ( that is, the name of the file we created

How to Run Java on Linux

To run examples from programming lessons from scratch Java on Linux will need the JAVA SE Development Kit ( JDK) and IDE for developing your projects. Follow the instructions step by step to get started with Java.

Install Java

  1. Open a terminal and type the following command to install Java:

    sudo add-apt-repository ppa:webupd8team/java sudo apt update; sudo apt install oracle-java8-installer

  1. Accept license agreement and terms of use by clicking “OK” and “Yes”, respectively.
  2. You have installed Java. To verify that the installation was successful, enter the following command in a terminal:

java –version

If the current version is displayed, the installation was successful. If not, check with Oracle help page.

Installing IntelliJ IDEA

  1. Go to .
  1. Download the free Community Edition by clicking the "Download" button.
  2. After downloading change the directory in the terminal to your downloads directory and extract the Java tar file into the /opt folder with the following command:

sudo tar xf .tar.gz -C /opt/

  1. After unpacking, change the directory to the bin folder of the IntelliJ IDEA program:

    cd /opt/ /bin

  2. To start the IDE, enter the following command:
  3. Select “Don’t import settings” (“ Don't import settings") and click "OK". After this, we accept the Jetbrains privacy policy by clicking on the “Accept” button.
  4. Now for the passage programming courses Java, you can customize the interface for yourself. Create a shortcut on your desktop for quick access to the program. After that, to launch the IDE, click “Next" at all the following stages.
  5. The program will show the welcome page. Click "Create New Project" (" Create a new project»).
  6. In the next window, select "Java" in the left pane and make sure that Java is selected in the Project SDK line. If not, then select the location where you installed JDK: /usr/lib/jvm/java-8-oracle.
  1. Click “Next” twice and create a project.
  2. In the next step, enter a project name and click the “Finish” button. Now in the left panel you will see your project. If this panel is not visible, go to the menu Views > Tool Windows > Project.
  3. Add new class Java. Select src from the left panel right click and go to New > Java Class. Provide a class name. There should be no spaces in the class name.
  1. Write the Java code and save the project.
  2. To run the program, go to Run > Run... Click on HelloWorld ( Project name) - the program will compile the file and run it.

How to Run Java on Windows (XP, 7, 8 and 10)

To master Java programming basics and running the platform on Windows, you will need a JAVA SE Development Kit (JDK) and an IDE for developing projects. Follow step by step instructions given below:

Installing Java

  • Go to download page Java Standard Edition Development Kit.
  1. In the Java SE Development Kit section at the top of the table, click "Accept License agreement" (" Accept the license agreement"). Then click on the Windows (x64) link if you have a 64-bit operating system or Windows (x86) if you have a 32-bit OS.
  1. After downloading, run installation file and follow the instructions that appear on the screen. Click " Next" Select all functions by pressing " This feature will be installed on local hard drive" and copy the installation location ( it is highlighted in yellow) in Notepad, then click again Next».
  1. During the installation process, you will be prompted to install the JRE. Click "Next" and then "Finish" to complete the installation.
  2. Now you need to edit the PATH variable. Go to Control Panel > System and Security > System. In the left pane, select " Additional system parameters".
  1. Click " Environment Variables» . In chapter " System variables" Find the PATH variable and in the next window click "Edit".
  1. Select all text in the " Variable value" and copy it to a separate text file. This will make it easier to edit and check for errors. See if the copied text contains the line: C: ProgramData Oracle Java javapath; . If yes, then you can move on to next step. If not, then paste the installation location you copied earlier at the beginning of the variable and add bin at the end of the line like this: C : Program Files (x 86) Java jdk 1.8.0_112 bin ; Please note that your JDK version (jdk 1.8.0_112) may be different. Copy the value of the variable and paste it into the PATH box.
  1. Click " OK" to save your changes.
  2. To check if the platform is installed correctly introduction to programming Java, open command line by typing cmd in the line Windows search or through the command “Run…” ( Windows - R). Enter the java -version command. If the current version of Java is displayed, the installation was successful. If not, check with Oracle help page.

Installing IntelliJ IDEA

  1. Go to IntelliJ IDEA download page.
  2. Download the free Community Edition by clicking "Download".
  1. Once downloaded, run the installation file and follow the instructions that appear on the screen. Then create a desktop shortcut for the 64-bit version and add associations with the .java extension. Click "Next" and continue with the installation.
  1. Once installed, open IntelliJ IDEA by clicking on the desktop icon.
  2. Select "Don't import settings" (" Don't import settings") and click "OK". After this, we accept the Jetbrains privacy policy by clicking “Accept”.
  3. Now you can customize the interface for yourself. You can also skip this step and leave everything as default by clicking the “Skip All and Set Defaults” button.
  4. The program will show the welcome page. Click "Create New Project" (" Create a new project»).
  1. In the next window, select "Java" in the left pane and click "New" at the top of the program window to select JDK. Here you need to select the location where you installed the JDK during the Java installation, and then click “Next”.
  2. IntelliJ IDEA will find the JDK and recognize it. There is no need to mark any other options, just click “Next".
  3. On the next screen, enter the project name: HelloWorld and click Finish. If the program says that the directory does not exist, click OK. If you don't see the left pane, go to Views > Tool Windows > Project.
  4. To set the class name, select the src folder in the left pane. Right-click on it, go to New > Java and set the class name. There should be no spaces in the class name.
  1. Write the code and save the Java project programming lesson.
  2. To run the program, go to the menu Run > Run... Click on HelloWorld - the program will compile the file and run it.

Your first Java program

To introduce users to a new programming language, they use the Hello World program (“Hello, world!”). This simple program, which displays the words Hello, World! In this section we will teach you how to write this program in Java using IntelliJ IDEA.

  1. Open IntelliJ IDEA.
  2. Go to File > New > Project… > Java ( in the left navigation bar).
  3. Set the Project Name from programming course J a va. We'll call it Hello World and click Finish.
  4. Now we need to create a new Java class.
  5. Select the src folder in the left pane, then go to File > New > Java Class and name the new class HelloWorld.
  6. Copy the following code into the HelloWorld.java file and save it.

public class HelloWorld ( public static void main(String args) ( // prints "Hello, World!" System.out.println("Hello, World!"); ) )

  1. Click the start button ( Run). If everything is in order, you will see Hello, World! on the screen.

How to learn Java?

Official Java Documentation

Oracle, the company that owns Java, publishes quality tutorials. The official documentation covers all Java features and is updated regularly.

Note: the only negative is that the official Java documentation is not always written in the simplest language.

If you really want to learn programming in Java, buy it good book. Of course, 1000 pages cannot be read in one day. But a good tutorial will help you learn programming.

Java: Complete Guide(10th edition)

A great book for those just starting to learn Java. The latest edition includes all the features of the Java 8 release.

The book covers everything you need to know about Java programming, including syntax, keywords, and programming fundamentals, as well as the Java API library, Java applets, and more.

Java Philosophy (4th Edition)

If you are switching to Java from another programming language, this book is for you. If you are starting from scratch, it is best to read it along with the other.

Java 8 Pocket Guide: Ambulance Java programmers

This book contains clear answers to questions that arise when teaching J ava programming from scratch. It briefly covers all the basic Java concepts (including Java 9). Don't want to flip through hundreds of pages looking for the right line? Buy this book.

Instead of a conclusion

If you start learning Java, you can't go wrong - it is a promising programming language, full of a wide variety of possibilities.

Before you start learning Java, here are a few tips:

  • Don't read educational articles and examples like a novel. The only way to become good programmer- write a lot of code.
  • If you're coming from another programming language (say, C#), you don't need to write code in the C# style.
  • Find Java communities online. Once you learn how to write simple programs in Java, find popular sites and forums dedicated to Java. Try to solve problems that other programmers have. This is a great way to expand your own knowledge. Plus, if you get stuck, you'll know where to ask for help.

We hope this article will encourage you to learn Java and help you start working on your first programs.

This publication is a translation of the article “ Learn Java Programming. The Definitive Guide", prepared by the friendly project team

A Java source file is a text file containing one or more class definitions. The Java translator assumes

that the source code of programs is stored in files with Java extensions. The code obtained during the translation process for each class is written in a separate output file, with a name that matches the name of the class, and the extension class.

First of all, in this chapter we will write, broadcast, and run the canonical “Hello World” program. After that, we'll look at all the essential lexical elements understood by the Java translator: whitespace, comments, keywords, identifiers, literals, operators, and delimiters. By the end of the chapter, you will have enough information to navigate a good Java program on your own.

So here is your first Java program

:

class HelloWorld(

System. out. println("Hello World");

In order to work with the examples given in the book, you need to obtain and install the Java Developers Kit over the network from Sun Microsystems - a package for developing Java applications (

http://java.sun.com/products/jdk ). Full description of the package utilities JDK - in Appendix 1 .

The Java language requires that all program code be contained within named classes. The above example text should be written to the HelloWorld.java file. Be sure to check that the capital letters in the file name match those in the name of the class it contains. In order to translate this example, you need to run the Java translator - javac, specifying the name of the file with the source text as a parameter:

\> javac HelloWorld.Java

The translator will create a file HelloWorld.class with the processor-independent bytecode of our example. In order to execute the resulting code, you must have a Java runtime environment (in our case this is java program), into which you need to load a new class for execution. We emphasize that the name of the class is indicated, and not the name of the file in which this class is contained.

>java HelloWorld

Not much useful work has been done, but we have verified that the installed Java translator and runtime environment work.

Step by step

HelloWorld is a trivial example. However, even such a simple program for a beginner in the languageJava can seem intimidatingly complex because it introduces you to a lot of new concepts and details of the language's syntax. Let's take a closer look at each line of our first example, analyzing the elements that make up a Java program.

class HelloWorld(

This line uses a reserved word

class. It tells the translator that we are going to define a new class.The full class definition appears between the opening curly brace on line 1 and the matching closing curly brace on line 5. The curly braces inJava is used in exactly the same way as in C and C languages++.

public static void main (String args) (

This seemingly overly complex example string is a consequence of an important requirement when the Java language was designed. The point is that in

Java lacks global functions. Since similar lines will appear in most of the examples in the first part of the book, let's take a closer look at each element of the second line.

By breaking this string into separate tokens, we immediately encounter the keyword

public This - access modifier, which allows the programmer to control the visibility of any method and any variable. In this case, the access modifierpublic means the methodmain is visible and accessible to any class. There are 2 more access level indicators - private and protected, which we will get to know in more detail inchapter 8 .

The next keyword is

static. This word is used to declare class methods and variables used to work with the class as a whole. Methods that use the static keyword in their declaration can only work directly with local and static variables.

You will often need methods that return a value of one type or another: for example,

int for integer values, float - for real ones, or the class name for programmer-defined data types. In our case, we just need to display a string and return the value from the methodmain is not required. That is why the modifier was usedvoid. This issue is discussed in more detail inchapter 4 .

Finally we get to the method name

main. There is nothing unusual here, it’s just that all existing implementations of Java interpreters, having received the command to interpret a class, begin their work by calling the method main. A Java translator can translate a class that does not have a main method. But the Java interpreter runs classes without a method main can't.

All parameters that need to be passed to the method are specified inside a pair of parentheses as a list of elements separated by ";" characters. (semicolon). Each element of the parameter list consists of a type and identifier separated by a space. Even if a method has no parameters, you still need to put a pair of parentheses after its name. In the example we are now discussing, the method

main has only one parameter, albeit of a rather complex type. String args declares a parameter namedargs, which is an array of objects - representatives of the classString. Note the square brackets after the args identifier. They indicate that we are dealing with an array, and not with a single element of the specified type. We'll return to a discussion of arrays in the next chapter, but for now let's note that the type String - this is class. We'll talk about strings in more detail inChapter 9 .

System. out. prlntln("Hello World!");

This line executes the method

println of the out object. An object out is declared in the classOutputStream and is statically initialized in the System class. IN chapters 9 And 13 you will have a chance to get acquainted with the nuances of how classes work String and OutputStream.

The closing curly brace on line 4 ends the method declaration

main, and the same parenthesis on line 5 ends the HelloWorld class declaration.

Lexical Basics

Now that we've looked at the minimal Java class in detail, let's go back and look at the general syntax of the language. Programs for

Java - it is a collection of spaces, comments, keywords, identifiers, literal constants, operators and delimiters.a language that allows arbitrary formatting of program text. In order for the program to work properly, there is no need to align its text in a special way. For example, classHelloWorld could be written in two lines or any other way you like. And it will work exactly the same, provided that there is at least one space, tab character, or newline character between the individual tokens (between which there are no operators or separators).

Comments

Although comments have no effect on the executable code of the program,

when used correctly, they turn out to be a very significant part of the source text. There are three types of comments: single-line comments, multi-line comments, and finally documentation comments. Single-line comments begin with // characters and end at the end of the line. This commenting style is useful for providing short explanations of individual lines of code:

a = 42; // If

42 - the answer, then what was the question?

For more detailed explanations, you can use comments placed on several lines, starting the comment text with the characters /* and ending with the characters */. In this case, all text between these pairs of characters will be regarded as a comment and the translator will ignore it.

* This code is a bit complicated...

*I'll try to explain:

The third, special form of comments is intended for the service program

javadocwhich uses Java translator components to automatically generate documentation for class interfaces. The convention used for this type of comment is to place a documenting comment before the declaration of a public class, method, or variable., you need to start it with the characters /** (slash and two asterisks). Such a comment ends in the same way as a regular comment - with the symbols */. The javadoc program can recognize in documenting comments some special variables whose names begin with the @ symbol. Here is an example of such a comment:

* This class can do amazing things. We recommend it to anyone who

* wants to write an even more advanced class, take it as

* basic.

* @see Java. applet. Applet

* ©author Patrick Naughton

class CoolApplet extends Applet ( /**

* This method has two parameters:

key is the name of the parameter.is the value of the parameter namedkey.

*/ void put (String key, Object value) (

Reserved Keywords

Reserved keywords are special identifiers that the language

Java is used to identify built-in types, modifiers, and program execution controls. Today in the language J ava available 59 reserved words(see table 2). These keywords, along with the syntax of operators and delimiters, are included in the language descriptionJava. They can only be used for their intended purpose and cannot be used as identifiers for variable, class or method names.

table 2

Java Reserved Words

Note that the words

byvalue, cast, const, future, generic, goto, inner, operator, outer, rest, varreserved in Java, but not yet used. In addition, in Java there are reserved method names (these methods are inherited by every class and cannot be used except in cases of explicit overriding of class methods Object).

Table 3

Reserved method names

Java

Identifiers

Identifiers are used to name classes, methods, and variables. The identifier can be any sequence of lowercase and uppercase letters, numbers, and the symbols _ (underscore) and $ (dollar). Identifiers should not begin with a number, so that the translator does not confuse them with numeric literal constants, which will be described below.

Java - case-sensitive language. This means that, for example, Value and VALUE - various identifiers.

Literals

Constants in

Java are given by their literal representation. Integers, floating point numbers, boolean values, characters and lines can be placed anywhere source code. The types will be discussed inchapter 4 .

Integer literals

Integers are the type most commonly used in regular programs. Any integer value, such as 1, 2, 3, 42, is an integer literal. This example shows decimal numbers, that is, numbers with a base of 10 - exactly those that we use every day outside the world of computers. In addition to decimal, numbers with base 8 and 16 - octal and hexadecimal - can also be used as integer literals. Java recognizes octal numbers by leading them with a leading zero. Normal decimal numbers cannot start with zero, so the program's use is external valid number 09 will result in an error message when translating because 9 is not in the range 0..

7, valid for octal digits. Hexadecimal constants are distinguished by leading zero-x characters (0x or 0X). The range of hexadecimal digit values ​​is 0..15, and as numbers for the values ​​10..15 letters from A to are used F (or from a to f). With hexadecimal numbers you can express computer-friendly values ​​in a concise and clear way, for example by writing Oxffff instead of 65535.

Integer literals are values ​​of type

int, which is in Java is stored in a 32-bit word. If you require a value that is greater than approximately 2 billion, you must use a constant likelong. In this case, the number will be stored in a 64-bit word. For numbers with any of the bases mentioned above, you can assign a lowercase or uppercase letter to the rightL, thus indicating that given number belongs to the typelong. For example, Ox7ffffffffffffffL or9223372036854775807L is the largest value for a number of type long.

Floating point literals

Floating point numbers represent decimal values ​​that have fraction. They can be written in either regular or exponential formats. In the usual format, a number consists of a number of decimal digits, followed by a decimal point, and the following decimal digits of the fractional part. For example, 2.0, 3.14159, and .6667 are valid values ​​for floating point numbers written in standard format. In exponential format, after the listed elements, the decimal order is additionally indicated. The order is determined by the positive or negative decimal number following the symbol E or e. Examples of numbers in exponential format: 6.022e23, 314159E-05, 2e+100. IN

Java floats are treated as doubles by default. If you require a type constantfloat, the symbol F or f must be added to the right of the literal. If you are a fan of redundant definitions, you can add to literals like double character D or d. Default Type Valuesdouble are stored in a 64-bit word, less precise type values float - in 32-bit.

Boolean literals

A boolean variable can only have two values ​​-

true and false (false). Boolean values true and false are not converted to any numeric representation. Keyword true in Java is not equal to 1, and false is not equal to 0. In Java these values ​​can only be assigned type variables boolean or used in expressions with logical operators.

Character literals

Symbols in

Java - these are the indexes in the symbol tableUNICODE. They are 16-bit values ​​that can be converted to integers and to which integer arithmetic operators, such as addition and subtraction, can be applied. Character literals are placed inside a pair of apostrophes (" "). All visible table symbolsASCII can be directly inserted inside a pair of apostrophes: -"a", "z", "@". For characters that cannot be entered directly, several escape sequences are provided.

Table 3.

2. Escape sequences

Control sequence

Description

Octal character

(ddd)

Hexadecimal character

UNICODE (xxxx)

Apostrophe

Backslash

Carriage return

Line feed (line feed, new line)

Page translation

(form feed)

Horizontal tabulation

(tab)

Go back one step

(backspace)

String literals

String literals in

Java looks exactly the same as many other languages ​​- it's arbitrary text enclosed in a pair double quotes(""). Although lowercase literals inJava are implemented in a very unique way(Java creates an object for each line), this does not appear externally. Examples of string literals:“Hello World!”; "two\strings; \And this is in quotes\"". All escape sequences and octal/hexadecimal notations that are defined for character literals work exactly the same in strings. String literals inJava codes must start and end on the same line of source code. In this language, unlike many others, there is no escape sequence for continuing a string literal on a new line.

Operators

An operator is something that performs some action on one or two arguments and produces a result. Syntactically, operators are most often placed between identifiers and literals. The operators will be discussed in detail in

chapter 5 , their list is given in Table 3. 3.

Table 3.

3. Language operators Java

Separators

Only a few groups of characters that can appear in a syntactically correct Java program still remain unnamed. These are simple delimiters that affect appearance and functionality of the program code.

Name

What are they used for?

round brackets

They highlight lists of parameters in a method declaration and call; they are also used to set the priority of operations in expressions, highlight expressions in program execution control statements, and in type casting operators.

braces

square brackets

Used in array declarations and when accessing individual elements array.

semicolon

Separates operators.

Separates identifiers in variable declarations, also used to link statements in a loop header

for.

Separates package names from subpackage and class names, and is also used to separate a variable or method name from a variable name.

Variables

A variable is the main element of storing information in a Java program. A variable is characterized by a combination of identifier, type, and scope. Depending on where you declared the variable, it may be local, for example to the code inside for loop, or it can be a class instance variable that is available to all methods of that class. Local scopes are declared using curly braces.

Variable Declaration

The basic form of a variable declaration is:

type identifier [ = value] [, identifier [ = value

7...];

A type is either one of the built-in types, that is,

byte, short, int, long, char, float, double,boolean, or the name of a class or interface. We will discuss all these types in detail innext chapter . Below are some examples of variable declarations various types. Note that some examples involve initializing an initial value. Variables for which no initial values ​​are specified are automatically initialized to zero.

The example below creates three variables corresponding to the sides of a right triangle and then

c Using the Pythagorean theorem, the length of the hypotenuse is calculated, in this case the number 5, the size of the hypotenuse of a classic right triangle with sides 3-4-5.

class Variables(

public static void main (String args) (

= Math.sqrt(a* a + b* b);

System.out.println("c = "+ c);

Your first step

We have already achieved a lot: first we wrote a small program in the language

Java and looked in detail at what it consists of (code blocks, comments). We have been introduced to a list of keywords and operators whose purpose will be explained in detail in subsequent chapters. Now you are able to independently distinguish the main parts of any Java program and are ready to start readingchapter 4 , which discusses in detail simple types data.