Data Types

This document was written by CS 290W TA Joshua Kay and was last modified

So, what have we learned so far? We have talked about Classes, Objects, and Inheritance - which we know to be the basis of Object-Oriented Programming.

We know how to write and compile a simple java program which will run as an Applet. We know about for, while, and do...while loops.

We even know how to write clear code, and a little bit about LayOuts and Buttons and even Action Events.

So, let's learn about Java Data types. There are 8 basic data types. Data that is stored in these variables is written directly into a memory location, and there is no additional object information stored with it. Here they are...

TypeDeclaration KeywordSize
bytebyte8 bits
short integershort16 bits
standard integerint32 bits
long integerlong64 bits
floating-pointfloat32 bits
double-precisiondouble64 bits
characterchar16 bits(UNICODE)
booleanboolean1 bit

For CS 290W we will only use int, float, boolean, and char. Now, it is possible to convert between these data types.

	int x=129;
	float y=(float)x;
	long z=(long)x;
	//etc...
This is called simple typecasting. Note that you can't cast a boolean into a numeric type - the compiler won't allow it. Boolean holds one value, which is either 'true' or 'false'

You do take the chance of losing some precision when you switch between data types - so be careful if you are writing a program which needs to be precise.

We need to talk about variable names. Variables are int, char, float, etc. - we need to name them something, right? However, there are 47 keywords reserved by the compiler to indicate different operations - these words should NOT be used to name classes, variables, or constants... here they are...

abstract, boolean, break, byte, case, catch, char, class, const, continue, do, double, else, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, null, package, private, protected, public, return, short, static, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while

So, don't use any of these keywords when you name a variable - it can lead to hours of headaches. How, then, do we name variables?

Java variable, method, and class names can begin with either a letter, a dollar sign($), or an underscore(_). Following that are any combination of characters and numbers - but they MUST begin with either a letter, $, or _.

Now, there are several naming conventions in Java, which tend to be followed by the Java programming world. Classes generally begin with an uppercase letter. Methods and variables usually begin with a lowercase letter. If you want to use a multi-word phrase to name a variable or method, glue them together, and make the second and consecutive words uppercase as their first letter. Ex:

	class DogStuff
	{
		int howManyDogs=0;
		int howManyPuppies=0;

		void washDog()
		{
			// what a mess!
		}
		void petDog()
		{
			// nice doggie!
		}
	}
Notice that method names tend to be action verbs... That is, the name of the method should say what the method is going to do.