The Java programming language: where to start learning. Where is Java applied?

Table of contents:

The Java programming language: where to start learning. Where is Java applied?
The Java programming language: where to start learning. Where is Java applied?
Anonim

Java - PL from Sun microsystems. It was originally developed as a language for programming electronic devices, but later became used for writing server software applications. Java programs are cross-platform, meaning they can run on any operating system.

Java Programming Basics

Java as an object-oriented language follows the basic principles of OOP:

  • inheritance;
  • polymorphism;
  • encapsulation.
java basics
java basics

In the center of "Java", as in other OOPs, there are an object and a class with constructors and properties. It is better to start learning the Java programming language not from official resources, but from manuals for beginners. In such manuals, the features are described in detail, code examples are provided. 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, thenruns on the JVM virtual machine. The conversion to bytecode is done in Javac, Jikes, Espresso, GCJ. There are compilers that translate the C language into Java bytecode. Thus, a C application can run on any platform.

The "Java" syntax is characterized by the following:

  1. Class names must start with a capital letter. If the name consists of several words, then the second one should start with uppercase.
  2. If multiple words are used to form a method, the second one must start with a capital letter.
  3. Processing starts with the main() method - it is part of every program.

Types

The Java programming language has 8 primitive types. They are shown below.

types in java
types in java
  • Boolean - boolean type, takes only two values true and false.
  • Byte - 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 of -128 to 127.
  • Short has a range of -32768 to 32767, used to represent numbers. The size of variables of this type is 2 bytes.
  • Int also stands for numbers, but its size is 4 bytes. It is most often used to work with integer data, and byte and short are sometimes promoted to int.
  • Long are used for large integers. Possible values range from -9223372036854775808 to 9223372036854775807.
  • Float and double are used for decimals. Themthe difference is that float is useful when high precision in the fractional part of the number is not required.
  • Double displays all characters after the delimiter ".", and float - only the first ones.
  • String is the most commonly used primitive type for defining strings.

Classes and objects

Classes and objects play an important role in Learning the Java Programming Language for Beginners.

classes and objects in java
classes and objects in java

A class defines a template for an object, it must have attributes and methods. To create it, use the Class keyword. If it is created in a separate file, then the name of the class and file 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 parent's methods. To do this, use the word extends:

class class_name extends superclass_name {};

A constructor is a member of any class, even if it is not explicitly set. In this case, the compiler creates it on its own:

public class Class{ public Class(){ } public Class(String name){ }}

The name of the constructor is the same as the name of the class, by default it has only one parameter:

public Puppy(String name)

Object is created from a class using the new() operator:

Point p=new Point()

It gets 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.

  • class Point {

    int x, y;

    }

    Point p=new Point()

    class TwoPoints {

    public static void main(String args[]) {

    Point p1=new Point();

    Point p2=new Point();

    p1.x=10;

    p1.y=20;

    p2.x=42;

    p2.y=99;

    } }

Object variables and objects are completely different entities. Object variables are links. They can point to any variable 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. They are local by default and cannot be used in other classes. The "." operator is used to access the fields:

classname.variable

You can set static fields with 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:

import static classname;

Method is a subroutine for those classes in which it is declared. Described at the same level as variables. Specified as a function and can be of any type, including void:

  • class Point { int x, y;

    void init(int a, int b) {

    x=a;

    Y=b;

    } }

In the example above, the Point class has integer x and y fields, the init() method. Access to methods, as well as tovariables, is done by using the operator ".":

Point.init();

The init property does not return anything, so it is of type void.

Variables

Variables occupy a separate place in the Java Programming Language Tutorial. All variables have a specific type, it determines the required place for storing values, the range of possible values, the list of operations. Before manipulating values, variables are declared.

what is java
what is java

Several variables can be declared at the same time. A comma is used to list them:

int a, b, c;

Initialization is carried out after or during the declaration:

int a=10, b=10;

There are several types:

  • local variables (local);
  • instance variables;
  • static variables (static).

Local variables are declared in methods and constructors, they are created during the startup 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 of the declared block. In Java, variables do not have an initial value, so it is mandatory to assign one before first use.

Instance variables must be declared inside the class. They are used as methods, but they can only be accessed after the object has been created. The variable is destroyed when the object is destroyed. Instance variables, unlike local variables, have values according todefault:

  • numbers - 0;
  • logic - false;
  • links are null.

Static variables are called class variables. Their names start with an uppercase character and are specified with the static modifier. They are used as constants, respectively, one specifier from the list is added to them:

  • final;
  • private;
  • public.

Started at the beginning of the program, destroyed after the execution stops. Just like instance variables, they have default values that are assigned to empty variables. Numbers have the value 0, booleans have the value false, object references are initially null. Static variables are called like this:

ClassName. VariableName

Garbage Collector

In The Java Programming Language for Beginners Tutorial, the automatic garbage collector section is the most interesting.

garbage collector
garbage collector

In Java, unlike the C language, it is not possible to manually delete an object from memory. For this, an automatic deletion 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. There are methods for forced garbage collection, although they are not recommended for normal use.

The module for automatically deleting unused objects runs in the background and starts when the program is inactive. To clear objects from memory, the program stops, after the memory is freed, it is interruptedoperation resumes.

Modifiers

There are different types of modifiers. In addition to those that determine the access method, there are modifiers for methods, variables, and a class. Methods declared as private are available only in the declared class. Such variables cannot be used in other classes and functions. Public gives access to any class. If you need to get a Public class from another package, then you must first import it.

types of modifiers
types of modifiers

The protected modifier is similar to public - it allows access to class fields. In both cases, the variables can be used in other classes. But the public modifier is available to absolutely everyone, and the protected modifier is available only for inherited classes.

The modifier used when creating methods is static. This means that the generated method exists independently of instances of the class. The Final modifier does not control access, but indicates the impossibility of further manipulation of the object's values. It prohibits changing the element for which it is specified.

Final for fields makes it impossible to change the first value of the variable:

  • public static void mthod(String[] args) {

    final int Name=1;

    int Name=2;// gives an error

    }

Variables with final modifier are constants. They are usually written in capital letters only. CamelStyle and other methods don't work.

Final for methods indicates a prohibition on changing the method in the inheritedclass:

  • final void myMethod() {

    System.out.printIn("Hello world");

    }

Final for classes means that the class cannot be inherited:

  • final public class Class {

    }

Abstract - modifier for creating abstract classes. Any abstract class and abstract methods are intended to be further extended in other classes and blocks. The transient modifier tells the virtual machine not to process the given variable. In this case, it simply won't save. For example, transient int Name=100 will not persist, but int b will.

Platforms and versions

Existing Java programming language families:

  • Standard Edition.
  • Enterprise Edition.
  • Micro Edition.
  • Card.
java platforms
java platforms
  1. SE - is the main one, widely used to create custom applications for individual use.
  2. EE is a set of specifications for enterprise software development. Contains more features than SE, so it is commercially used by large and medium enterprises.
  3. ME - designed for devices with limited power and memory, they usually have a small display size. Such devices are smartphones and PDAs, digital TV receivers.
  4. Card - designed for devices with extremely limited computing resources, such as smart cards, sim cards, ATMs. For these purposes, the bytecode has been changed,platform requirements, constituting libraries.

Application

Programs in the Java programming language tend to be 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 optimizations, the Java Virtual Machine has improved its performance.

Actively used to create mobile applications for Android. The program is compiled into a non-standard bytecode and executed on the ART virtual machine. Android Studio is used for compilation. This IDE from Google is the official Android development IDE.

Microsoft has developed its own implementation of the Java Virtual Machine MSJVM. 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 that worked only on the Windows platform. Microsoft released the J language, whose syntax and overall operation is very similar to Java. It did not conform to the official specification and was eventually removed from the standard Microsoft Visual Studio developer toolkit.

Java programming language and environment

Software development is carried out in these IDEs:

  1. JDK.
  2. NetBeans IDE.
  3. Eclipse IDE.
  4. IntelliJ IDEA.
  5. JDeveloper.
  6. Java for iOS.
  7. Geany.

JDK is distributed by Oracle as a Java Development Kit. Includes compiler, standardlibraries, utilities, executive system. Modern IDEs rely on the JDK.

It is convenient to write Java programming language code in Netbeans and Eclipse IDE. These are free integrated development environments, they are suitable for all Java platforms. Also used for programming in Python, PHP, JavaScript, C++.

IntelliJ IDE from Jetbrains is distributed in two versions: free and commercial. Supports writing code in many programming languages, there are third-party plugins from developers that implement even more programming languages.

JDeveloper - another development from Oracle. Entirely written in Java, so it works on all operating systems.

Popular topic

Editor's choice