Friday, August 06, 2010

Introduction to Java, Week 1, Reading, Java Programming by Joyce Farrell Chapter 1: Creating Your First Java Classes

Java Programming
  • Learning About Programming
    A program is a set of instructions that tell a computer what to do. Programs could be written in machine language, a low level programming language, that requires that the  1s and 0s get set on the computer directly.  Programs have evolved to high level programming languages with words we humans would understand as well as the ability to use names for memory locations in place of the location itself.  This is done with syntax, the rules of the language you choose.
    To do this series of program statements are written in a form similar to English. These are then, depending on the language, run through a compiler (converts the whole program to something that will run) or to an interpreter (takes it one statement at a time) (although Java in its unique way does both). In doing so, syntax errors, mistakes in how the language is used, may happen and will need to be corrected in a processes called debugging.
    A good programmer will not only know the syntax but also the logic or the way the program flows to a program as logic problems as as bad if not worse then syntax problems.   
  • Introducing Object-Oriented Programming Concepts
    • Procedural Programming
      Procedural Programming involves writing steps of code that will follow one after another, called procedures.  By string several procedures together you can get the results you want by doing a call, or a jump to it, to it when needed. 
    • Object-Oriented Programming
      Object-Oriented Programming revolves around creating objects, stand alone applications that can be reused in other programs. To understand object-oriented programming one needs to understand its three parts, Encapsulation, inheritance, and polymorphism.
    • Understanding Objects, Classes and Encapsulation
      Objects are made up of attributes, things that define the object. A class is a collection of objects that have common properties which are defined in the class definition. When you create one of these you create an instance of the class. 
      Objects are also made up of methods, blocks of code that say what the object can and cannot do. 
      Like other programming languages, variables are used to move data around. Unlike regular variables, objects can encapsulate  the data, or make it so that it cannot be changed. Another important feature is that someone does not need to know how the class works, only that it does. Someone could change the method inside and as long as all is similar, it will continue to be transparent to the one using it.
    • Understanding Inheritance and Polymorphism
      Inheritance is ability to create a class that has similar features but can have more specific features.  None of the old ‘features’ change, just new ones are added.  Polymorphism is the idea that one word or symbol can be used different ways depending on how it is interpreted.
  • Learning About Java
    Developed by Sun Microsystems, Java is meant to be machine neutral so that it can run on any computer that has a Java Virtual Machine (JVM) installed on it.  Java source code is first compiled  so that it can be run on the JVM through an interpreter. This gives security to the operating system and allows for programmers to write one set of code that will work on all machines. 
    • Java Program Types
      There are two types of Java programs, A Java applet is a program that is designed to be embedded in a web page.  A Java Application is a stand alone Java program.  They can be run from the console, a character based environment, or a GUI environment called a windowed application.
  • Analyzing A Java Application That Uses Console Output
    • Understanding the Statement That Prints Output
      public class First
          {
          public static void main(String[] args)
              {
                  system.out.println("First Java application");
              }
          } 


      The line that starts with system.out.println is the one that does the work of the program above.  It is a program line, which ends in a semicolon. First Java application is a literal string, a series of characters that will appear just as they are in the quote marks. It is also an argument, a piece of information passed to something else.  We are passing it to method called println, putting output on the screen through the out object. To finish this, system  is the class, the collection of system objects. So this one line has to be part of a class, defined in the first line.

    • Understanding the First Class
      What is written must be part of a class. in our case it is the First class.   It could have any name at all as long as it: 1) begins with a letter of the English alphabet, an underscore or dollar sign; 2) only have letters, digit, underscores or dollar signs; 3) not be a reserved keyword; 4)cannot be true, false or null.
      A standard is to start with an uppercase letter and use other upper case for readability, but it is not required.
      The word public at the beginning of class line is an access modifier, telling us what other classes can access it. 
      Inside the { } following the class are a list of methods and data items.  In ours we have a method called main contained also in {}.  All {} must match.  We add white space to these to make them readable, but it is not required. 
    • Understanding the main() Method
      Now to look at the method itself.  In the statement:


      public static void main(String[] args)






    we find the following:
    Public declares that it can be accessed by other methods. Static means that it is accessible and useable.  Void means that it is empty, not that it has no contents, but that it returns nothing back out of it.  Between the parenthesis String[] represents what can be passed to the method and args represents the string objects that will get passed.
    One should note that not all classes have a main() function but all Java applications will as the JVM will look to it first. 


  • Adding Comments To A Java Class
    Comments should be used to document what is going on in a program.  They will not run when you execute your program but will help you find problems that you may have generated as well as let others know what you were doing at the time.
    There are three types of comments:
    1) line comments – start a line with // and the line will not run.  It can also be put at the end of a programming line and nothing after it will run.
    2) block comments – starting with a /* and ending with */, anything in the middle will not be used by the JVM.  Useful to remove code on a temporary basis to see if it is a problem.
    3)Javadoc comments – starts with /** and ends with */, can be used to generate documentation on the program.
  • Saving, Compiling, Running, and Modifying A Java Application


    • Saving a Java Class
      In creating a Java class, if it is public, you must save it with the same name and a .java extension. 
    • Compiling a Java Class
      After writing it it must be complied to java bytecode which is then run through the interpreter  for that machine. To compile a program that was named First.java from a command line:

      javac First.java

      It is case sensitive.  If you get no errors it compiled OK and can then be run through the interpreter. Otherwise fix what ever errors come up.
    • Running a Java Application
      Again from a command line, when the class compiles OK it will create a file with the same name, in our case, First.  To run it:

      java First


    • Modifying a Java Class
      To modify a class, go back to the original source code and edit it. It will then need to be recompiled and run again.

  • Creating A Java Application Using GUI Output
    Java is not just command line.  It will also let you create Graphical User Interface (GUI) applications as well. A class called JOptionPane will allow for commands that will show, for example, output in a dialogue box. To use it, however, you will need to use the import command before the declaration line.
  • Correcting Errors and Finding Help
    When you try to compile a program, it will give you the errors that it can find.  Fixing one error may eliminate other errors later so do not get hung up on the errors that are way down the line.  Fixing these errors does not guarantee that you will not have logical errors, errors where you do things in the wrong order for example. 
    Help is available in a number of places, one being the Java web site, it contains information as well as APIs, FAQs and documentation and tutorials.

 






Building Java Programs: A Back to Basics Approach (2nd Edition)





Head First Java, 2nd Edition

Blogger Labels: Introduction,Java,Classes,computer,machine,language,words,memory,location,syntax,rules,series,English,interpreter,statement,logic,problems,Object,Procedural,steps,code,procedures,results,objects,needs,Encapsulation,inheritance,collection,definition,instance,data,method,Polymorphism,features,word,symbol,Microsystems,Virtual,system,machines,Program,Application,environment,Console,Output,ends,characters,marks,argument,piece,Class,letter,dollar,digit,signs,Inside,items,Public,Static,Void,contents,parenthesis,Comments,Useful,basis,Javadoc,documentation,extension,Otherwise,Again,Graphical,User,Interface,JOptionPane,commands,example,dialogue,declaration,Help,error,places,APIs,FAQs,Back,Basics,Approach,Edition,Head,languages,humans,locations,statements,errors,Concepts,methods,tutorials,itself,three,variables,args,println