We use cookies to personalize and improve the user experience of our website.
Cookie settings
Cookies are necessary for the website to function properly.
Java for beginners
Java is a high-level object-oriented programming language developed in 1995 by Sun Microsystems. Since then, it has become one of the most popular programming languages in the world, used to develop a wide range of applications including mobile apps, web apps, and desktop apps.
In this longread, we'll cover the core concepts of the Java programming language, as well as provide some code examples to help you get started programming in the language.
Basic Concepts 1. Object Oriented Programming (OOP) Java is an object-oriented programming language, which means that it uses objects to represent data and operate on it. Each object has properties and methods that can be used to interact with objects of other classes. 2. Classes and objects In Java, every object is created based on a class. A class defines the properties and methods of an object. For example, a Book class might have properties such as book title, author, and ISBN, as well as methods such as open book and close book. When a Book object is created, it has its own values for properties, such as "The Catcher in the Rye" for the title of the book. 3. Variables and data types Variables are used to store data in a program. There are several data types in Java such as integer type (int), floating point type (float), boolean type (boolean) and others. Each data type determines what type of data can be stored in the variable. 4. Control structures Control constructs are used to control the flow of program execution. Java has several control constructs, such as if-else and switch conditional statements, for, while, and do-while loops. 5. Arrays Arrays are used to store a set of data of the same type. For example, an array can contain a set of integers or strings. Arrays in Java are created using the "new" keyword and are defined by the data type and the number of elements. 6. Methods Methods are blocks of code that perform specific operations and can be called from other parts of the program. Each method has a name, a return type (if any), and a parameter list (if any). For example, a "printHello" method could be created to print the message "Hello" to the console. The method can be called from another method or from the main function of the program. 7. Exceptions Exceptions are errors that can occur during the execution of a program. In Java, exceptions can be handled using the try-catch construct, which allows the programmer to handle exceptions and prevent the program from crashing. 8. Inheritance and polymorphism Inheritance is a mechanism that allows classes to inherit the properties and methods of other classes. The descendant class can add its own properties and methods, as well as change or override the properties and methods of the parent class. Inheritance allows you to create class hierarchies, where descendant classes inherit properties and methods from parent classes. Polymorphism is a mechanism that allows objects of different classes to use the same methods with the same names. For example, the "Square" and "Circle" classes may have a "calculate area" method that will be used to calculate the area of each shape.
The original developer of Java, James Goslin, named the Java language after his favorite brand of coffee, which he respected and consumed daily in his work. Later, the visual effects of coffee beans were used as the Java logo, which became very recognizable in the world of information technology.
In this example, we have created a "Book" class that has "title", "author" and "ISBN" properties. The class also has a constructor that takes the values of these properties when the object is created. The class also has methods for getting each of the properties.
Code examples
Example 1: Creating a class
public class Book { // Свойства класса private String title; private String author; private int isbn;
// Конструктор класса public Book(String title, String author, int isbn) { this.title = title; this.author = author; this.isbn = isbn; }
// Методы класса public String getTitle() { return title; }
public String getAuthor() { return author; }
public int getIsbn() { return isbn; } }
Example 2: Creating a class object
Book myBook = new Book("Над пропастью во ржи", "Джером Д. Сэлинджер", 978-5-17-082547-