Java - Java from Sun microsystems. It was originally developed as a language for programming electronic devices, but later became used to write server software applications. Java programs are cross-platform, that is, they are able to work on any operating systems.
Java programming basics
Java as a language with support for object orientation meets the basic principles of OOP:
- inheritance;
- polymorphism;
- encapsulation.
In the center of "Java", as in other OOY, there is an object and a class with constructors and properties. To start learning the Java programming language is better not with official resources, but with tutorials for beginners. Such manuals describe in detail the possibilities, provide code examples. Books like “The Java Programming Language for Beginners” explain in detail the basic principles and features of the named language.
Features
Code in the Java programming language is translated into bytecode, then executed in the JVM virtual machine. Conversion to bytecode is performed on Javac, Jikes, Espresso, GCJ. There are compilers that translate the C language into Java bytecode. Thus, the C application can run on any platform.
The syntax "Java" is characterized by the following:
- Class names must begin with a capital letter. If the name consists of several words, then the second should begin with an uppercase.
- If several words are used to form the method, the second of them should begin with a capital letter.
- Processing begins with the main () method - it is part of every program.
Types
The Java programming language has 8 primitive types. They are presented below.
- Boolean - a boolean type, takes only two values true and false.
- Byte is the smallest integer type with a size of 1 byte. It is used when working with a stream of data or files, raw binary data. Has a range from -128 to 127.
- Short has a range from -32768 to 32767, used to represent numbers. The size of variables of this type is 2 bytes.
- Int also denotes numbers, but its size is 4 bytes. It is most often used to work with integer data, and byte and short sometimes increase to int.
- Long are used for large integers. Possible values range from -9223372036854775808 to 9223372036854775807.
- Float and double are used to indicate fractional. Their difference is that float is convenient when high precision in the fractional part of a number is not required.
- Double displays all characters after the separator “.”, And float - only the first.
- String is the most used primitive type by which strings are specified.
Classes and Objects
An important role in the book "Learning the Java Programming Language for Beginners" is played by classes and objects.
A class defines a template for an object; it must have attributes and methods. To create it, use the keyword Class. If it is created in a separate file, the class and file names must be the same. The name itself consists of two parts: the name and the extension .Java.
In Java, you can create a subclass that will inherit the methods of the parent. To do this, use the word extends:
- class class_name extends superclass_name {};
A constructor is a component of any class, even if it is not specified explicitly. In this case, the compiler creates it yourself:
- public class Class {public Class () {} public Class (String name) {}}
The name of the constructor is the same as the class name, by default it has only one parameter:
- public Puppy (String name)
Object is created from the class using the new () operator:
It receives all the methods and properties of the class with which it interacts with other objects. One object can be used several times under different variables.
Object variables and objects are completely different entities. Object variables are references. They can point to any variables of a non-primitive type. Unlike C ++, their type conversion is strictly regulated.
Fields and Methods
Fields are all variables associated with a class or object. By default, they are local and cannot be used in other classes. To access the fields, use the "." Operator:
You can set static fields using the static keyword. Such fields are the only way to store global variables. This is due to the fact that there are simply no global variables in Java.
Implemented the ability to import variables to gain access from other packages:
A method is a subroutine for those classes in which it is declared. It is described at the same level as the variables. It is set as a function and can be of any type, including void:
In the example above, the Point class has fields of type integer x and y, the init () method. Access to methods, as well as to variables, is carried out by using the “.” Operator:
The init property does not return anything, so it is of type void.
Variables
In the tutorial of the Java programming language, a separate place is occupied by variables. All variables have a specific type, it determines the required place to store values, the range of possible values, a list of operations. Before manipulating values, variables are declared.
Several variables can be declared at the same time. A comma is used to list them:
Initialization is carried out after or during the announcement:
int a = 10, b = 10;
There are several types:
- local variables
- instance variables
- static variables
Local variables are declared in methods and constructors; they are created during the launch of the latter and destroyed after completion. For them it is forbidden to specify access modifiers and control the level of accessibility. They are not visible outside the declared block. In Java, variables do not have an initial value, so it is mandatory to be assigned before first use.
Instance variables must be declared inside the class. They are used as methods, but you can access them only after creating the object. A variable is destroyed when an object is destroyed. Instance variables, unlike local ones, have default values:
- numbers are 0;
- logic is false;
- references are null.
Static variables are called class variables. Their names begin with an uppercase character and are instantiated with the static modifier. They are used as constants, respectively, one qualifier from the list is added to them:
Run at the beginning of the program, destroyed after stopping execution. Just like instance variables, they have standard values that are assigned to empty variables. Numbers have a value of 0, boolean variables are false, object references are initially null. Static variables are called as follows:
Garbage collector
In the tutorial, "Java programming language for beginners," the automatic garbage collection section is the most interesting.
In Java, unlike the C language, it is not possible to manually delete an object from memory. To do this, the automatic removal method is implemented - the garbage collector. With traditional deletion via null, only the reference to the object is removed, and the object itself is deleted. Forced garbage collection methods exist, although they are not recommended for use in normal work.
The module for automatic removal of unused objects runs in the background, it starts when the program is inactive. To clear objects from memory, the program stops; after the memory is freed, the interrupted operation is resumed.
Modifiers
There are different types of modifiers. In addition to those that determine the access method, there are modifiers of methods, variables, and class. Methods declared as private are available only in the declared class. Such variables cannot be used in other classes and functions. Public opens access for any class. If you need to get the Public class from another package, you should import it first.
The protected modifier is similar in effect to public - opens access to the fields of the class. In both cases, variables can be used in other classes. But the public modifier is available for absolutely everyone, and the protected modifier is available only for inherited classes.
The modifier used to create methods is static. This means that the created method exists independently of class instances. The Final modifier does not control access, but indicates the impossibility of further manipulating the values of the object. It prohibits changing the element for which it is specified.
Final for fields makes it impossible to change the first value of a variable:
Variables with the final modifier are constants. They are usually written only in capital letters. CamelStyle and other methods do not work.
Final for methods indicates a ban on changing a method in an inherited class:
Final for classes means that you cannot create class inheritors:
Abstract - modifier for creating abstract classes. Any abstract class and abstract methods are intended for further expansion in other classes and blocks. The transient modifier tells the virtual machine not to process the given variable. In this case, it simply will not be saved. For example, transient int Name = 100 will not be saved, and int b will be saved.
Platforms and Versions
Existing Java programming language families:
- Standard Edition.
- Enterprise Edition
- Micro Edition.
- Card.
- SE - is the main, widely used to create custom applications for personal use.
- EE is a set of specifications for enterprise-level software development. It contains more features than SE, so it is used commercially in large and medium-sized enterprises.
- ME - designed for devices with limited power and memory, they usually have a small display size. Such devices are smartphones and PDAs, digital television receivers.
- Card - designed for devices with extremely limited computing resources, such as smart cards, sim cards, ATMs. For these purposes, the bytecode was changed, the platform requirements that make up the libraries.
Application
Programs in the Java programming language are usually slower and take up more RAM. A comparative analysis of the Java and C languages showed that C is a bit more productive. After numerous changes and optimization of the virtual machine, Java has improved its performance.
It is actively used to create mobile applications for Android. The program is compiled into non-standard byte code, it is executed on the ART virtual machine. For compilation, Android Studio is used. This Google IDE is official for Android development.
Microsoft has developed its own implementation of the MSJVM Java Virtual Machine. It had such differences that broke the fundamental concept of cross-platform - there was no support for some technologies and methods, there were non-standard extensions working only on the Windows platform. Microsoft has released the J # language, the syntax and work of which is very similar to Java. It did not meet the official specification and was ultimately excluded from the standard Microsoft Visual Studio developer tools.
Java programming language and environment
Software development is carried out in such IDEs:
- JDK.
- NetBeans IDE.
- Eclipse IDE.
- IntelliJ IDEA.
- JDeveloper.
- Java for iOS.
- Geany.
The JDK is distributed by Oracle as a Java development kit. It includes a compiler, standard libraries, utilities, and an executive system. Modern integrated development environments rely on JDK.
It’s convenient to write code in the Java programming language in Netbeans and the Eclipse IDE. These are free integrated development environments; they fit all Java platforms. Also used for programming in Python, PHP, JavaScript, C ++.
Jetbrains IntelliJ IDE is available in two versions: free and commercial. It supports writing code in many programming languages, there are third-party plug-ins from developers that implement even more PLs.
JDeveloper is another development from Oracle. It is completely written in Java, therefore it works on all operating systems.