Lesson 2 – Primitive Types

Ok! Time to write some code.

Downloads

To get started you can use the basic starter project and follow the steps to compile the code examples for the lesson.

Create a package

To create a new package for this lesson right click on the starter project and select -> New -> Package (or use cmd/ctrl N and select package)

NewPackage

You can now name your package. For example com.codenerve.beginner.lesson2 and click ok.

Create a new class file

Now, we’re going to create a new class file to run some code. To do this right click on the package you have created and select -> New -> Class (or use cmd/ctrl N and select Class).

NewClass

Name the Class PrimitiveTypes and in the stubs section check the Main Method creation check box:

CreateClass

Primitive Types

You will now see an empty class with the an empty main method. At this point we are going to create some fields of different Primitive Types.

Fields are created outside of a method but in side the class declaration:

ClassFields

By default Fields are available to all methods inside this class as well as other classes in the same package. They may also be available outside the package using the keyword Public. But for now we are going to just use the default.

int

Lets begin by creating a new field of type int called ‘integer’. Insert the following code into the class:

To access this field from within the main method we also add the static keyword because the main method is static also. We’ll learn more on this in later lessons.

An int represents a whole number and is 32-bits long. That means it has a minimum value of -231 and a maximum value of 231-1.

Later in this lesson you will see other number types that are smaller or larger in size and therefore have a smaller or larger range of possible values.

boolean

Let’s continue to create some more fields each time underneath the last. This time a boolean called ‘bool’:

A boolean has only two possible values true or false.

char

A character:

The char data type is a single 16-bit Unicode character. This means it has 65,535 possible values including ‘a’ to ‘z’, punctuation and numerical values are all valid here.

byte

A byte

The byte data type is 8-bits. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory, in an application where memory saving is necessary (for example an applet or mobile application).

short

A short:

The short data type is a 16-bit integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). Like a byte this can be used instead of an int to save memory

long

A long:

The long data type is a 64-bit integer. The long has a minimum value of -263 and a maximum value of 263-1. Use this data type when you need a large range of values larger than that of an int.

float

A float:

The float data type is a 32-bit floating point. Use a float (instead of double) if you need to save memory.  This data type should not be used for precise values, such as currency.

double

A double:

The double data type is a 64-bit floating point number. For decimal values, this data type is the default choice. As mentioned above, this data type should not be used for precise values, such as currency. For accurate number values use BigDecimal.

So what does this all mean?

That depends on your level of expertise. At the minute you’ll probably use double, boolean, string (coming up shortly!) and int for 99.9% of your needs. And for now, that’s fine. Just be aware of the rest of the primitive types and their pro’s and cons ( Such memory savings and their ranges).

Default Values

Ok, let’s get some code running. Adding the following code snippet will enable you to see the default values for all the fields you have created above:

Output

To run right click on the class. Select Run as -> Java Application or  click the Run Button 

Output:

Assigning values

Ok we’ll that’s all well and good but we probably want to assign values that mean something to us and are useful to our program. Doing this is easy with the simple = operator.

Play around with the ranges of each value. If you try something out of range the code will not compile and will show an error:

error1

error2

Next up we’ll  learn some more interesting operators you can use to alter and compare the values you have set.

Next: Basic Operators

Menu