Java For Beginners #2 – Data Types, Variables and Arrays

Jan 18, 2016
avesdesign

If you’ve already read the first part of Java for Beginners, it’s time for you to learn more about data types, variables and arrays in Java. Almost all programming languages use data types, variables and arrays, and you need to know them well before programming because you can’t really write programs without knowing how to use different data types, variables and arrays.

If you’re familiar with any kind of programming you probably already know what data types, variables and arrays are all about. But for those of you who doesn’t have any prior experience in programming, this article will hopefully help you understand the different data types, variables and arrays in Java and how you can use them in your Java programming.

Data Types, Variables and Arrays in Java

Java For Beginners – Data Types, Variables and Arrays

Data Types in Java

Data type specifies the type and size of values that can be stored in an identifier. There are different data types that allow you to select the data type that’s most appropriate to the needs of your application. Since Java is an object oriented programming language, it cannot go without data types. In fact, Java has bunch of data types, some of them are primitives and some of them are non-primitives. These are the data types in Java:

Primitive data types – These include: boolean, integer, character and floating point
Non-primitive data types – These include: arrays, classes and interfaces.

So without any further ado, let’s learn more about different data types in Java:

Data types in Java – Boolean

Boolean is a data type that can store values with only two states: true or false (1 for true, 0 for false). It’s mostly used when you need to do some logical operations like determining whether a condition is true or false.

Data types in Java – Integer

Integer is probably the most used data type in Java. It can store only whole numeric values and it has a very wide range. However, because it can store only whole numbers, you can’t store characters in it. Here’s a table with the data types and their ranges of values that can be stored in them:

Data Type Size Range
byte 1 byte −128 to 127
short 2 bytes −32768 to 32767
int 4 bytes −2,147,483,648 to 2,147,483,647
long 8 bytes 9,223,372,036,854,775,808 to
9,223,372,036,854,755,807

Data types in Java – Character

Character does what it says, it stores characters. It has a size of about 2 bytes and it can hold only one character (because of the unicode sets). Its maximal value is ‘uffff’ (or 65,535) and its minimal value is ‘u0000′ (or 0).

Data types in Java – Floating point

Floating point is another primitive data type in Java and it’s most used when you want to store numeral value with fractional part (integer stores only whole numbers). The single floating point has a size of 4 bytes and the double floating point has a size of 8 bytes. These are their ranges:

Data Type Size Range
float 4 bytes 3.4e−038 to 3.4e+038
double 8 bytes 1.7e−308 to 1.7e+038

Variables in Java

Variables in Java (and in other programming languages) are locations in memory that can hold values and before assigning any value to a variable, you must declare it first. Java has three kinds of variables: instance variable, class variable and local variable.

Variables in Java – Instance variables

Instance variables are mostly used when you need to define attributes or the state of a particular object. They’re used to store information that’s needed by multiple methods in the objects.

Variables in Java – Class variables

Class variables are used for communicating between different objects in the same class or keeping track of global states. They’re are global to the class and to all the instances of that class.

Variables in Java – Local variables

Local variables are mostly used  in methods as temporary variables or inside the programming blocks as counters. They’re used to store information that’s needed by a single method.

Arrays in Java

Array is basically a variable that can store multiple values with the same data type. Array variables indicate the type of object that the array holds. Array can be one dimensional, two dimensional or multidimensional. If you want to use an array in your program, you need to declare it first. To do this you should write something like:


double[] myArray;
or
double myArray[];

The first example is the one that’s preferred by programmers, but it’s up to you which one you choose to use in your programs. After declaring your array, you can create it this way:


double[] myArray = new double[10];

What you do here is creating an array that holds 10 values which you can ‘add’ later in your program. If you want to ‘fill’ your array with specific values, then you can also create it this way:


double[] myArray = new double{value0, value1, value2, ..., valueN};

Here’s a simple program that shows how you can create and handle an array:


public class MyArrayTest {
public static void main(String[] args) {
// Declaring the array
double[] myArray = {0.5, 1, 1.5, 2};
// Printing the array
for (double elements: myArray) {
System.out.println(elements);
}
}
}

So these are the basic explanations of data types, variables and arrays in Java. Hopefully they’ll be helpful for you