Friday, January 25, 2013

Java - How to Program


Beginning to Program using Java
 Before we ran, we learned to walk first. Before we walked, we needed to learn how to get up on our feet---and way before that, we crawled.
Before we build highly scalable web-endpoints or a flashy Android application, we need to do the crawling analog in Java. We need to learn how to write a basic program using the Java language. We need to discover the mechanics of compilation and runtime, what legal statements can we declare and their effects. We need to know how Java statements are organized, what kinds of control structures are available, what kinds of things we can create and manipulate.

Learning Objectives

The goal of this chapter is take a first step towards a very long road of programming. We need to learn how programs are constructed, what tools and with what materials should we construct them and finally, how should we run them.

Some concepts in Java

Java is a compiled language. You need to write the programming statements in a source file, compile them to produce an executable format or object code and finally run the executable file. If you did not make syntactical mistakes the source code will compile without problems. If you did commit syntactical errors then the compiler will not let you through, you will not be able to produce an executable format of your source program.
A Java source file is a simple text file. You can use basic text editors to create and edit a source file. The choice for program editor is at best, a matter of preference, some people swear by the power of vi, some by Emacs, Textmate, JEdit and many others. This is not a discussion about text editors so I won't prescribe any to you. You are old enough and discerning enough to choose the tools that feels right. In case you are curious what I use for coding, it is vi on either the OSX or Linux platform.
A Java source file will bear the extension .java, no matter which platform you are using. Don't worry that the extension doesn't follow the usual three letter format, the popular OS platforms can handle that kind of file. You can use the file creation facilities of your choice-editor or any file creation commands available to your platform---such as touch. You might have this in Windows if you installed the UnixUtils software from SourceForge.
Java is an OOP  language. This has many consequences that affects you as a programmer, but right now, it affects what we can write inside our very first source file. You can write only classes andinterfaces inside a source file. For our first example, we will write a class and not an interface---interfaces are advanced constructs in Java, we will leave that for later. Inside classes, you can write variables, methods and program statements.
The source file cannot be ran directly on your platform. It needs to be compiled. If you have installed the JDK  properly on your platform, you can already use the java compiler. The compiler takes on a source file (or source files) as an argument and turns them into executable files. These files do not have .com or .exe extensions, they have .class extensions instead.
You cannot run .class files directly on top of your OS. Java executables run inside a special environment called the JRE . To run Java object files, you need to invoke the JRE and pass the name of the Java executable as a command line argument---like this
$ java NameOfExecutable 
There are many kinds of Java programs. Some run on webservers (servlet & JSP), some inside browsers (applet) and some on mobile platforms (android). Some Java apps run on the desktop and there are two kinds of those, one with GUI  and the other without---these are called CLI orCommand Line Interface applications. We will create a CLI app for this chapter.
Before we begin writing programs, I suggest that you create a folder for purposes of trying out the samples in this book. It is best to name the folder without special characters or whitespace.
Create a file named *Hello.java. For now, just copy the sample code
/**
*
* Our first program
*
* This is a block comment 6*
**/

class Hello {
    public static void main (String [] args) {
    System.out.println("Hello World\n"); }
}
Compile the source file using . You will have a file named Hello.class as a result of the compilation. Use the command . Do not include the extension .class when running Java programs, only the file name portion of the resulting .class file is required.
If you typed your program exactly as it appeared above, the program should compile without problems. If you encountered any compilation error, to back to the program editor and check the spelling of each and every word. Make sure you did not miss a curly brace or perhaps spelt main()as Main(). Java is case sensitive---main is different from Main. When you are sure that the program is free from syntactical errors, compile it again, then run.
The output of the sample code is a very uninteresting "Hello World"---true to tradition of every beginning programmer since Kernighan & Ritchie first started it on the first edition of the C Programming Language in 1969.
Unexciting as the output maybe, it is simple yet rich enough to get our feet wet in Java programming. You need to get used to the structure of the Hello program because a lot of the codes you will write starts out this way.



Call me and you will get the knowledge and skills you need instantly.
For inquiries contact me on my mobile number or email me at inquiry@eglobiotraining.com. View my multiply site http://erwinglobio.multiply.com
 
 
Prof. Erwin M. Globio, MSIT
Senior IT Lecturer
Far Eastern University
Email Address: inquiry@eglobiotraining.com
Skype: erwinglobio
 
 
Call Now:
 
SMART: 09393741359
SUN: 09323956678




No comments:

Post a Comment