Beginners Java Programming Tutorial – Datatypes

December 3, 2008 at 4:54 pm 1 comment

We also have some excellent Beginners Programming Java tutorials that use high quality narrated videos and practical working files to teach the fundamentals of programming in Java Beginners Java Tutorial Videos – Programming

.

Java – Datatypes and Variables

Beginners Java Tutorial on Datatypes and Variables, ideal for the novice who is just starting to learn programming in JAVA

In order to program effectively in Java, you need to understand a little about the different datatypes in the language. A variable in Java can essentially be one of two things: a primitive type or an Object reference. To create efficient programs, you need to think about which types to use, as this will have a direct effect on performance.

The Java language is ‘strongly typed’, which means that when you declare a variable, you have to specify the type:

int myNum = 1;
String myString = "hello";
Object myObject = new Object();

The advantage to this is that you have a degree of control over the underlying implementation (and therefore efficiency) of your projects. To make informed choices about datatypes, let’s look at the ways in which they are represented in memory.

Primitives

The Primitive types in Java are int, char, boolean, byte, double, float, long and short. Primitive type variables can be given a value when you declare them, and the values are stored in stack memory:

int myNum = 1;
//in this case the value 1 is stored on the stack

In contrast, reference types are stored in heap memory, with only the Object reference on the stack (more on this below). You don’t need to understand the details of how your variables are stored in the computer’s hardware to program efficiently, as there are simple practices that will minimise the amount of memory that you use. In general, primitive types use less memory than Objects.

When it comes to choosing between primitive types, you should also try to minimise memory usage, e.g. for integer arithmetic: an int uses up less memory than a long; for floating point: a float uses less than a double – naturally there is a trade-off involved in this choice, since the larger types offer a greater range of possible values.

Note: The primitive types also have associated Wrapper classes (e.g. Integer). As well as storing the value (of e.g. an int), variables of these types can have methods called on them. However, being of the reference type, they occupy space in heap memory, and as such you should avoid using them unless you have specific reasons for using their associated methods.

Reference Types

When you create an Object, the JVM allocates a section of heap memory for it, with the Object reference stored on the stack. In this example:

Object myObject = new Object();

your myObject variable is essentially a pointer to a location in memory. The data etc contained within your Object is stored at this location. Given that this happens every time you create an Object, you can imagine that the amount of memory used up by your programs will quickly accumulate. However, there are more simple measures you can take to minimise this.

Garbage Collection

The JVM uses ‘garbage collection’ to free up areas of memory that are no longer being used. A location used by an Object in your program will be considered a candidate for garbage collection, if there are no longer any Object references pointing to it. For this reason, you need to think about where you declare variables, and for how much of the processing they need to remain available.

For example, if you consider the following trivial example:

//print the string backwards if it's at least 10 chars
String myString = "blahdeblahdeblah";
String backString = "";
if(myString.length()>=10)
{
	for(int i=myString.length()-1; i>=0; i--)
	{
		backString += myString.charAt(i);
	}
	System.out.println(backString);
}

Providing you don’t need the backString variable further down in your program, it is better to declare it inside the ‘if’ statement:

//print the string backwards if it's at least 10 chars
String myString = "blahdeblahdeblah";
if(myString.length()>=10)
{
	String backString = "";
	for(int i=myString.length()-1; i>=0; i--)
	{
		backString += myString.charAt(i);
	}
	System.out.println(backString);
}

The backString variable is now ‘local’ to the conditional ‘if’ statement; once that statement has finished executing, the variable is no longer needed and is therefore garbage. In the first example, the variable had greater ‘scope’, and remained available longer than necessary, using up memory. You can also use this principle of ‘localising’ variables with the primitive types.

Note: The syntax for some reference types such as String and Array can be confusing at first – although these look the same as primitive types in that you can assign a value to them as you declare them (rather than using the keyword ‘new’, as with most other classes), they are Objects and occupy memory as such.

Next Java Tutorial – Methods and Control Structures

Entry filed under: Java. Tags: , , .

ActionScript 3 Tutorial – Using HTML and CSS Beginners Java Tutorial – Methods – Control Structures

1 Comment Add your own

Leave a comment

Trackback this post  |  Subscribe to the comments via RSS Feed