Software Development Basics – Part 1: Variables and Data Types

In the beginning, God created… the integers, floating point numbers and the characters and strings…

 

In this post you will learn data types supported in Java – that you can and will use in your code – and the ways to initialize them, valid values, etc. This is important because you will be handling different types of data – integers, floating point numbers, characters – always in your code.

 

First of all.. what is a variable? A variable is space in memory that is reserved for you to store data of a specific type. You can change this value, manipulate it or do whatever you want with it.

 

Primitive data types

 

So did you think that I was kidding with the opening quote in this post? Data types are primitive! They go back to the beginning of time… The truth is that they are called Primitive Data Types because they are already predefined in the language. This means that you can use them from the beginning and that they use reserved keywords.

 

In Java and many other programming languages, you need to declare and initialize the variable and assign it the data type. You can not change the type of the data that is going to be stored in your variable.

 

In general terms, you will declare your variables like this:

 

[access_type (optional)] data_type name = value;

 

For example, if you want to use an integer value of 10 you will need to do this:

int firstNumber = 10;

 

(Name convention: there are conventions used to name your variables and methods. The one that I like is called camel-case: first letter in lower-case and the first letter of each new word in upper-case, for example: thisIsMyVariableNameInCamelCase).

 

Also it is IMPORTANT to give your variable name a descriptive name. For example:

 

String firstName = “John”;

 

NEVER EVER IN YOUR LIFE give a variable a name of a character in a Anime series PLEASE (I saw it done at my workplace and I just need to tell you that this guy was fired.

Access types will be covered in later posts, so for now forget about them.

 

Java has eight primitive types that can be divided into 4 categories. Each data type has a predefined size and type of value it can store. This means that you need to know in advance the maximum or minimum value that your data can have. (Which can sometimes be a problem)

 

 

Family Type Size Range and notes Default Value
Integer Numbers byte 8 bit -128 to 127 0
short 16 bit -32,768 to 32,767 0
int 32 bit -231 to 231-1 0
long 64 bit -263 to 263-1Use an L after the number to identify it as long. 0L
Floating Point Numbers float 32 bit Single-precision 32-bit IEEE 754 floating point. It should never be used for precise values.
Use an f after the number to identify it as float.
0.0f
double 64 bit Double-precision 64-bit IEEE 754 floating point. Generally the default option for floating point. It should never be used for precise values.Use a d after the number to identify it as double. 0.0d
Boolean boolean 1 bit true – false false
Text char 16 bit Single Unicode character. It should be surrounded by single quotes: ‘a’ ‘\u0000’

 

You don’t need to memorize the minimum and maximum values of each of these data types but you need to have in mind that a limitation in its size exists.

 Special notes

 For Integer values you can have decimal, hexadecimal and binary representations:

 

int decimalValue = 31;

int hexValue = 0xA2;

int binaryValue = 0b1111;

For floating point numbers you can use exponent notation:

double exponentNotationNumber = 3.453e2;

Strings

 There is another data type widely used but it is not a primitive data type. I am talking about Strings – a group of characters that represent a string of text.

 When you want to declare a String, you need to follow the previous rules and you need to add your text between double quotes:

 

String stringName = “This is a string”;

 

However, when you do this, a new String object is created. For now, you need to know that every time you create a new string or modify an existing one, you create a new object and use more memory. You need to be careful with Strings. We will talk about them more in detail later.

 

Now what?

 

You can create a class and try to create some variables of each type. Then you can try to assign different values and even incorrect ones to see what happens. You can check my Hello World post to see how to create a simple class.

 See you next post, it will be about operations that you can do in the code. The fun will begin soon I promise.

 

** Questions? Leave a comment or email me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s