Java For Beginners: Get From Zero to Object-Oriented Programming
by Lucas Barzan
Contents
Introduction
I have worked really hard to make this book for you because I know how it can be difficult to learn programming, especially when youre starting out. So I want to teach you in the way Ive learned, which is by doing.
Who is this book for
This book is for everyone who wants to learn Java and knows nothing or little about programming. If you want to be a programmer and you want to start by learning Java, this book is your first step. If you want to develop Android apps, this book is your first step. If you want to learn something new, this book is your first step.
Who should back away from this book
First of all, this book is for people who are willing to learn something new. There will be many times in which youll feel confused or overwhelmed, but you have to able to handle that frustration if you want to achieve your final goal. Your WHY has to be strong enough so that you wont give up in the middle of this book.
Also, I dont recommend this book if youre looking for an advanced guide that will cover absolutely everything about the Java programming language. I will teach you the essential stuff about Java to get you up and running in your path to become a programmer.
How to Learn Java
Some books only give you the theory and and dont even bother to make you practice. I think thats a terrible way to teach (programming, at least). You should get hands-on experience as soon as possible and because of that youll often be writing and running code in your IDE throughout this book. So I recommend you to be next to a computer. Think of it as a course, not a romance to read on your bed or on the beach.
Practicing should be taken seriously. Its the one and only way to be a great programmer and I cant guarantee that youll learn anything if you dont do the exercises. I mean it. Theres no shortcut.
The exercise sections are called Workout Sets because our intelligence is like a muscle: the more we use it, the more we grow it.
By the end of this book, youll have done 70 exercises.
Although I provide the answers to the exercises in most cases, just thinking about an exercise and jumping to the answer is not practicing. And if you do that, youll just be lying to yourself. So, if youve downloaded this book, you have to make a commitment to yourself. Read. Think. Do the exercises. Repeat.
Trust the process and great results will come!
What if something goes wrong?
Its very common to make mistakes when programming, especially in the beginning. So, if you have any questions that are not covered in this book, you can follow these steps to solve your problem:
First, search for your problem. I can almost guarantee you that someone else has been through the same situation. If you get an error, search for that error. If you want to know how to implement something, search for that. Youre probably going to find some answers on StackOverflow , which is the biggest Q&A website for developers.
If you havent found any answer although thats very unlikely , you can create a new question on StackOverflow and wait until someone answers it (its the internet, it wont take much time to happen). Follow the websites instructions to post your question and, first of all, be sure that there isnt any similar questions that can help you, otherwise yours will be removed as a duplicate.
Chapter 0: Java?
What is Java?
According to its official website, Java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!.
Java is a general-purpose language, which means it is used to build almost anything (i.e. it wasnt created to solve only one type of coding problem).
And before I forget, Java has nothing to do with Javascript, except for the similar name and the fact that they are both famous programming languages.
I will not spam you with pages of Javas history because thats out of the scope of this book. All you need to know is that Java is famous and has been on the market for a long time, so there isnt any plans for its death. Which brings us to the next section...
Why Learn Java?
Although Java is not the most beginner-friendly language out there, it offers other benefits, such as:
- Programs written in Java are easy to maintain;
- Java has a huge community and youll never be alone. If you get an error, its very likely that it happened to other people as well;
- People who know how to program in Java have access to great career opportunities. Many tech giants such as Google or Amazon also use Java to develop the backend of their websites, and because it has pretty good performance, a lot of startups who reach the scaling point would integrate their apps with Java to power features that need to have good performance;
- Java isnt going anywhere, unlike other unestablished programming languages.
How Java Works
1. You write the code
First, we write code in Java. It's called our source code. Here's an example:
public class myFirstClass {
public static void main ( String [] args ) {
System . out . println ( "Hello, World!" );
}
}
Don't worry! You don't understand anything now and it might look very confusing, I know.
If I were to give me any advice when I started programming, I'd say "Get used to not knowing what everything means. It's completely OK. It just means you have things to learn!".
2. You compile it
Then there's javaC, which is our Java Compiler. It checks for errors and won't let you compile until it's satisfied that everything will run correctly.
The compiler creates a new document, coded into Java bytecode. Any device capable of running Java will be able to interpret/translate this file into something it can run. The compiled bytecode is platform-independent. And that's where JVM comes in.
3. The JVM runs it
The JVM (Java Virtual Machine) interprets the bytecode that was created and gives instructions that the device will understand. Each OS (whether it's Windows, MacOS, Linux or even a mobile platform) can have its own JVM in order to run Java programs.
No matter in what OS the bytecode gets created, it can run on any device that has a JVM. That's what is called "Write Once, Run Anywhere" .
Chapter 1: Getting Started
Java Development Kit
JVM = Java Virtual Machine.
JRE = Java Runtime Environment, which contains everything you need in order to execute Java applications.
JDK = Java Development Kit, which contains everything you need in order to develop Java applications. Thats what we need to download.
WARNING: Unless you have a secret fetish that involves downloading and preparing stuff, things will be a little bit boring for now. But that's something we just have to do once. Then you'll only need to do something like this again if you're updating these tools, which doesn't happen all the time.
By the time I'm writing this, the newest JDK version is JDK 9. Do a little research and if there's a newer and also stable version, download it.