<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5641760749505863784</id><updated>2012-02-16T13:16:14.114-06:00</updated><category term='Command Line'/><category term='Javac'/><category term='long'/><category term='JAVA'/><category term='double'/><category term='GreyM'/><category term='char'/><category term='concatenation'/><category term='main'/><category term='PATH variable'/><category term='IS-A'/><category term='String'/><category term='parameters'/><category term='subclasses'/><category term='constructors'/><category term='OO'/><category term='API'/><category term='int'/><category term='Objects'/><category term='Hello World'/><category term='for loops'/><category term='superclasses'/><category term='print'/><category term='overriding'/><category term='inheritance'/><category term='Random Numbers'/><category term='If Statements'/><category term='extends'/><category term='Operators'/><category term='Starting Off'/><category term='println'/><category term='Eclipse'/><category term='classes'/><category term='overloading'/><category term='compiling'/><category term='methods'/><category term='integer'/><category term='character'/><category term='J2SE'/><category term='byte'/><category term='boolean'/><category term='while loops'/><category term='variables'/><title type='text'>Drops of Java</title><subtitle type='html'>Drop by drop filling the coffee pot of knowledge</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://dropsofjava.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://dropsofjava.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Grey-M</name><uri>http://www.blogger.com/profile/10237241798100727258</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5641760749505863784.post-8638042410708848234</id><published>2008-01-20T13:46:00.000-06:00</published><updated>2008-01-21T17:09:52.712-06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='overloading'/><category scheme='http://www.blogger.com/atom/ns#' term='inheritance'/><category scheme='http://www.blogger.com/atom/ns#' term='subclasses'/><category scheme='http://www.blogger.com/atom/ns#' term='IS-A'/><category scheme='http://www.blogger.com/atom/ns#' term='GreyM'/><category scheme='http://www.blogger.com/atom/ns#' term='superclasses'/><category scheme='http://www.blogger.com/atom/ns#' term='overriding'/><category scheme='http://www.blogger.com/atom/ns#' term='extends'/><title type='text'>Extending the Possibilities</title><content type='html'>Alright! Hello everyone! Today we are going to cover inheritance! Inheritance is a fairly simple very powerful tool in the JAVA toolkit. A quick breakdown of what it is is that it allows you to take bits and pieces of code that are common among a group of relatable objects (Objects like say cats and dogs because they are both animals and will both have some of the same characteristics). It is similar to factoring out terms in math (or atleast that's how I see it though there are limits to this similarity that we'll get to). If you have:&lt;br /&gt;2x +2&lt;br /&gt;then you can do this...&lt;br /&gt;2(x+1)&lt;br /&gt;&lt;br /&gt;Same with our objects.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public Class Cat{&lt;br /&gt;  public void eat(){&lt;br /&gt;    //eating code&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;public Class Dog{&lt;br /&gt;  public void eat(){&lt;br /&gt;   //eating code&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Both are animals and animals must eat. So this is how we factor out in java.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public Class Animal{&lt;br /&gt;  public void eat(){&lt;br /&gt;    //eating code&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public Class Cat &lt;span style="color: rgb(255, 0, 0);"&gt;extends Animal&lt;/span&gt;{&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public Class Dog &lt;span style="color: rgb(255, 0, 0);"&gt;extends Animal&lt;/span&gt;{&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Since both the Cat and Dog classes extend animal they both will have the eat() method (The word extend is a keyword for this thing we call inheritance). This works for nearly every method that we put in animal now (We'll get to a couple of exceptions).&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public Class Animal{&lt;br /&gt;  public void eat(){&lt;br /&gt;    //eating code&lt;br /&gt;  }&lt;br /&gt;  public void scratch(){&lt;br /&gt;    //scratching code&lt;br /&gt;  }&lt;br /&gt;  public void doTrick(){&lt;br /&gt;    //trick code&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public Class Cat extends Animal{&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public Class Dog extends Animal{&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;All these methods are now inherited by Cat and Dog. Cat and Dog will both inherit everything that is inheritable from the &lt;span style="color: rgb(51, 51, 255);"&gt;superclass &lt;/span&gt;(the class from which they inherit from). That being said you don't want a Dog to inherit a meow() method so all the methods within the superclass must be useable by the &lt;span style="color: rgb(0, 153, 0);"&gt;subclasses &lt;/span&gt;(the classes that inherit everything). The subclasses will also be able to inherit the &lt;span style="color: rgb(204, 51, 204);"&gt;instance variables&lt;/span&gt;, the variables that tell you about the state of the object.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public Class Animal{&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;  String nameOfPet;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;  Int age;&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Our Cat and Dog will now have both a nameOfPet and age. But what if there are two things that both Cat and Dog do but do differently? They both play but they play differently. We have a way to handle that and it links up rather nicely to the previous post on constructors (This is also where the factoring example loses it's usefulness).&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public Class Animal{&lt;br /&gt;  String name;&lt;br /&gt;  Int age;&lt;br /&gt;  public void eat(){&lt;br /&gt;    //eating code&lt;br /&gt;  }&lt;br /&gt;  public void scratch(){&lt;br /&gt;    //scratching code&lt;br /&gt;  }&lt;br /&gt;  public void doTrick(){&lt;br /&gt;    //trick code&lt;br /&gt;  }&lt;br /&gt;  public void play(){&lt;br /&gt;    //standard play code that works for most animals&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We'll say a dog plays like most animals&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public Class Dog extends Animal{&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;We'll say a cat only likes to play with a ball on a string&lt;br /&gt;public Class Cat extends Animal{&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;  public void play(){&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;    //specific code for playing with a ball on a string&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;  }&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;That method in Cat &lt;span style="color: rgb(255, 0, 0);"&gt;overrides &lt;/span&gt;the method in Animal. The compiler will always use the most specific method when deciding which to allow the class to use. Our Cat play() method is more specific then the Animal play() method so it will use the Cat play() method. Now some different cases.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class Animal{&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;  private &lt;/span&gt;void morph(){&lt;br /&gt;    //morphing code&lt;br /&gt;  }&lt;br /&gt;  public void play(){&lt;br /&gt;    //playing code&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;public class Cat extends Animal{&lt;br /&gt;  public void play(&lt;span style="color: rgb(204, 51, 204);"&gt;Boolean haveALaserPointer&lt;/span&gt;){&lt;br /&gt;    //playing with laserpointer code&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Alright so this is what happens here:&lt;br /&gt;- our class Cat does not inherit morph(), this is because morph() is &lt;span style="color: rgb(51, 204, 0);"&gt;private &lt;/span&gt;(we'll do more with public, private, default and protected states later but right now public and private are what we are dealing with and for something to be inherited it cannot be private!)&lt;br /&gt;- our class Cat inherits play() from Animal and creates it's own play method with the parameter &lt;span style="color: rgb(204, 51, 204);"&gt;Boolean haveALaserPointer&lt;/span&gt;. This connects very nicely with constructors because we had overloaded constructors which were different constructors with different parameters but they all existed. The same goes for methods and in this case we have two methods which means we have overloaded methods. The Cat class will have both in this case!&lt;br /&gt;&lt;br /&gt;So now you have the basic breakdown of inheritance. But when do you use it? &lt;span style="color: rgb(255, 0, 0);"&gt;You use inheritance when two objects have a IS-A relationship&lt;/span&gt;. That is when some object is something else. For example a cat &lt;span style="color: rgb(255, 0, 0);"&gt;IS-A&lt;/span&gt; animal (I know the grammar isn't quite right) and a car &lt;span style="color: rgb(255, 0, 0);"&gt;IS-A&lt;/span&gt; vehicle. You don't use inheritance if the &lt;span style="color: rgb(255, 0, 0);"&gt;IS-A&lt;/span&gt; relationship doesn't work like a bear &lt;span style="color: rgb(255, 0, 0);"&gt;IS-A&lt;/span&gt; mule or a beer &lt;span style="color: rgb(255, 0, 0);"&gt;IS-A&lt;/span&gt; hotdog. Just doesn't work, they may have some of the same attributes like the bear and mule both being animals and the beer and hotdog both being something we consume but one does not stem from the other in the family tree. A beer may have something like this: Food to beverage to alchohol to beer. The hotdog may have some similarities but it does not come after beer in the family tree: Food to snack to hotdogs.&lt;br /&gt;&lt;br /&gt;Summary and extra tidbits:&lt;br /&gt;-inheritance is when one class inherits the attributes (methods, instance variables) from another class called the superclass that relates to it by a IS-A relationship&lt;br /&gt;-the class that inherits these attributes is called the subclass&lt;br /&gt;-methods can be overridden(another method in the subclass with the same name and parameters) and overloaded(same name different parameters)&lt;br /&gt;-subclasses will have the overloaded methods and the original methods from the superclass&lt;br /&gt;-inheritance can happen in many steps: Graeme can inherit from boys which can inherit from people which can inherit from ... and so on and so on.&lt;br /&gt;-you cannot change public to private when overriding but you can when overloading (we'll talk about private in a later post).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I think that covered a lot. There is more, there always seems to be more with Java. But once again I'm tired. If you have any questions or you need anything cleared up comment and I'll see what I can do for ya.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5641760749505863784-8638042410708848234?l=dropsofjava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dropsofjava.blogspot.com/feeds/8638042410708848234/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5641760749505863784&amp;postID=8638042410708848234' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/8638042410708848234'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/8638042410708848234'/><link rel='alternate' type='text/html' href='http://dropsofjava.blogspot.com/2008/01/extending-possibilities.html' title='Extending the Possibilities'/><author><name>Grey-M</name><uri>http://www.blogger.com/profile/10237241798100727258</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5641760749505863784.post-4975833632442929257</id><published>2008-01-13T22:37:00.000-06:00</published><updated>2008-01-13T23:35:25.844-06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='parameters'/><category scheme='http://www.blogger.com/atom/ns#' term='constructors'/><title type='text'>Giving Objects Personality</title><content type='html'>Today we talk more about objects! Yay! Specifically how they are constructed. Last time we made ourselves a computer object. If you recall the line that instantiated [made] the object looked like this:&lt;br /&gt;&lt;br /&gt;Computer myComputer = new Computer();&lt;br /&gt;&lt;br /&gt;and follows the format:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;ClassName &lt;/span&gt;objectName = &lt;span style="color: rgb(51, 255, 51);"&gt;new &lt;/span&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;ClassName()&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;Now &lt;span style="color: rgb(204, 51, 204);"&gt;Computer()&lt;/span&gt;  is called a constructor. This looks and acts a lot like a method but it isn't! It is a constructor! A constructor is the code that runs when a new object is instantiated [made], it customizes your object for you if you give it the proper information and have a constructor made to take that information. Now we didn't make a constructor for our last program but the compiler made one for us! The compiler automatically made the constructor for us because we didn't make a constructor ourselves and it looks something like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class Computer{&lt;br /&gt;  &lt;span style="color: rgb(204, 51, 204);"&gt;public Computer(){&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;   }&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now that's not a very good constructor, it doesn't give the object any attributes. In our last program we used methods to change instance variables we'll use a constructor to set a few new ones in this case.&lt;br /&gt;&lt;br /&gt;Note: Notice how constructors are different then methods! First constructors have the exact name of the class. Second methods have a return type (so far we have only seen void which means no return type).&lt;br /&gt;&lt;br /&gt;Now constructors can take in information and use it to change the instance variables of an object or you can just change the instance variables without taking in any information, like a default. Some examples of constructors:&lt;br /&gt;&lt;br /&gt;*we'll give our computers some different instances variables for the purposes of this example (all the following code would go within the braces for the Computer class):&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;int hardDriveSizeInGB;&lt;br /&gt;double processorSpeedInGHz;&lt;br /&gt;int numOfUSBPorts;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;public Computer(){&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;    hardDriveSizeInGB = 250;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;    processorSpeedInGHz = 2.8;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;    numOfUSBPorts = 3;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;public Computer(int hardDrive, double processor, int ports){&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;    hardDriveSizeInGB = hardDrive;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;    processorSpeedInGHz = processor;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;    numOfUSBPorts = ports;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;public Computer(int hardDrive)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;    hardDriveSizeInGB = hardDrive;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;    processorSpeedInGHz = 2.8;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;    numOfUSBPorts = 3;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now each of these is different. Which one is called is dependant on what you give the constructor in the declaration.&lt;br /&gt;Computer myComputer = new &lt;span style="color: rgb(51, 102, 255);"&gt;Computer(300)&lt;/span&gt;;&lt;br /&gt;This will call our third constructor because it satisfies the needs for it's parameters. Same works for the other two.&lt;br /&gt;Computer myComputer = new &lt;span style="color: rgb(255, 0, 0);"&gt;Computer();&lt;/span&gt;&lt;br /&gt;Computer myComputer = new &lt;span style="color: rgb(51, 204, 0);"&gt;Computer(300, 4.4, 12);&lt;/span&gt;&lt;br /&gt;They will call their respective constructors. But make sure you give the correct parameters for atleast one of your constructors otherwise it'll blow up in your face. Now if we didn't have our first constructor with no arguments &lt;span style="font-weight: bold;"&gt;the compiler will no longer make it automatically for us&lt;/span&gt;. As soon as you have one constructor made it won't bother.&lt;br /&gt;&lt;br /&gt;Note: Having more than one constructor for a class is called having &lt;span style="font-style: italic;"&gt;overloaded constructors&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Common mistake! You must make constructors with different argument lists. Meaning you can't have two constructors that just take two integers, the compiler doesn't know which one to call and it will blow up.&lt;br /&gt;&lt;br /&gt;One last tidbit, there are other things you can do within constructors like verify that what is being put into the constructor is feasible. Take this example:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Computer obsenelyHugeHD = new Computer(20000)&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;public Computer(int hardDrive)&lt;br /&gt;   &lt;span style="color: rgb(102, 0, 0);"&gt;if (hardDrive &lt;&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&lt;span style="color: rgb(102, 0, 0);"&gt;        hardDriveSizeInGB = hardDrive;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 0, 0);"&gt;    } else {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 0, 0);"&gt;         hardDriveSizeInGB = 250;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;    processorSpeedInGHz = 2.8;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;    numOfUSBPorts = 3;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;That's all for constructors for now. There is more though! But we'll cover that another day, I'm tired. If you have any questions leave a comment and I'll clear it up the best I can.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5641760749505863784-4975833632442929257?l=dropsofjava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dropsofjava.blogspot.com/feeds/4975833632442929257/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5641760749505863784&amp;postID=4975833632442929257' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/4975833632442929257'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/4975833632442929257'/><link rel='alternate' type='text/html' href='http://dropsofjava.blogspot.com/2008/01/giving-objects-personality.html' title='Giving Objects Personality'/><author><name>Grey-M</name><uri>http://www.blogger.com/profile/10237241798100727258</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5641760749505863784.post-9136248569671465981</id><published>2008-01-07T16:26:00.000-06:00</published><updated>2008-01-07T18:46:08.822-06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='constructors'/><category scheme='http://www.blogger.com/atom/ns#' term='methods'/><category scheme='http://www.blogger.com/atom/ns#' term='Random Numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='GreyM'/><category scheme='http://www.blogger.com/atom/ns#' term='main'/><category scheme='http://www.blogger.com/atom/ns#' term='OO'/><category scheme='http://www.blogger.com/atom/ns#' term='Objects'/><category scheme='http://www.blogger.com/atom/ns#' term='JAVA'/><category scheme='http://www.blogger.com/atom/ns#' term='concatenation'/><title type='text'>Getting Object Oriented</title><content type='html'>Alright! So this post is all about objects! JAVA itself is an object-oriented language meaning that pretty much everything revolves around objects. Objects are instances of something that have a current state and can do things. Such as my computer, it is on and it is inputting what I’m typing. So really objects can be about anything. Now how do we make ourselves an object?&lt;br /&gt;&lt;br /&gt;We have already made code that has the main elements in java:&lt;br /&gt;classes&lt;br /&gt;main() method&lt;br /&gt;variables&lt;br /&gt;statements&lt;br /&gt;&lt;br /&gt;Now let’s start building ourselves a small program and we’ll make it about a computer.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class Computer{&lt;br /&gt;  public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now we are going to give our computer it’s state, or in other words what it is currently with what are called &lt;span style="color: rgb(255, 0, 0);"&gt;instance variables&lt;/span&gt;.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class Computer{&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;  boolean isOn;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;  String typeOfComputer;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;  String currentScreen;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now we are going to add a couple of &lt;span style="color: rgb(51, 204, 0);"&gt;methods. Methods &lt;/span&gt;are what we use to &lt;span style="color: rgb(51, 255, 51);"&gt;control &lt;/span&gt;the &lt;span style="color: rgb(255, 0, 0);"&gt;instance variables.&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class Computer{&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;  boolean isOn;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;  String typeOfComputer;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;  String currentScreen;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;  public void turnOn() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;  }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;  public void changeType() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;  }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;  public void changeCurrentScreen() {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;  }&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;turnOn(), changeType()&lt;/span&gt; and &lt;span style="color: rgb(51, 204, 0);"&gt;changeCurrentScreen()&lt;/span&gt; are our methods but they don’t do anything... So we’ll give them something simple to do that corresponds with their name.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public void turnOn() {&lt;br /&gt;  isOn = true;&lt;br /&gt;}&lt;br /&gt;public void changeType() {&lt;br /&gt;  int x = (int) Math.random() * 3 + 1;&lt;br /&gt;  if (x == 1){&lt;br /&gt;    typeOfComputer = "Mac";&lt;br /&gt;  } else if(x == 2){&lt;br /&gt;    typeOfComputer = "Alienware";&lt;br /&gt;  } else if (x == 3){&lt;br /&gt;    typeOfComputer = "Super Computer";&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;public void changeCurrentScreen() {&lt;br /&gt;  int x = (int) Math.random() * 2 + 1;&lt;br /&gt;  if (x == 1) {&lt;br /&gt;    currentScreen = "Home screen";&lt;br /&gt;  } else if(x == 2) {&lt;br /&gt;    currentScreen = "Google";&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The turnOn() method now turns on our computer&lt;br /&gt;changeType() method now randomly changes the type of computer&lt;br /&gt;changeCurrentScreen()method now randomly changes our computer screen&lt;br /&gt;&lt;br /&gt;Couple tidbits that need to be explained...&lt;br /&gt;int x = (int) Math.random() * 3 + 1;&lt;br /&gt;&lt;br /&gt;within the method we are declaring some integer x, we’ll talk about it more later on (not today) but x only exists within the method it is declared then it dies. You can’t use it elsewhere unless you declare it again.&lt;br /&gt;&lt;br /&gt;The (int) casts the random number we make between 1 and 4 into an integer, it truncates it therefore giving us 1, 2 or 3.&lt;br /&gt;&lt;br /&gt;Now it’s nice and all having these methods but how do we use them? We have to call/invoke them.&lt;br /&gt;&lt;br /&gt;So we’ll try this...&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;  turnOn();&lt;br /&gt;  changeType();&lt;br /&gt;  changeCurrentScreen();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Won’t work...&lt;br /&gt;&lt;br /&gt;The computer does not know what you are talking about. We have the code for our method to control our object but we have no object and we haven’t actually made those controls we have just wrote the code for them (I’ll admit that sounds a little confusing for the moment).&lt;br /&gt;&lt;br /&gt;To create our Computer object we need a line of code that looks like what follows:&lt;br /&gt;&lt;br /&gt;Computer myComputer = new Computer();&lt;br /&gt;&lt;br /&gt;When we create this myComputer object it creates everything within the class so we have also made the methods/controls for this object and this object must be created before we can do anything to it so we'll put it first in the main() method.&lt;br /&gt;This is a statement that creates a new Computer object called myComputer. All these statements generally follow this format:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;ClassName &lt;/span&gt;objectName = &lt;span style="color: rgb(51, 255, 51);"&gt;new &lt;/span&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;ClassName()&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;ClassName &lt;/span&gt;is the name of the class that you want to create, the object that you want to create.&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;ClassName()&lt;/span&gt; is called a &lt;span style="color: rgb(204, 51, 204);"&gt;constructor &lt;/span&gt;and you need those brackets, we’ll do more with constructors later.&lt;br /&gt;&lt;br /&gt;Now we can fix our code and tell the computer what we want it to do and what we want it to do it to.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;  Computer myComputer = new Computer();&lt;br /&gt;  myComputer.turnOn();&lt;br /&gt;  myComputer.changeType();&lt;br /&gt;  myComputer.changeCurrentScreen();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Then there is just one last thing we need to do with our program. We did all of these changes to myComputer but what do we have to show for it? Bupkiss. So we’ll make another method to display our info.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public void display() {&lt;br /&gt;&lt;span style="font-size:85%;"&gt;  System.out.println("Is the computer on? " + isOn);&lt;br /&gt;  System.out.println("What type of computer is it? " + typeOfComputer);&lt;br /&gt;  System.out.println("What is the current screen? " + currentScreen);&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Remember that the “+” concatenates strings and the variables together.&lt;br /&gt;&lt;br /&gt;Then you just have to invoke this method after you have changed the instance variables and your good.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;  Computer myComputer = new Computer();&lt;br /&gt;  myComputer.turnOn();&lt;br /&gt;  myComputer.changeType();&lt;br /&gt;  myComputer.changeCurrentScreen();&lt;br /&gt;&lt;span style="color: rgb(153, 51, 0);"&gt;  myComputer.display();&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So our whole code ends up being:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class Computer {&lt;br /&gt;  boolean isOn;&lt;br /&gt;  String typeOfComputer;&lt;br /&gt;  String currentScreen;&lt;br /&gt;&lt;br /&gt;  public static void main(String[] args) {&lt;br /&gt;    Computer myComputer = new Computer();&lt;br /&gt;    myComputer.turnOn();&lt;br /&gt;    myComputer.changeType();&lt;br /&gt;    myComputer.changeCurrentScreen();&lt;br /&gt;    myComputer.display();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public void turnOn() {&lt;br /&gt;    isOn = true;&lt;br /&gt;  }&lt;br /&gt;  public void changeType() {&lt;br /&gt;    int x = (int) Math.random() * 3 + 1;&lt;br /&gt;    if (x == 1){&lt;br /&gt;      typeOfComputer = "Mac";&lt;br /&gt;    } else if(x == 2){&lt;br /&gt;      typeOfComputer = "Alienware";&lt;br /&gt;    } else if (x == 3){&lt;br /&gt;      typeOfComputer = "Super Computer";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;  public void changeCurrentScreen() {&lt;br /&gt;    int x = (int) Math.random() * 2 + 1;&lt;br /&gt;&lt;br /&gt;    if (x == 1) {&lt;br /&gt;      currentScreen = "Home screen";&lt;br /&gt;    } else if(x == 2) {&lt;br /&gt;      currentScreen = "Google";&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  public void display() {&lt;br /&gt;&lt;span style="font-size:85%;"&gt;    System.out.println("Is the computer on? " + isOn);&lt;br /&gt;    System.out.println("What type of computer is it? " + typeOfComputer);&lt;br /&gt;    System.out.println("What is the current screen? " + currentScreen);&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;That was a lot but it covers a massive concept, or at least a small part of a large concept. Hope this is understandable, if not tell me and I'll see what I can do!&lt;br /&gt;&lt;br /&gt;Good night!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5641760749505863784-9136248569671465981?l=dropsofjava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dropsofjava.blogspot.com/feeds/9136248569671465981/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5641760749505863784&amp;postID=9136248569671465981' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/9136248569671465981'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/9136248569671465981'/><link rel='alternate' type='text/html' href='http://dropsofjava.blogspot.com/2008/01/getting-object-oriented.html' title='Getting Object Oriented'/><author><name>Grey-M</name><uri>http://www.blogger.com/profile/10237241798100727258</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5641760749505863784.post-2225259297537746479</id><published>2007-12-20T22:23:00.000-06:00</published><updated>2007-12-20T22:57:40.417-06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='long'/><category scheme='http://www.blogger.com/atom/ns#' term='double'/><category scheme='http://www.blogger.com/atom/ns#' term='character'/><category scheme='http://www.blogger.com/atom/ns#' term='char'/><category scheme='http://www.blogger.com/atom/ns#' term='boolean'/><category scheme='http://www.blogger.com/atom/ns#' term='int'/><category scheme='http://www.blogger.com/atom/ns#' term='integer'/><category scheme='http://www.blogger.com/atom/ns#' term='GreyM'/><category scheme='http://www.blogger.com/atom/ns#' term='byte'/><category scheme='http://www.blogger.com/atom/ns#' term='variables'/><category scheme='http://www.blogger.com/atom/ns#' term='String'/><title type='text'>Would You Like Some Variables As Well?</title><content type='html'>On a quick note beforehand... how did you find the looping post? I may end up redoing it for you guys if you didn't "grok in fullness" because I may not have done it justice, the &lt;&gt; statements blogger really doesn't like and I had to rewrite the whole thing a couple times. Am searching for another method to show proper form currently (but for now examples won't be tabbed in).&lt;br /&gt;&lt;br /&gt;Alright so we have used &lt;span style="color: rgb(255, 0, 0);"&gt;variables &lt;/span&gt;already but they weren't really explained in any sort of detail. Variables are a sort of &lt;span style="color: rgb(255, 0, 0);"&gt;container &lt;/span&gt;but they are &lt;span style="font-weight: bold;"&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;picky containers&lt;/span&gt;. &lt;/span&gt;Tupperware with attitude. If you try to put something in it it doesn't like it spits it back out with a nasty error message. To declare a variable you must say what &lt;span style="color: rgb(0, 153, 0);"&gt;type &lt;/span&gt;it is and give it a &lt;span style="color: rgb(51, 51, 255);"&gt;name&lt;/span&gt;. To use a variable it must be &lt;span style="color: rgb(153, 51, 153);"&gt;initialized. &lt;span style="color: rgb(0, 0, 0);"&gt;So here is an example of a statement &lt;span style="color: rgb(255, 0, 0);"&gt;declaring a variable&lt;/span&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;int &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;x&lt;/span&gt; &lt;span style="color: rgb(204, 51, 204);"&gt;= 0&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;The int is the type of variable x is.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;x is the name of our variable.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;= 0 initializes our variable.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We can also declare a variable and initialize it in two steps:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(153, 51, 153);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;int &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(153, 51, 153);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(153, 51, 153);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;x&lt;/span&gt; &lt;span style="color: rgb(204, 51, 204);"&gt;= 0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(153, 51, 153);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;&lt;br /&gt;&lt;br /&gt;Now for our purposes at the moment our variables are going to go inside our main() method. This is for reasons that will be explained in upcoming lessons.&lt;br /&gt;&lt;br /&gt;Now you aren't just going to want to use integers. You are going to want to use many different types of variables. Here is a list of the variables for you:&lt;br /&gt;&lt;br /&gt;int, these are integers and can store positive and negative nondecimal numbers.&lt;br /&gt;boolean, these are true and false operators and must be set to true or false.&lt;br /&gt;byte, can hold values from -127 to 128 according to this &lt;a href="http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html"&gt;site&lt;/a&gt;.&lt;br /&gt;double, can hold decimal values.&lt;br /&gt;long, can hold really long nondecimal numbers, kind of like a super integer.&lt;br /&gt;char, can store characters like "j" or "a".&lt;br /&gt;String, can hold a bunch of characters strung together, like "hello there" or "ass22d&amp;amp;&amp;amp;^(*".&lt;br /&gt;&lt;br /&gt;There we are, toy with those at will, we'll get them out of the main() method next time.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5641760749505863784-2225259297537746479?l=dropsofjava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dropsofjava.blogspot.com/feeds/2225259297537746479/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5641760749505863784&amp;postID=2225259297537746479' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/2225259297537746479'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/2225259297537746479'/><link rel='alternate' type='text/html' href='http://dropsofjava.blogspot.com/2007/12/would-you-like-some-variables-as-well.html' title='Would You Like Some Variables As Well?'/><author><name>Grey-M</name><uri>http://www.blogger.com/profile/10237241798100727258</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5641760749505863784.post-4805670532583232538</id><published>2007-12-19T19:20:00.000-06:00</published><updated>2007-12-19T20:46:45.299-06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Operators'/><category scheme='http://www.blogger.com/atom/ns#' term='while loops'/><category scheme='http://www.blogger.com/atom/ns#' term='println'/><category scheme='http://www.blogger.com/atom/ns#' term='for loops'/><category scheme='http://www.blogger.com/atom/ns#' term='GreyM'/><category scheme='http://www.blogger.com/atom/ns#' term='print'/><category scheme='http://www.blogger.com/atom/ns#' term='JAVA'/><title type='text'>Our Methods are Getting Loopy</title><content type='html'>Hello, the post may be a little messed up, blogger really doesn't like &lt; pre &gt;!&lt;br /&gt;&lt;br /&gt;Today we are going to learn "loops". Loops are a way of repeating code over and over and over and so on... There are a couple types of loops in JAVA, "while", "do-while" and "for" loops. We will do "while" and the basic "for".&lt;br /&gt;&lt;br /&gt;The while loop:&lt;br /&gt;- while some condition is true the loop will run.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;while (some condition) {&lt;br /&gt;  //code to run&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;An example of this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;while (x &gt; 0) {&lt;br /&gt;  //code to run while x &gt; 0&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The for loop:&lt;br /&gt;- this type of loop is for some code that you know needs to run a certain amount of times. These are basically a while loop with a sort of counter built in.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;for (some variable; condition; increment/decrement) {&lt;br /&gt;  //code to run&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;An example of this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;for (int x = 0; x &lt; 5 ; x = x + 1) {&lt;br /&gt;    //we declare a variable x and set it to zero&lt;br /&gt;    //more on declaring variables later&lt;br /&gt;    //this will run when x &lt; 5&lt;br /&gt;    //x will go up by one each time the loop runs through&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;A couple of things we have to go over here:&lt;br /&gt;1) The "//" indicates a comment. A comment is a piece of writing that java ignores. It just skips merrily along right over it. "//" is used for 1 line comments.&lt;br /&gt;&lt;br /&gt;2) "/* your comments */" is for larger comments. All writing in between the /* */ is ignored.&lt;br /&gt;&lt;br /&gt;3) Operators... the &lt;, &gt;, != and == are your basic operators. THERE ARE 2 EQUALS SIGNS THERE! This is on purpose, when comparing two values you use TWO EQUALS SIGNS! When setting something equal to another thing you use one equals sign. The != means not equal to.&lt;br /&gt;&lt;br /&gt;4) You can also combine these operators, &lt;= and &gt;=, less than or equal to and greater than or equal to. For a complete list go &lt;a href="http://www.cafeaulait.org/course/week2/03.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So now let's make a small program with some of the things we know. We'll make it display a message a certain amount of times.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class Display {&lt;br /&gt;   public static void main(String[] args) {&lt;br /&gt;      for (int x = 0; x &lt; 5; x = x + 1){&lt;br /&gt;          System.out.println("Counting: " + x);&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The output to the command line for this will be:&lt;br /&gt;Counting: 0&lt;br /&gt;Counting: 1&lt;br /&gt;Counting: 2&lt;br /&gt;Counting: 3&lt;br /&gt;Counting: 4&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The x is incrementing so you will see it go up by one each time starting from 0. Notice the x = x + 1 only runs once the loop has gone through once and not immediatly.&lt;br /&gt;&lt;br /&gt;The line:&lt;br /&gt;System.out.print&lt;span style="color: rgb(255, 0, 0);"&gt;ln&lt;/span&gt;("Counting: " + x);&lt;br /&gt;Notice that this is not:&lt;br /&gt;System.out.print("Counting: " + x);&lt;br /&gt;&lt;br /&gt;.print prints continuously on one line.&lt;br /&gt;.println print to a new line each time.&lt;br /&gt;&lt;br /&gt;Also in that line we have "Five times" + x&lt;br /&gt;The "+" in this acts to combine the two for output. It &lt;span style="color: rgb(255, 0, 0);"&gt;concatenates &lt;/span&gt;it, joins them together.&lt;br /&gt;&lt;br /&gt;The while loop itself is a bit simpler:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class Display2{&lt;br /&gt;    public static void main(String[] args) {&lt;br /&gt;        int x = 0;&lt;br /&gt;        while (x &lt; 5) {&lt;br /&gt;            x = x + 1;&lt;br /&gt;            System.out.println(x);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This will give us an output of:&lt;br /&gt;1&lt;br /&gt;2&lt;br /&gt;3&lt;br /&gt;4&lt;br /&gt;5&lt;br /&gt;&lt;br /&gt;This is enough to soak in for one day. We'll start making some simple programs soon so we can get this deep in our heads.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5641760749505863784-4805670532583232538?l=dropsofjava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dropsofjava.blogspot.com/feeds/4805670532583232538/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5641760749505863784&amp;postID=4805670532583232538' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/4805670532583232538'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/4805670532583232538'/><link rel='alternate' type='text/html' href='http://dropsofjava.blogspot.com/2007/12/our-methods-are-getting-loopy.html' title='Our Methods are Getting Loopy'/><author><name>Grey-M</name><uri>http://www.blogger.com/profile/10237241798100727258</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5641760749505863784.post-8541491573341953680</id><published>2007-12-18T21:00:00.000-06:00</published><updated>2007-12-18T21:56:53.185-06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Random Numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='GreyM'/><category scheme='http://www.blogger.com/atom/ns#' term='If Statements'/><category scheme='http://www.blogger.com/atom/ns#' term='JAVA'/><title type='text'>An Iffy Brew</title><content type='html'>Hello everybody,&lt;br /&gt;&lt;br /&gt;Today we make some decisions, well the computer will. We are going to do "if" tests(! These are core elements that allow computers to make decisions that are based on a condition.)&lt;br /&gt;&lt;br /&gt;If's take the general form of...&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;If (argument) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;code to run if true&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The argument can be many things, generally they compare two things but it can be a boolean (true or false variable).&lt;br /&gt;You can add an else as well...&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;If (argument) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;code to run if true&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;} else {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;code to run if false&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 153, 153);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So to try this out we'll make a quick program to check if a number we generate is greater than or less than fifty.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;public class OddOrEven{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: rgb(0, 153, 0);"&gt;public static void main(String[] args) {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: rgb(204, 51, 204);"&gt;double z = Math.random() * 100;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: rgb(102, 51, 51);"&gt;if (z &lt;&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 153, 51);"&gt;            &lt;span style="color: rgb(204, 51, 204);"&gt;System.out.print("Less than 50");&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 153, 51);"&gt;       &lt;span style="color: rgb(153, 102, 51);"&gt; &lt;span style="color: rgb(102, 51, 51);"&gt;} else {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 153, 51);"&gt;            &lt;span style="color: rgb(204, 51, 204);"&gt;System.out.print("Greater than 50");&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 153, 51);"&gt;        &lt;span style="color: rgb(102, 51, 51);"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;We have our class in blue because all programs need a class.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;Then we have our main method, it is the method that runs first.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;In purple we have our statements&lt;br /&gt;-&lt;/span&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;double z = Math.random() * 100;&lt;br /&gt;-"double z" declares a double(a decimal value that can hold more information then a single) named z&lt;br /&gt;-"= Math.random() * 100 " generates a pseudorandom number from 0 to 1 (computers can have true randomness), by multiplying by 100 we get a number between 0 and 100.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(204, 153, 51);"&gt;&lt;span style="color: rgb(102, 51, 51);"&gt;In brown we have our if else block.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;If statements can also be nested which means an if block within an if block.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;if (argument) {&lt;br /&gt;    code to run if just the first one is true&lt;br /&gt;    if (argument) {&lt;br /&gt;        code to run if both are true&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Why don't you try to make a program that checks if the integer z is &gt; 50 and if it is check if it is less than 75 and if z &lt;&gt; 25.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Good luck!&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;My sample answer is &lt;a href="http://www.archive.org/download/JavaNestedIfStatements/OddOrEven.java"&gt;here&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5641760749505863784-8541491573341953680?l=dropsofjava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dropsofjava.blogspot.com/feeds/8541491573341953680/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5641760749505863784&amp;postID=8541491573341953680' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/8541491573341953680'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/8541491573341953680'/><link rel='alternate' type='text/html' href='http://dropsofjava.blogspot.com/2007/12/iffy-brew.html' title='An Iffy Brew'/><author><name>Grey-M</name><uri>http://www.blogger.com/profile/10237241798100727258</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5641760749505863784.post-7462610943413166259</id><published>2007-12-17T18:22:00.000-06:00</published><updated>2007-12-17T19:01:45.018-06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javac'/><category scheme='http://www.blogger.com/atom/ns#' term='methods'/><category scheme='http://www.blogger.com/atom/ns#' term='Command Line'/><category scheme='http://www.blogger.com/atom/ns#' term='classes'/><category scheme='http://www.blogger.com/atom/ns#' term='GreyM'/><category scheme='http://www.blogger.com/atom/ns#' term='main'/><category scheme='http://www.blogger.com/atom/ns#' term='Hello World'/><category scheme='http://www.blogger.com/atom/ns#' term='compiling'/><title type='text'>Your Basic Blend</title><content type='html'>Hello there!&lt;br /&gt;&lt;br /&gt;Alright so now we get to some of the basic code structure of Java!&lt;br /&gt;&lt;br /&gt;First we have the &lt;span style="color: rgb(255, 0, 0);"&gt;source file&lt;/span&gt;.&lt;br /&gt;- Inside your source file you put your &lt;span style="color: rgb(0, 0, 153);"&gt;classes.&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-Inside your classes you put your &lt;span style="color: rgb(0, 102, 0);"&gt;methods&lt;/span&gt;.&lt;br /&gt;-Inside your classes you put your &lt;span style="color: rgb(204, 51, 204);"&gt;statements&lt;/span&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A &lt;span style="color: rgb(255, 0, 0);"&gt;source file&lt;/span&gt; is a file with a &lt;span style="color: rgb(255, 0, 0);"&gt;.java extension&lt;/span&gt;.&lt;br /&gt;Then inside your source file you have (this would be in your text editor):&lt;br /&gt;(P.S. these statements should be tabbed but blogger takes them out. I'll get on it)&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;public class Heater {&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(0, 102, 0);"&gt;void heat() {&lt;/span&gt;&lt;br /&gt;         &lt;span style="color: rgb(204, 51, 204);"&gt;1stStatement;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;            2ndStatement;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(0, 102, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So let's make the traditional initiation program "Hello World".&lt;br /&gt;&lt;br /&gt;We know it must have a &lt;span style="color: rgb(255, 0, 0);"&gt;source file&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;class(es)&lt;/span&gt; , &lt;span style="color: rgb(0, 153, 0);"&gt;method(s)&lt;/span&gt; and &lt;span style="color: rgb(204, 51, 204);"&gt;statement(s).&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;file HelloWorld.java (in text editor save it as a .java)&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;public class HelloWorld {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;    public static void main (String[] args) {&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(204, 51, 204);"&gt;System.out.print("Hello World");&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(51, 255, 51);"&gt; &lt;span style="color: rgb(0, 102, 0);"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Alright let's break that down...&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;public class HelloWorld {&lt;br /&gt;-the public means it can be accessed by all&lt;br /&gt;-class means it is a class...&lt;br /&gt;-HelloWorld is the name of the class&lt;br /&gt;-you must open the class with a brace {&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;public static void main (String[] args) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;- once again public means it can be accessed by all (as opposed to private which we'll get to)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;- static we'll do later&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;- void means it is a method with no return type&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;- main() is the name of the method, &lt;span style="font-weight: bold;"&gt;the main() method always runs first!&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;- (String[] args), this is a line that creates an "array" of strings and were calling that array args and is a parameter of main(). This allows for information to be entered into the program from the command line as far as I know.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;- you must open the method with a brace {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;System.out.print("Hello World");&lt;br /&gt;-System.out.print puts the string(characters, letters, numbers strung together) that is in the parenthesis () and quotes "" into the command line.&lt;br /&gt;- you must end a statement with a semicolon ;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Then you finish up by closing the method then the class.&lt;br /&gt;&lt;br /&gt;Now we want to run this.&lt;br /&gt;First we must compile this into a .class file.&lt;br /&gt;Navigate to the folder your file is in in command prompt (unsure for mac)&lt;br /&gt;Type dir to check that the .java file is in the directory.&lt;br /&gt;Then now that we have the Javac command because of the last post type:&lt;br /&gt;javac HelloWorld.java&lt;br /&gt;&lt;br /&gt;If there are no errors in your program this will compile. Otherwise your going to get some odd messages. I'll put some common errors and errors I made when I first did this.&lt;br /&gt;&lt;br /&gt;- Java is case sensitive, capitols matter!&lt;br /&gt;- All statements must end with a semicolon!&lt;br /&gt;- Your string must be within quotes!&lt;br /&gt;- You must open classes and methods with opening braces!&lt;br /&gt;- You must close classes and methods with closing braces!&lt;br /&gt;&lt;br /&gt;Alright so if you have your .class file now you can type (still in the same directory):&lt;br /&gt;java HelloWorld&lt;br /&gt;and voila! You get the uber-exiting message Hello World.&lt;br /&gt;&lt;br /&gt;Now that you have that you can edit your original code to make that say whatever you want. If you want the message to appear on new lines you just have to put in another identical statement with a different string. Have fun!&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5641760749505863784-7462610943413166259?l=dropsofjava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dropsofjava.blogspot.com/feeds/7462610943413166259/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5641760749505863784&amp;postID=7462610943413166259' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/7462610943413166259'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/7462610943413166259'/><link rel='alternate' type='text/html' href='http://dropsofjava.blogspot.com/2007/12/your-basic-blend.html' title='Your Basic Blend'/><author><name>Grey-M</name><uri>http://www.blogger.com/profile/10237241798100727258</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5641760749505863784.post-439154234343992001</id><published>2007-12-16T15:52:00.000-06:00</published><updated>2007-12-16T16:47:26.819-06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='J2SE'/><category scheme='http://www.blogger.com/atom/ns#' term='PATH variable'/><category scheme='http://www.blogger.com/atom/ns#' term='Javac'/><category scheme='http://www.blogger.com/atom/ns#' term='Command Line'/><category scheme='http://www.blogger.com/atom/ns#' term='Starting Off'/><category scheme='http://www.blogger.com/atom/ns#' term='API'/><category scheme='http://www.blogger.com/atom/ns#' term='Eclipse'/><category scheme='http://www.blogger.com/atom/ns#' term='GreyM'/><category scheme='http://www.blogger.com/atom/ns#' term='JAVA'/><title type='text'>The Basic Tools</title><content type='html'>Okay so lets get the stuff you'll need to run some Java programs!&lt;br /&gt;&lt;br /&gt;Alright so first if your on Windows you'll need Java 2 Standard Edition SDK which can be downloaded &lt;a href="http://java.sun.com/javase/downloads/index.jsp"&gt;here&lt;/a&gt;, just click on the one for &lt;span style="font-weight: bold;"&gt;&lt;/span&gt;JDK 6 Update 3. If your on a Mac you have it already.&lt;br /&gt;&lt;br /&gt;When you are writing Java programs write them in a text editor that you can make the file a .java, the simplest ones like notepad are good.&lt;br /&gt;&lt;br /&gt;You now need to set the PATH variable of your computer, follow &lt;a href="http://java.sun.com/javase/6/webnotes/install/jdk/install-windows.html#Environment"&gt;this guide&lt;/a&gt; (windows) for that because I found it rather annoying. This is so you can use the Javac command in command prompt to run your program.&lt;br /&gt;&lt;br /&gt;I have no clue how to change the PATH variable for Mac or if there even is a PATH variable but this may help because I believe that Mac runs on UNIX. Scroll down to Common Error Messages on UNIX Systems it has an explanation.&lt;br /&gt;&lt;br /&gt;Now I find most of that annoying to the extreme but the books and tutorials I have read all say that you should start off running programs in the command line. Personally I think that you should download &lt;a href="http://www.eclipse.org/"&gt;Eclipse&lt;/a&gt;. It is far easier to tell where you made errors and far easier to correct them resulting in faster learning I believe (if you can't find your mistakes and don't know what they are you can't correct them right?).&lt;br /&gt;&lt;br /&gt;Here is a &lt;a href="http://java.sun.com/javase/6/docs/api/"&gt;link &lt;/a&gt;to pretty much the ultimate reference guide for the Java API(explained later, bookmark this you'll need it eventually).&lt;br /&gt;&lt;br /&gt;So now you have all that you will need.&lt;br /&gt;Comment if you need help.&lt;br /&gt;&lt;br /&gt;Bye&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5641760749505863784-439154234343992001?l=dropsofjava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dropsofjava.blogspot.com/feeds/439154234343992001/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5641760749505863784&amp;postID=439154234343992001' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/439154234343992001'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5641760749505863784/posts/default/439154234343992001'/><link rel='alternate' type='text/html' href='http://dropsofjava.blogspot.com/2007/12/basic-tools.html' title='The Basic Tools'/><author><name>Grey-M</name><uri>http://www.blogger.com/profile/10237241798100727258</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
