Why learn Java and how to do it effectively. Yandex report

How is Java different from other popular languages? Why Java might be the first language to learn? Let's make a plan that will help you learn Java both from scratch and by applying programming skills in other languages. Let's list the differences between creating production code in Java and developing in other languages. Mikhail Zatepyakin read this report at a meeting for future participants internships Yandex and other novice developers - Java Junior meetup.


Hello everyone, my name is Misha. I am a developer from Yandex.Market, and today I will tell you why you should learn Java and how to do it effectively. You can ask a reasonable question: why will I tell this, and not some strong developer with a bunch of years of experience? The fact is that I myself studied Java recently, a year and a half ago, so I still remember how it was approximately and what are the pitfalls.

A year ago, I got an internship at Yandex.Market. I developed a backend for Beru, for the Market itself, you probably used it. Now I continue to work there, in a different team. We are developing the Yandex.Market analytical platform for business partners.

Why learn Java and how to do it effectively. Yandex report

Let's get started. Why learn Java from a practical point of view? The fact is that Java is a very famous programming language. He has a very large community.

For example, there is such a TIOBE index, a popular index of the popularity of programming languages, and Java takes the first place there. Also on sites with vacancies, you will probably notice that most of the vacancies are just about Java, that is, developing in Java, you can always find a job.

Since the community is very large, any question you have will be answered on some Stack Overflow or other sites. Also, when developing in Java, you are actually writing code in the JVM, so you can easily switch to Kotlin, Scala, and other languages ​​that use the JVM.

Why learn Java and how to do it effectively. Yandex report

Why is Java good from an ideological point of view? There are different programming languages. They solve different problems, you know that. For example, it's great to write one-line scripts in Python to solve quick problems.

On the pluses, you can fully control the executable code. For example, we have cars, Yandex unmanned vehicles, their code is written in pluses. Why? Java has such a thing - Garbage Collector. It clears RAM from unnecessary objects. This thing starts spontaneously and does stop-the-world, that is, it stops the rest of the program and goes to count objects, clear memory from objects. If such a thing will work in a drone, it's not cool. Your drone will drive straight, at that moment it will clear its memory and not look at the road at all. Therefore, the drone is written on the pluses.

Why learn Java and how to do it effectively. Yandex report

What tasks does Java solve? This is primarily a language for developing large programs that are written for years, tens or hundreds of people. In particular, a lot of the backend in Yandex.Market is written in Java. We have a distributed team in several cities, ten people in each. And the code is easy to maintain, it has been maintained for ten or more years, and at the same time new people come in, understand this code.

What characteristics should a language have in order for the code in it to be easily maintained and to be easily developed in large teams. First of all, it should be readable code, and it should be easy to implement complex architectural solutions on it. That is, it should be easy to write high-level abstractions on it, etc. All this is exactly what Java provides us with. It is an object-oriented language. It is really easy to implement high-level abstractions and complex architectures on it.

There are also a lot of frameworks and libraries for Java, because the language is already over 15 years old. During this time, everything that could be written was written on it, so there are a bunch of libraries for everything that you might need.

Why learn Java and how to do it effectively. Yandex report

What basic skills, in my opinion, should a beginner javier have? First of all, this is knowledge of the Java core language. Further, this is some kind of Dependency Injection framework. The next speaker, Kirill, will talk about this more fully. I won't go too deep. Next is the architecture and design patterns. We need to be able to write architecturally beautiful code in order to write great applications. And this is some kind of SQL or ORM for database tasks. And it's more about the backend.

Why learn Java and how to do it effectively. Yandex report

Go! java core. Here I won’t discover America especially - you need to know the language itself. What you should pay attention to. Firstly, Java has released a lot of versions in recent years, that is, in 2014-2015, the seventh one was released, then the eighth, ninth, tenth, a lot of new versions, and a lot of new cool things were introduced into them, for example, the Java Stream API , lambda, etc. Very cool fresh cool things that are used in production code, what is asked in interviews and what you need to know. Therefore, you should not take a book from the shelf in the Java-4 library and go learn it. Such a plan: learn Java-8 or higher.

We carefully pay attention to innovations, such as the Stream API, var, etc. They are asked at interviews, they are constantly used in production. That is, the Stream API is much cooler than cycles, in general, a very cool thing. Be sure to pay attention.

And there are other things like iterators, Exceptions, and so on. Something that seems unimportant to you while you are writing some little code yourself. You don't need those Exceptions, who needs them anyway? But they will definitely be asked at interviews, they will definitely come in handy in production. In general, you should pay attention to Exceptions, iterators and other things.

Why learn Java and how to do it effectively. Yandex report

Data structures. Without structures, nowhere, and it will be great if you don’t just know that there are set, dictionary, sheets. And also different implementations of structures. For example, the same dictionary in Java has many implementations, including HashMap and TreeMap. They have different asymptotics, they are arranged differently inside. You need to know how they differ, when to use which.

It will also be very cool if you know how these data structures work internally. That is, it is not easy to know their asymptotics - how long does the rate work, how long does the passage work, but how the structure inside works - for example, what is a bucket in HashMap.

You should also pay attention to trees and graphs. These are things that are not very common in production code, but they are loved in interviews. Accordingly, you need to be able to bypass trees, graphs in width, depth. These are all simple algorithms.

As soon as you start writing any big code, complex, library-based, multi-class code, you will realize that you have a hard time without build systems and resolve dependencies. This is primarily Maven and Gradle. They allow you to import libraries into your project in really one line. That is, you write a one-line xml and import it into the library project. Great systems. They are about the same, use either Maven or Gradle.

Next - some kind of version control system. I recommend Git because it's popular and there are tons of tutorials. Almost everyone uses Git, cool thing, you can't do without it.

And - some kind of development environment. I recommend IntelliJ Idea. It greatly speeds up the development process, helps you a lot, writes any boilerplate code for you, in general, is cool.

Why learn Java and how to do it effectively. Yandex report

Links from the slide: SQLZOO, habrapost

SQL. A little bit about backenders. Here, in fact, was a funny case. Two days before my second interview for an internship, an HR girl called me and said that in two days they would ask me SQL and HTTP, I need to learn. And I didn’t know anything about SQL or HTTP about anything. And I found such a cool site - SQLZOO. On it, I learned SQL in 12 hours, I mean, SQL syntax, how to write SELECT queries, JOIN, etc. Very cool site, I highly recommend it. In fact, in 12 hours I learned 90% of what I know now.

It's also great to know database architecture. These are any keys, indexes, normalization. There is a series of posts about this on Habré.

Why learn Java and how to do it effectively. Yandex report

In Java, besides SQL, there are all sorts of Object-relational mapping systems like JPA. There is some code. In the first method, some SQL code is SELECT id name FROM info.users WHERE id IN userIds. From the users database, from the table, they get their IDs, names.

Next, there is a mapper that turns an object from a base into a Java object. And there is a third method below, which, in fact, executes this code. All this with the help of JPA can be replaced with one line, which is written below. It does the same thing - find All ByIdIn. That is, by the name of the method, it generates an SQL query for you.

Very cool stuff. I myself, when I did not know SQL, used JPA. In general, pay attention. If you are too lazy to learn SQL - fire. And, in general, fire!

Why learn Java and how to do it effectively. Yandex report

Spring. Who heard about such a thing as the Spring framework? See how many of you there are? Not without reason. Spring is in the requirements of every second Java backend job. Without it, it’s really nowhere in a large development. What is Spring? First of all, it is a Dependency Injection framework. About this too will tell next speaker. But in short, this is a thing that allows you to easily import the dependencies of one class to another. That is, the knowledge of dependencies is simplified.

Spring Boot is a piece of Spring that allows you to boot your server application with one button. You go to THID, press a couple of buttons, and now you have your server application up on localhost 8080. That is, you haven’t written a single line of code yet, but it already works. Very cool stuff. If you write something of your own - fire!

Spring is a very large framework. It not only brings you a server application and resolves Dependency Injection. It allows you to do a lot of things, including creating REST API methods. That is, you wrote some method, attached the Get mapping annotation to it. And now you already have some method on localhost that writes you Hello world. Two lines of code and it works. Cool thing.

Spring also makes it easier to write tests. Without testing in a large development in any way. The code needs to be tested. To do this, Java has a cool JUnit 5 library. And JUnit in general, but the latest version is the fifth. There is everything for testing, all sorts of assertions and other things.

And there is an awesome Mockito framework. Imagine that you have some functionality that you want to test. The functionality does a lot of things, including, somewhere in the middle, it enters VKontakte with your ID, for example, and receives the name and surname of the VKontakte user from the ID. Probably, you will not enter VKontakte in the tests, this is strange. But you need to test the functionality, so you made this class, with the help of Mockito, a mock, its imitation.

You will say that when a request with such and such an ID comes to this class, it returns some last name, for example, Vasya Pupkin. And it will work. That is, you will test all the functionality for a mok of one class. Very cool stuff.

Why learn Java and how to do it effectively. Yandex report

Link from slide

Design patterns. What it is? These are templates for solving common development problems. In development, the same or similar tasks often arise, which would be great to solve somehow well. Therefore, people came up with best practices, some patterns, how to solve these problems.

There is a website with most popular patterns - refactoring.guru, you can read, find out what patterns are, read a bunch of theory. The problem is that it's practically useless. In fact, patterns without practice are of little use.

You will hear about some patterns like Singletone or Builder. Who heard these words? A lot of people. There are such simple patterns that you can implement yourself. But most of the patterns: strategy, factory, facade - it's not clear where to apply them.

And until you see in practice in some foreign code the place to which this pattern is applied, you will not be able to apply it yourself. Therefore, practice is very important with patterns. And just reading about them on refactoring.guru isn't super helpful, but it's a must-do.

Why learn Java and how to do it effectively. Yandex report

Why are patterns needed? Suppose you have some User class. It has an Id and a Name. Each User must have both an Id and a Name. Top left is class.

What are the ways to initialize User? Two options - either a constructor or a setter. What are the disadvantages of both approaches?

Constructor. new User(7, "Bond"), ok. Now let's say that we do not have a User class, but some other one with seven numeric fields. You will have a constructor with seven consecutive numbers. It is not clear what these numbers are, which one belongs to which property. The constructor is not cool.

The second option is setter. You clearly write: setId(7), setName("Bond"). You understand which property belongs to which field. But the setter has a problem. Firstly, you can forget to hit something, and secondly, your object becomes mutable. This is not thread safe and slightly reduces the readability of the code. Therefore, people came up with a cool pattern - Builder.

Why learn Java and how to do it effectively. Yandex report

What is it about? We will try to collect the advantages of both approaches - both the setter and the constructor - in one. We make a certain object, Builder, which will also have the Id and Name fields, which will itself be built on the basis of the setter, and which will have the Build method, which returns you a new User with all the parameters. We get an immutable object and a setter. Cool!

What are the problems? Here we have the classic Builder. The problem is we can still forget to hit some field. And if we forgot to visit the ID, in this case in the Builder it will be initialized with zero, because the int type is not nullable. And if we make the Name "Bond" and forget to visit the ID, we will have a new User with id "0" and the name "Bond". Not cool.

Let's try to fight this. In the Builder, we will change int to int so that it is nullable. Now everything is great.

Why learn Java and how to do it effectively. Yandex report

If we try to make a User with the name “Bond”, forgetting to give it an ID, we will get a null pointer exception, because the ID is not nullable, and the Builder has null, specifically the pointer exception.

Why learn Java and how to do it effectively. Yandex report

But we can still forget to put the name, so we set object replay to null. Now, when we build our object from the Builder, it checks that the field is not nullable. And that is not all.

Let's look at the last example. In this case, if we somehow put null in the ID runtime, and it would be great to know right away that you did it and it’s not cool that you are now making a mistake.

Why learn Java and how to do it effectively. Yandex report

You need to throw an error not at the moment of creating the User, but when you set it to the null ID. Therefore, in the Builder, we will change Integer to int in the setter, and he will immediately swear here that he threw out null.

In short, what is the point? There is a simple Builder pattern, but even in its implementation there are some subtleties, so it's very cool to look at different implementations of patterns. Each pattern has dozens of implementations. It's all very interesting.

Why learn Java and how to do it effectively. Yandex report

How do we write Builder in production code? Here is our User. We hang the Builder rotation from the Lombok library on it, and it itself generates the Builder for us. That is, we do not write code, but Java already considers that this class has a Builder, and we can call it like this.

I mentioned earlier that Java has libraries for just about everything, including Lombok, a cool library that lets you avoid having to write a boilerplate. builder, GET.

Why learn Java and how to do it effectively. Yandex report

Patterns are architectural - related not only to one class, but to the system as a whole. There's a cool principle in systems design: the Single Responsibility Principle. What is he talking about? The fact that each class should be responsible for some of its functionality. In this case, we have a Controller that communicates with users, JSON objects. There is Facade, which converts JSON objects into models, with which the Java application will work further. There is a Service that has complex logic that works with these models. There is a Data Access Object that puts these models in the base and takes them out of the base. And there is the database itself. In other words, it's not all in one class, but we're making five different classes, and that's another pattern.

Why learn Java and how to do it effectively. Yandex report

Once you've more or less learned Java, it's great to write your own project that will have a database, work with other APIs, and expose your server application to REST API clients. It would be great to put such a thing on your resume, this is a cool ending to your training. With that, you can go and get a job.

Why learn Java and how to do it effectively. Yandex report

Here is an example of my server application. In my second year, I wrote term paper with the guys. They were writing a mobile application for organizing events. There, users could log in through VKontakte, put points on the map, create events, invite their friends to them, save an image of events, etc.

What did I do in the project? Wrote a server application on Spring Boot without using SQL. I did not know him, I used JPA. What can it do? Log in to VK via OAuth-2. Take the user's token, go to VK with him, check that this is a real user. Receive information about users through VKontakte. It skillfully saves information in the database, also through JPA. Skillfully save pictures and other files in the computer's memory, and save links to them in the database. At that time, I did not yet know that there were CLOB objects in the database, so I did it this way. There was a REST API for users, client applications. And there were unit tests for basic functionality.

[…] A small example of my successful learning of Java. In my first year at the university, they taught me C # and gave me an understanding of OOP programming - what are classes, interfaces, abstraction, why are they needed. It helped me a lot. Without this, learning Java is quite difficult, it is not clear why classes are needed.

Why learn Java and how to do it effectively. Yandex report

In the second year at the university, they again gave Java core, but I didn’t stop there, I went to study Spring myself and wrote a term paper, my project, which I mentioned above. And with all this, I went on an internship at Yandex, passed an interview, and got into Yandex.Market. There I wrote a backend for Beru, this is our marketplace, and for Yandex.Market itself.

After that, six months ago, I transferred to another team within the same Market. We do analytics for business partners. We are in the analytical platform, there are three of us on the backend, so I have a very large share of influence on the project. It's very interesting, actually. That is, we actually provide data on the market - what sales, in what categories, in what models, for business partners, large well-known companies. And there are only three of us, we write this code, and it's very cool.

Thank you! Useful links:
"Java 8. Beginner's Guide".
Data structures.
SQLZOO.
Database normalization.
Design patterns.
Design Patterns.
CleanCode.
Effective Java.

Source: habr.com

Add a comment