A Piece of Java
Introduction to Programming
Faith Kim
Copyright 2021 Faith Kim
All rights reserved. This book or any portion may not be reproduced or used in any manner whatsoever without the express written permission of the publisher except for the use of brief quotations in a book review.
Although the author has made every effort to provide correctness of the information in this book, the author gives no warranty and accepts no responsibility or liability for the completeness and accuracy of information contained in this book. The author will not be held responsible or liable for any losses, damages, costs, or disruption caused directly or indirectly by the errors or omissions in the information provided in this book.
All product names and company names are trademarks or registered trademarks of their respective owners. Use of them does not imply any affiliation or endorsement by them. Any company names, brands, and trademarks used within are for clarification and educational purposes and no owners are in any way affiliated with this work.
Contents
Chapter 0: Welcome to Java
Welcome to learning how to program in Java! This book is meant for beginners in programming, so congratulations on starting your journey.
Theres many code examples provided, so I highly recommend you type along with the code and refrain from copying and pasting. We want to build the muscle memory in your typing to get comfortable with writing in Java.
Theres so much to learn, and I am no expert myself. I have struggled in my college years learning Java, and I hope to pass on some of my knowledge so that you dont struggle as much as I did.
This book will not go over the nitty gritty technicals of how Java works at a machine level but just enough information to get you coding.
What is Java?
Java is a programming language first created by Sun MicroSystems in the 1990s, then later acquired by Oracle Corporation in the 2000s. Java is used to create desktop applications, web applications, and even apps for mobile devices, so its pretty versatile in use.
Java is a great language to learn to build your foundation in programming, specifically in object oriented programming. I believe when you learn Java, you're able to pick up on other languages pretty quickly since the OOP principles stay consistent. So Java is a nice transferable skill to have.
As mentioned before, theres many code examples. The final version of the code and solutions to exercises will be available here: http://bit.ly/introtojavabook
Should you run into issues troubleshooting Java or would like more clarification on programming concepts, please feel free to email me: apieceofjava@gmail.com
Chapter 1: Setting up Java
I will provide two options in which you can experience Java:
Option 1: Writing and Running Java on your computer. This will be the primary way we will learn Java in this book.
Option 2: Use an online platform to write and run Java. If you cannot get Option 1 to work, then you can use this option.
Section 1.1: Tools You Need to Run Java
There are three things you need to write and run Java:
1. File Organizer
2. Text Editor
3. Command Window
The three components Ive mentioned exist on your computer already. Lets go over each one.
Tool 1: File Organizer
This is where you will create new folders to save your code.
On my desktop, Ive created a new folder called Java_Practice. I will be saving all of my code to this folder.
Tool 2: Text Editor
Just as you would use applications like a word processor to write your essays, you need an application to write and save code.
The basic text editors you can use on your computer is the following:
For Windows users: Notepad
For Macintosh users: TextEdit
We'll use these text editors to save Java files. They arent pretty as they dont color code, but they get the basic job done.
If you want color coding, then I suggest downloading Atom. Atom is a text editor supported for both Windows and Macintosh computers: https://atom.io/
Tool 3: Command Window
On Windows computers, its called the Command Line or Command Prompt. On Macintosh computers, its called a Terminal. Throughout this book, I will mostly call it the command window, though I may use these terms interchangeably.
A command window is a remote control to your computer. It knows about directories, file locations, and what files exist in those directories. You can also travel to different folders.
We can use the command window to also run code. In our case, well be running Java programs.
Look up Command Line on your Windows machine or Terminal on your Macintosh computer. It should look something like this:
For Windows users, it should be a black box. The default display for Macintosh users is usually a white box.
Notice the C:\Users\Faith. This is telling us the current file location the command window is aware of. Your file path will look different from mine since were on different computers.
On Windows, you may see C:\Users\
On Macintosh computers, you may see ~
That is your root directory, and that's usually where your downloads, desktop, and documents folders are located.
You should know the following command for this book:
cd : change directory; this command allows you to go into different folders.
So I want my command window to be at my Java_Practice folder, which is located in the desktop directory. First, we need to travel to desktop using the following command:
cd desktop
Lets type this command into the window:
After hitting Enter, notice how the file path is now C:\Users\Faith\ Desktop . This means our command worked and successfully moved into the desktop folder.
Now we want to go inside the Java_Practice folder. We use the same command again but with a different folder name:
Great, now the command window's file path is C:\Users\Faith\Desktop\ Java_Practice . It means we successfully moved the command window over to the Java_Practice folder.
Well learn more commands to run Java programs in the next chapter.
Section 1.2: Setting up Java
Option 1: Setting up Java on Your Computer
Our current command window doesnt have the tools to understand Java, so we can't run Java programs yet. Therefore, we need to download the kit that will help our window understand. This kit is called JDK (Java Development Kit).
This book will use JDK version 8, but I recommend you use the latest version available on Oracle's website, which is JDK 15 currently: https://www.oracle.com/java/technologies/javase-jdk15-downloads.html
It shouldn't matter which JDK version you use, as the core programming concepts are the same for all Java versions. If you want to download older versions, Oracle requires an account sign-in. The latest version doesn't. Here is a general link to all JDKs available for download:https://www.oracle.com/java/technologies/javase-downloads.html