Java Variables

Java variables come in two types:

Primitive

Object Reference

Primitives hold fundamental values (simple bit patterns including integers, Boolean, and floating point numbers.

 

Object references hold, well, references to objects.

1. Variables must have a type

2. Variables must have a name

A variable is similar to a cup or a container. Each cup holds a value.

The sizes for six numeric primitives in Java are byte (8), short (16), int (32), and long (64).

You can assign a value to a variable in several ways:

1. Type a literal value after the equals sign

2. assign the value of one variable to another (x=y)

3. use an expression combining the two (x = y + 32)

Here are some examples of literal values that have been marked in bold:

int size = 28;     -declare an int named ‘size’, assign it the value 28

char initial = ‘j’;     -declare a char named ‘initial’, assign it the value ‘j’

double d = 465.783;     -declare a double named ‘d’, assign it the value of 465.783

Boolean isCrazy;     -declare a Boolean named ‘isCrazy’ (no assignment)

isCrazy = true;     -assign the value ‘true’ to the previously-declared isCrazy

int y = x + 234     -declare an int named ‘y’, assign it the value that is the sum of whatever x is now plus 234

Android App Development with Eclipse, the Android SDK, and more Java

So my foray into Android App development has begun and I’m happy that I have eclipse up and running with the Android SDK. I have a virtual NexusOne up and running my first Hello World App. The only real snag I encountered was being unable to build an .apk and run my first few test apps because of the error: SDK does not have any Build Tools installed. I went to C:UsersJasonandroid-sdks and found that the Build Tools directory was empty (despite running the SDK manager and re-installing the Build-Tools component.) This was resolved by re-downloading the SDK, extracting the zipped files to C:Downloadsadt-bundle-windows-x86_64-20130522 and then manually copied the contents of the Build Tools folder from the unzipped location to my c:users directory.

HelloWorld

I’ve also progressed in my Java studies to become better at understanding and looking for what code will compile and which code will produce a compiler error. Also I’m becoming more comfortable with string arrays, getting java to find out how many words are in each list, generate random numbers that are constrained to a random number within the array, and building a string phrase using words within the arrays. For example:

String[] cats = {“Tricksie”, “Gunner”, “Bear”, “Apache”, “Esmeralda”};

int x = cats.length;

int rand1 = (int) (Math.random() * x);

String phrase = cats[rand1] ;

System.out.println(phrase + ” is a name for a cat.”);

– The first output could be: Bear is a name for a cat.

– The second output would probably be a different name and so on.

The logic of while and if methods can get tricky when creating an application that will output results when checking on conditions of your variables. For example,

class Test {

public static void main(String [] args) {

int x = 0;

int y = 0;

while ( x < 5 ) {

x = x + 1;

y = y + x;

System.out.print(x + “” + y +” “);

x = x + 1;

}

}

}

In this instance, the output would be:

11 34 59

If x starts out as a 0, then it passes the condition of being < 5 so it continues. x is added to 1 so it’s value = 0 + 1 = 1. It is told to print out it’s value as 1 and then add another 1 onto itself, and so on until it’s value is greater than 5, at which time the output ceases due to while condition no longer being true.

While, do-while, and for loops always has to pass a conditional test, or an expression that results in a boolean value; either true or false.

 

 

My first Serious Business Java App

OK so this java application is not so serious, but it does include a class with a main(), an int and a String variable, a while loop, and an if test. A little more polish and I’ll be building that brewery back end in no time.

BeerSongCode

And the output when the class/app is run:

 5 bottles of beer.
 You take one down.
 You pass it around.
 4 bottles of beer on the wall.
 4 bottles of beer on the wall.
 4 bottles of beer.
 You take one down.
 You pass it around.
 3 bottles of beer on the wall.
 3 bottles of beer on the wall.
 3 bottles of beer.
 You take one down.
 You pass it around.
 2 bottles of beer on the wall.
 2 bottles of beer on the wall.
 2 bottles of beer.
 You take one down.
 You pass it around.
 1 bottle of beer on the wall.
 1 bottle of beer on the wall.
 1 bottle of beer.
 You take one down.
 You pass it around.
 No more bottles of beer on the wall.

I recommend the book Head First Java if you want to learn to write Java applications.