Search Java Programs

Thursday, January 7, 2010

Java variables

Command line variables provide a simple way for the user to pass data and manipulate the runtime of the application. Command line variables are space delimited and multi word strings need to be properly quoted or escaped. See your operating system or terminal documentation for exact syntax (usually surrounded with "" or before the space).
Create a new file CommandLineEx.java

class CommandLineEx {
public static void main(String[] args) {
System.out.println(args[0]);
}
}

This code functions exactly like HelloWorld.java except the printed string comes from the first argument passed to the program. All other options passed on the command line are ignored. See the for loop example for a more complete example of parsing command line variables.

Basic Variable Types

Java variables are typed and must be declared before use. Java has 8 primitive data types. A byte is an 8 bit signed integer with an allowable value between -128 and 127. A short is a 16 bit signed integer with an allowable value between -32738 and 32767. An int is a 32 bit signed integer with an allowable value between -2,147,483,648 and 2,147,483,647. A long is a 64 bit signed integer with an allowable value between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.
Create file BasicDataIntEx.java

class BasicDataIntEx {
public static void main(String[] args) {
byte b = 5;
byte b2;
short s = 48;
short s2;
int i = 100;
int i2;
long l = 1000;
long l2;

i2 = i / b;
l2 = l * s;
System.out.println(i2);
}
}

First initialize 2 variables each of byte, short, int long types, and perform some math operations on the values and print. Math operations cannot be stored into byte or short types, but they can be used in mathmatical operations stored into int or long types.
Adjust BasicDataIntEx.java so the math operations and output are:

i++;
l--;
i+=10;
l-=100;

System.out.println(i2);
System.out.println(l2);
System.out.println(i);
System.out.println(l);

When an integer style variable is followed by ++ the variable is incremented, and -- it is decremented. If these statements happened to be part of an equation a = b++; a would contain the value of b before the incrementing. Also a = --b; a would be one less than the value of b, but b would remain unchanged. The code i += 10 is shorthand for i = i + 10; and the code l-=100 is similar shorthand for l = l - 100.

The rest of the Java primitive data types are, float which is a 32 bit single precision IEEE 754 floating point number. Double, a 64 bit single precision IEEE 754 floating point number. The char, a single 16 bit unicode character. And boolean which is the standard true/false boolean type.
Create the file FloatDoubleEx.java

class FloatDoubleEx {
public static void main(String[] args) {
float pi = 3.14f;
float r = 4f;
float c;

c=pi*(r*r);
System.out.println(c);
}
}
You might notice this is simply the formula to find the circumference of a circle. Floating point numbers should be denoted with a trailing f. Now change the variable types to double. Also change the trailing f to a d to denote double.
Create a file CharBooleanEx.java

class CharBooleanEx.java {
public static void main(String[] args) {
char a = "b";
boolean b = true;
}
}
We don't do much with these at this point but show you how to declare them.

1 comment:

Website Design by Mayuri Multimedia