Java - Command Line Programs
Written by Alex Armstrong   
Wednesday, 30 May 2012
Article Index
Java - Command Line Programs
Data Types
Try It Out - Java Guess

Java Data Types

Before we can move on to look at the more exciting details of Java, we have to clear up some more of the basics.

In particular, you need to know about what sorts of simple variables and types of data Java supports.

In Java every variable is specified to store just one type of data. If a variable is declared as being a string type which can only hold text then trying to use it to store a number will cause an error.

This is may seem to be a silly idea and just a way of generating needless error messages, but the idea is that it adds a check on what you are doing. The argument goes that if you start out with a variable that is to be used to work with text and you try to store a number in it then this must be an indication that you have made a mistake and are confused about what your program is doing. 

This approach is called "strong typing" as opposed to weakly typed or dynamic languages.

In Java all variables have to be declared using:

type name

where type is one of the standard variable types -

 

 

Type

byte

1 byte

integer

short

2 byte

integer

int

4 byte

integer

long

8 byte

integer

float

4 byte IEEE floating point

double

 

8 byte IEEE floating point

char

 

2 byte Unicode character

boolean

 

1 byte true/false

 

Don't worry too much what the different types are all about in any detail as they are easier to understand when you encounter them in action. Essentially you have four different sizes of integer values, two types of floating point or fractional values, a character type and a Boolean which can be true or false.

For example

int count, total;

defines two integer variables and

float x,y,z;

defines three single precision floating point numbers.

There are some other tricks of the trade you need to know. When you use a numeric value then it is treated as a decimal number:

total=1234;

but any number starting with a 0 is treated as an octal constant and any starting with 0x is treated as a hex constant. This can be a source of errors so take care not to include a leading zero. For example

System.out.println(10);

displays 10 on the console but:

System.out.println(010);

displays 8. The difference is that 010 is 8 in octal.

A character constant is surrounded by single quotes, e.g.

char c = 'a';

Notice that you can only specify a single character in this way. That is, 'ab' is incorrect and generates an error.

If you want to work with a set of characters then you need to use a string constant (more of strings later) that is surrounded by double quotes. For example:

'abc'

is illegal but

"abc"

is perfectly OK. This is a particular problem if you already know another langauge such as JavaScript where ' and " mean the same thing.

You can also initialize variables when you declare them as in:

char c=’a’;

which both declares c to be a char and stores the character a in it.

If you want a real example of variables in use try changing the main method in our earlier example to read:

public static void main(String args[]){
 int a,b,c;
 
a=10;
 b=23;

 c=a+b;
 
System.out.println(c);
}

 

Notice that you can put more than one instruction on a line; as long as they are separated by semicolons, Java treats them as separate instructions.

In fact Java really doesn’t care about program layout and strips line feeds and white space out of a program before it compiles it. Of course, this isn’t an excuse to write messy looking, difficult to read, programs - use good layout at all times and never take a short cut just to save some typing.

Operators

When it comes to operations with variables you can use the usual +.-,* and / symbols to mean add, subtract, multiply and divide.

You can also use ++ and - - to mean increment and decrement.

For example, after:

a=10;
b=10;
a++;
b--;

a contains 11 and b contains 9.

An advanced detail is when the increment or decrement actually occurs. If you increment or decrement a variable in an expression the value that is used depends on whether you place the ++ or - - in front or behind the variable. If the ++ or - - is in front then the operation is done first and if behind it is done after.

For example:

a=10;b=++a;

stores 11 in b and in a, but:

a=10;b=a++;

stores 10 in b and 11 in a.

If you find this confusing then the moral is - don’t get carried away with the use of ++ and - -

It is very easy to create complex and confusing expressions using them.



Last Updated ( Wednesday, 07 November 2012 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.