Search This Blog

Wednesday, January 12, 2011

Lesson 1- the way java work



THE WAY  JAVA WORK
The goal is to write one application and have it work on whatever device your friends have.
The mechanism of java working includes:
1.source: This is collection of code( class, method, statement). Which is named by .JAVA extension? ex- song.java(a file for song library)
2. Compiler: Run your document through a source code compiler. The compiler checks for errors and won’t let you compile until it’s satisfied that everything will run correctly. Compiler compile the song.java file by running javac the compiler application). If you don’t have errors, you’ll get a second document named song.class ,  the compiler-generated song.class file is made up of bytecodes.
3. Output(Code): The compiler creates a new document, coded into java bytecode. Any device capable of running java will be able to interept/translate this file into something it can run. The compile bytecode is platform independent. Compile code: song.class
4. Java Virtual machine: This is not physical java machine. This is implemented in software running in their electronic gadgets. The virtual machine reads and run bytecode. So, run the program  by starting the java virtual machine with the song.java file. The JVM translates the bytecode into something the underlying platform understand, and runs your program.
JAVA CODE STRUCTURE
YOU CAN SAY WHAT IS GOES INSIDE SOURCE FILE(.java extention file)
1.       A source code file(with the .java extinction) holds at least one class definition.
Public class song{

}
2.       CLASS( what goes exactly inside class): A class has one or more methods. In the song class, the play  method will hold instructions for how the Song should play. Your methods must be declared inside a class. In other words, within the curly braces of the class.
Public class song{
   Void play() {
     }
}
3.       Method: This is set of statement. It is kind of like a function or procedure. Within the curly braces of a method, write your instructions for how that method should be performend.
Public class song{
     Void play()    {
Statement 1;
Statement2;
  }
}
·         Put a class in source file.
·         Put methods in class.
·         Put statements in a method.

Anatomy of a class
When the JVM starts running, it looks for the class you give it at the command line. The it starts looking for a specially-written method that looks exactly like:

Public static void main (string[] args){
//your code goes here
}
Next , the JVM runs everything between the curly braces{ } of your main method. Every Java application has to have at least one class, and at least one main method (not one per class, just one main per application).

Public class firstprogram {
   Public static void main (string[] arg){
       System.out.print(“This first is java program”);
       }
  }
……………………………………………………..
Description of program code
Public: public so everyone can access it.
Class: A class keyword used in java library
Firstprogram: it is a class name(declare a class name)
{ : opening curly brace of the class
Static: Static means it does NOT reset variable values each run through(not automatically anyways).
Void: do not return anything.
Main: A startup function, it start program called method.
String: arguments to the method this method must be given an array of strings, and the array will be called ‘args’
System.out.print: this says print to standard output(defaults to command-line)
; : every statement must end with a semicolon.

Writing a class with a main
In java, everything goes in a class. You’ll type your source code file(with a .java extension), then compile it into a new class file(with a .class extension). When you run program, you’re really running a class.
Running a program means telling the java virtual machine(JVM) to ”Load the Hello class, then start executing its main() method. keep running ‘til all the code in main is finished.”

The main() method is where your program starts running.

No matter how big your program is ( in other words, no matter how many classes your program uses), there’s got to be a main() method to get the ball rolling.

                 Public class MyFirstApp{
   
                       Public static void main (string []  args){
                                     System.out.println(“I Rule!);
                                     System.out.println(“The World”);
                      }
                }
……………………………………………………………………………….
   Output:
 I Rule!
The World

What can you say in the main method?
Once you’r main (or any method), the fun begins. You can say all the normal things that you say in most programming languages to make the computer do something.

Your code can tell the JVM to:

1.    Do Something
Statement: declarations, assignments, method calls, etc.
Int x  = 3;
String name  = “Dirk”;
x   = x    *   17;
system.out.print (“x  is    +   x);
double d   Math.random();
//this is a comment

2.     Do Something Again and Again
Loop: for and while

     While     (  x   =  12)     {
  
          X    =     x   -    1;
  }
 For   (int x   =  0;   x  <  10;  x   =  x   +  1 )   {
System.out.print  (  “ x   is  now    + x);
}

3.       Do Something Under This Condition
Branching: if/else tests

 If   (  x   ==  10)  {
      System.out.print ( “x  must be 10”);
}    else  {
     System.out.print  ( “x  is’t  10”);
}
If (  ( x  <  3)  &   (  name.equals ( “ Dirk”)))  {
      System.out.println ( “ Gently”) ;
}
System.out.print ( “ this line runs no matter what “):
  While  (moreBalls  == true )  {
KeepJuggling();
}
Beauty of Syntax
·         Each statement must end in a semicolon,
X  = x  +  1;

·         A single-line comment begins with two forward slashes.
X = 22;

// this line disturbsme

·         Most white space does’t matter.      X        =        3      ;

·         Variables are declared with a  name and a type

Int weight;   //type: int, name:    weight

·         Classes and method must be defined within a pair of curly braces.
Public void go ()  {
// amazing code here

}




No comments:

Post a Comment