Showing posts with label Tutorial. Show all posts
Showing posts with label Tutorial. Show all posts

Monday, August 6, 2012

Getting a Java Compiler and IDE

Before you can program anything in Java, you are going to need two pieces of software. Both are completely free. The first is the "Java Development Kit" or "JDK" and the second is an editor called "Eclipse". Both are available by download from their respective websites. Below is a guide for installing each.

Getting the Java Development Kit (JDK)

The first step to programming in Java is to install the JDK. The kit provides programmers with the tools necessary to compile, debug, and run their own java programs. It is possible that you already have the JDK from some other program, but it is best to just get the newest version before starting.

The Java Developement Kit can be downloaded from the Oracle website. Once you are there, you should see something that looks like this:



This website changes from time to time, but the download you want is almost always on the left. It's the one that says, "Java Platform (JDK)" and a version number and nothing else. Do not get JavaFX or NetBeans or JavaEE. Click "download," as it is highlighted above, and you will be taken to a new page. Scroll down to the first box that looks like this:


In order to download the JDK, you must first accept the Oracle Binary Code License Agreement. If you agree to the terms listed in the link, click the radio button next to "Accept License Agreement." Then, choose the download link for your operating system. Each operating system has two version, "x86" and "x64". These two type have to do with what kind of operating system you have, 32-bit or 64-bit, respectively. If you have a 32-bit machine (At this date, most people do and 32-bit works on all machines), then click the "x86" link.

Once the JDK installer is downloaded, run the executable and follow the installation wizard. It shouldn't matter where you install the JDK, but the defaults are probably the most safe. For the version we downloaded, it was as simple as "next", "next", "finish". However, after the JDK finished installing, the wizard offered an installation of JavaFX. If this happens for you, close the JavaFX installer and proceed in this tutorial.



Getting Eclipse, an Integrated Development Environment (IDE)

Imagine you want to write a paper. You would probably start by opening your favorite text editor. Good text editors usually have helpful features like spell checking, fonts, page numbers, and printer support. Similar to a good text editor for writing papers, you will want a good Integrated Development Environment (or IDE) to write a program. The best IDE that I can recommend is Eclipse. It has real-time error checking, a nice auto-formatter, extremely helpful auto-complete options, and it suggested fixes for current bugs. You can even access all of the java documentation through eclipse without having to open a browser, just mouse over some code you don't understand and it may be able to help. Eclipse takes a little extra time to learn, but it's worth it for the time you'll save while programming. Here's how you get it:


You will see a page with lots of downloads. Find the one that looks like this:


and click it. Doing so will bring up a page like this:


As you can see, the downloads are on the right. Choose the download that fits for you. And you will see a download and mirror page that looks like this:



The easiest download is the one right at the top that looks like the image above. Just click it and download. If you would like to find a mirror, scroll down for a list of alternate locations to download from.

When the download is complete, you will need to locate and unzip the file. If you have Windows XP or Windows 7, you can right click and "extract all". Mac computers should have an equivalent. If you use Linux, you probably already know all about this. If you are on a computer that does not have an unzip option, or if you are unhappy with you current archive manager, for instance if WinRAR is bothering you to register, download 7-zip and use that.

Eclipse comes in a stand-alone format; there is no installer. Inside the folder you extracted there should be another folder named "eclipse". Put the "eclipse" folder, with everything in it, where ever you want it, then open it up and run "eclipse.exe". If you want a shortcut, take the chance to create one now.

Once you have downloaded and installed the JDK and Eclipse, and Eclipse is running, you are ready to proceed to the next section of this tutorial.



Sunday, August 5, 2012

Primitive Data Types

I don't even see the code.
All I see is integer, double, String.
Before we can continue on to doing more interesting things with Java, we first need to take a look at the types of information that Java can work with. The vast majority of the operations in Java depend on a small set of basic data types, from which everything else can be built.

Integers, declared as int, can deal with positive and negative numbers with no fractional part. When doing math with them, if you would get anything after the decimal place, it is ignored completely. No rounding occurs. The int type is 4-bytes, and can store any number from -2,147,483,648 to 2,147,483,647. There are also variants of this type that can deal with a smaller or wider range of numbers, namely byte, short, and long.

Floating-point numbers, declared as double, can deal with positive or negative numbers, and can include a fractional part. Unlike the integer types, however, their math is inexact. Due to their inner workings, there are some numbers within their range that they simply can't represent. Unless you're doing something strange, though, they should behave very close to how you would expect. The double type is 8-bytes, and can store an extremely wide range of values - the minimum and maximum both have 308 digits, and for obvious reasons, I won't list them here. There is also a smaller variant of this type, namely float.

Boolean values, declared as boolean, can hold one of two different values: true or false. While that may not sound terribly useful at first glance, almost every decision-making structure in Java is based off of boolean values and comparisons between them. Because of this, you'll surely become very familiar with them throughout these lessons. For obvious reasons, there are no larger or smaller variants of this type.

Characters, declared as char, can hold a single Unicode character. They make up the basis of Strings in Java, which is the data type that holds text, such as "Hello world!". The good news is, you probably won't have to use them much or maybe even at all, because Java's String support is so good. A char is 2-bytes, and can hold any value from 0 to 65535, but is displayed as the corresponding letter or symbol.

Lastly character strings, declared as String, can hold any amount of text. They're quite ubiquitous, and are highly useful. While they're not technically a primitive type, I'm including them here because they're awesome (and they behave like primitives in a lot of ways).

End of Lesson Quiz

What type would you use to represent the number of times a user would like the program to do something?

[Show Answer]


What type would you use to represent whether or not the program should display error messages or simply ignore them?

[Show Answer]


What data type did we use in the previous lesson as part of the program to display Hello world! ?

[Show Answer]


What data type would you use to represent the amount of money in a bank account?

[Show Answer]


If you're working with the int type, what would the result be of 14 divided by 5?

[Show Answer]


Next Lesson: Variables

Table of Contents


Simple Java Tutorials - Table of Contents


  • Introduction

  • Chapter 1: Getting Started

  • Chapter 2: Programming Basics
    • Primitive Data Types
    • Variables
    • Expressions and Literals
    • Conditionals
    • Boolean Logic
    • Explicit Loops
    • Indefinite Loops
    • Declaring Methods
    • Using Methods

  • Chapter 3: Intermediate Programming
    • Recursion
    • Objects
    • The String Class
    • String Manipulation
    • Text User Input
    • String Parsing

(More lessons to be added when we get more of these done.)