You get a bonus - 1 coin for daily activity. Now you have 1 coin

Java Basics The first Java program / tokens. Identifiers. Comments

Lecture



Starting to learn a new language, it is useful to ask what source data can be processed by the means of this language, in what form they can be set, and what standard means of processing this data are embedded in the language. This is a rather boring task, because in every developed programming language there are a lot of data types and even more rules for using them. However, non-observance of these rules leads to the appearance of hidden errors, which are sometimes very difficult to detect. Well, in every craft you have to “play scales” first, and we cannot get away from it.

All the rules of the Java language are exhaustively set out in its specification, abbreviated as JLS. Sometimes, in order to understand how a particular construction of the Java language is performed, it is necessary to refer to the specification, but, fortunately, this is rare, the rules of the Java language are quite simple and natural.

This chapter lists the primitive data types, operations on them, control operators, and the pitfalls that should be avoided when using them. But let's start, according to tradition, with the simplest program.

First java program

According to a long tradition dating back to the C language, programming language textbooks begin with the program "Hello, World!". He will break this tradition. In listing 1.1, this program is in its simplest form, written in the Java language.

Listing 1.1. The first program in Java;

class HelloWorld {

public static void main (String [] args) {

System.out.println ("Hello, XXI Century World!");

}

}

That's all, just five lines! But even with this simple example, you can see a number of essential features of the Java language.

  • Each program is one or several classes, in this simplest example there is only one class (class).
  • The beginning of the class is marked with the service word class followed by the class name chosen arbitrarily, in this case Helloworld . All that is contained in the class is written in curly brackets and makes up the body of the class (class body).
  • All actions are performed using information processing methods, simply say method. This name is used in the Java language instead of the name "function" used in other languages.
  • Methods are distinguished by name. One of the methods must be called main , the program starts with it. In our simplest program, there is only one method, which means its name is main .
  • As befits a function, a method always returns as a result (most often, it returns (returns)) only one value, the type of which is necessarily indicated before the name of the method. The method may not return any value, playing the role of the procedure, as in our case. Then, instead of the return type, the word void is written, as was done in the example.
  • After the method name in brackets, separated by commas, the arguments (or arguments) or method parameters are listed. For each argument, specify its type and, after the space, the name. In the example, only one argument, its type is an array consisting of strings of characters. The string of characters is the type of string that is built into the Java API, and the square brackets are a sign of an array. The array name can be arbitrary; in the example, the name args is chosen.
  • Modifiers may be written before the value returned by the method. In the example there are two of them: the word public means that this method is accessible from everywhere; The static word provides the ability to call the main () method at the very beginning of the program. Modifiers are generally optional, but they are necessary for the main () method.

Comment

In the text of this book after the name of the method brackets are put to emphasize that this is the name of the method, and not a simple variable.

  • Everything that contains a method, the method body, is written in curly braces.

The only action that the main () method performs in the example is to call another method with the complex name System.out.println and pass it to the processing of one argument, the text constant "Hello, 2lth century world!" . Text constants are written in quotes, which are only delimiters and are not part of the text.

The compound name System.out.println means that the System class, part of the Java API, defines a variable named out , containing instances of one of the Java API classes, the PrintStream class, which has the println () method. All this will become clear later, but for now we will simply write this long name.

The effect of the println () method is to output its argument to the output stream, usually associated with displaying a text terminal, in the MS-DOS Prompt or Command Prompt or Xterm window , depending on your system. After the output, the cursor moves to the beginning of the next line of the screen, as indicated by the ending ln , the word println - abbreviation of the words print line. The Java API also has a print () method, leaving the cursor at the end of the output string. Of course, this is a direct influence of the Pascal language.

Immediately make an important remark. The Java language distinguishes between lowercase and lowercase letters; the names main, main, and MAIN are different from the "point of view" of the Java compiler. In the example it is important to write a String, System with a capital letter, and main with a small one. But inside the text constant, it doesn’t matter whether to write Century or century , the compiler doesn’t “look” at it at all, the difference will be visible only on the screen.

Comment

The Java language distinguishes between uppercase and lowercase letters.

You can write your names as you like, you could give the class the name helloworid or helloworid , but there is an agreement between the Java programmers called the "Code Conventions for the Java Programming Language" stored at http://java.sun.com/docs /codeconv/index.html. Here are a few points of this agreement:

  • class names begin with a capital letter; if the name contains several words, then each word begins with a capital letter;
  • method and variable names begin with a lowercase letter; if the name contains several words, each next word begins with a lowercase letter;
  • the names of the constants are written in capital letters; if the name consists of several words, then an underscore is inserted between them.

Of course, these rules are optional, although they are included in the JLS, Section 6.8, but they greatly facilitate the understanding of the code and give the program a Java-specific style.

The style is determined not only by the names, but also by placing the program text in rows, for example, the arrangement of curly brackets: should the opening curly brace be at the end of the line with the class or method header, or should it be carried to the next line? For some reason, this trivial question causes fierce debate, some development tools, such as JBuilder, even offer to choose a certain style of brace placement. Many firms set their own corporate style. In the book we will try to follow the style of "Code Conventions" and with regard to splitting the program text into lines (the compiler treats the entire program as one long line, for it the program is just a sequence of characters), and in terms of indents ( indent) in the text.

So, the program is written in any text editor, for example, Notepad. Now it must be saved in a file whose name matches the name of the class containing the main () method and give the file name a Java extension. This rule is very desirable to perform. In doing so, the Java runtime system will quickly find the main () method to get started, simply by looking for a class that matches the file name.

Board

Name the program file with the name of the class containing the main () method, respecting the case of letters.

In our example, we will save the program in a file named HelloWorld.java in the current directory. Then we call the compiler, passing it the file name as an argument:

javac HelloWorld.java

The compiler will create a file with bytecodes, give it the name Helloworid.class and write this file to the current directory.

It remains to call the interpreter, passing in the name of the class (and not the file) as an argument:

Java HelloWorld

The screen will display:

Hello, 21st Century World!

Comment

Do not specify the class extension when invoking the interpreter.

In fig. 1.1 shows how it all looks in the Command Prompt window of the MS Windows 2000 operating system.

  Java Basics The first Java program  tokens.  Identifiers. Comments

Fig. 1.1. Command Prompt window

When working in an integrated environment, all of these actions are triggered by selecting the appropriate menu items or using hot keys — there are no uniform rules here.

Comments

You can insert comments into the program text that the compiler will not take into account. They are very useful for explanations during the program. During the debugging period, one or several operators can be turned off from actions, marking them with comment symbols, as programmers say, by “commenting” them. Comments are entered as follows:

  • behind two oblique features in a row //, without a space between them, the comment begins, continuing to the end of the line;
  • behind the forward slash and asterisk / * begins the comment, which can take several lines, up to the asterisk and the forward slash * / (without spaces between these characters).

Comments are very convenient for reading and understanding the code; they turn the program into a document describing its actions. A program with good comments is called self-documented. Therefore, Java introduces third type comments, and the JDK includes a javadoc program that extracts these comments into separate HTML files and creates hyperlinks between them: behind a slash and two asterisks in a row, without spaces, / ** the comment begins, which can take several lines up to an asterisk (one) and a slash * / and be processed by the javadoc program. In such a comment, you can insert instructions to the javadoc program, which begin with the symbol @.

This is how JDK documentation is created.

Add comments to our example (Listing 1.2).

Listing 1.2. The first program with comments

/ **

* Explaining the content and features of the program ...

* @author First Name (author)

* @version 1.0 (this is the software version)

* /

class HelloWorld {// HelloWorld is only a name

// Next method starts program execution

public static void main (String [] args) {// args are not used

/ * The following method simply prints its argument.

* on the display screen * /

System.out.println ("Hello, 21st Century World!");

// The next call is commented out,

// method will not be executed

// System.out.println ("Farewell, 20th Century!");

}

}

Asterisks at the beginning of lines have no meaning, they are written just to highlight the comment. An example, of course, is overloaded with explanations (this is a bad style), different forms of comments are simply shown here.

created: 2014-10-09
updated: 2021-03-13
132548



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Object oriented programming

Terms: Object oriented programming