Variables must have a type and also must have a name. It needs a name so you can use it in the code. Variables hold primitives and references to objects. There is actually no such thing as an object variable. There’s only an object reference variable. An object reference variable holds bits that represent away to access an object.
Friday, January 23, 2015
Friday, January 16, 2015
Head First Java Chapter 2
A class is a blueprint for an object. It tells the virtual machine how to make an object of that particular type. You need two classes to create and use a object. One class for the type of object you want to use and a tester class to test your class. The tester class is where you put your main method.
A java application is basically objects talking to other objects.
A java application is basically objects talking to other objects.
Monday, January 12, 2015
Head First Java Chapter 1
I learned that a source file holds one class definition and that it represents a piece of the program. A class has one or more methods and the methods must be declared inside a class. Method is a set of statements, their like functions or procedures. Everything goes in a class. Running a program means it tells Java Virtual Machine to load a class and start executing its main ( ) method. Also java has three standard looping constucts, while, do-while, and for.
public class BeerSong {
public static void main(String[] args) {
int beerNum = 99;
String word = "bottles";
while (beerNum > 0)
{
if (beerNum == 1)
{
word = "bottle";
}
System.out.println(beerNum + " " + word + " of beer on the wall");
System.out.println(beerNum + " " + word + " of beer");
System.out.println("Take one down.");
System.out.println("Pass it around.");
beerNum = beerNum - 1;
if (beerNum > 0)
{
System.out.println(beerNum + " " + word + " of beer on the wall");
}
else
{
System.out.println("No more bottles of beer on the wall");
}
}
}
}
This is the beer song it works by launching the main class and it has 99 bottles of beer on the wall. The code will count all the way to 0. Wheb it gets to 1 bottle on the wall it changes bottles to bottle so it is grammercally correct. Then the code is correct and there is no more issues.
Subscribe to:
Comments (Atom)