Structure of Java program

Structure of Java program

//comments

Package Statements

import Statements

class<Class Name>{

variables

methods

Public static void main(String[]args){

Statements
}

}

Rules:

Package Statement:

  Package Stmt is an optional Statement.The PAckage stmt must be the first executable Statement in a java program.The package statement can we specified at most one time.

Import Statement:

  In Java if a fully qualified name, which includes the package and the class name, is given then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class.

-->Aclass can contain variables and method.Which are called assa members of a class.Aclass can contain any no. of variables and any no.of methods no restrictions.To execute a java program we are suppose to write a method called main method whosee syntax must be as follows.

     Public static void main(String[]args)

--> The execution of a java program will always begin with main method only.
A method can contain any no.of Statements(no restrictions).
--> A java program can contain  any no. of classes.and any no. of comments.And they can be specified any where in the program .The comments are non-executable statements of a java program.


Procedure to write save compile and execute a java program:


Step1: Writing a java program

     To write a java program we can use any editor like notepad,wordpad, etc...
   
    Ex:
       Class FirstProgram {

           Public static void main(String[]args){

  System.out.println("welcome to java ");

}

}
  
Step2: Saving the java program.
       
      java program can have any name but extension of the java program must be ".java".
      
                   Ex:FirstProgram.java

Step3: Compiling the Java program.

  To compile a java program we require a command prompt and we use "javac"command.

     Syntax:javac ProgramName/Filename.

       Ex:javac FirstNAme.java
-->When a java program is compiled .java file (source code)will be converted to .classfile(byte code)

Step4: Executing a java program.

  To execute a java program we require a command prompt and we use a java command.
 
      Syntax:java ClassName
 
         Ex:java FirstProgram

-->To develop a java application have to install java software .The java Software is a free where and it can be download in internet.

-->Once the java software is installed we have to set the path.The path is an environment variable.The operating systyem variable are called as "environment variable".This variable should contain the location or address Where java is installed.


Local Environment Setup:

If you are still willing to set up your environment for Java programming language, then this section guides you on how to download and set up Java on your machine. Please follow the following steps to set up the environment.

Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Follow the instructions to download java and run the .exe to install Java on your machine. Once you installed Java on your machine, you would need to set environment variables to point to correct installation directories:

Setting up the path for windows 2000/XP:

Assuming you have installed Java in c:\Program Files\java\jdk directory:

    Right-click on 'My Computer' and select 'Properties'.

    Click on the 'Environment variables' button under the 'Advanced' tab.

    Now, alter the 'Path' variable so that it also contains the path to the Java executable. Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.

Setting up the path for windows 95/98/ME:

Assuming you have installed Java in c:\Program Files\java\jdk directory:

    Edit the 'C:\autoexec.bat' file and add the following line at the end:
    'SET PATH=%PATH%;C:\Program Files\java\jdk\bin'

Setting up the path for Linux, UNIX, Solaris, FreeBSD:

Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation if you have trouble doing this.

Example, if you use bash as your shell, then you would add the following line to the end of your '.bashrc: export PATH=/path/to/java:$PATH'
Popular Java Editors:

To write your Java programs, you will need a text editor. There are even more sophisticated IDEs available in the market. But for now, you can consider one of the following:

    Notepad: On Windows machine you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad.

    Netbeans:is a Java IDE that is open-source and free which can be downloaded from http://www.netbeans.org/index.html.

    Eclipse: is also a Java IDE developed by the eclipse open-source community and can be downloaded from http://www.eclipse.org/.

  
Java Literals:


A literal is a source code representation of a fixed value. They are represented directly in the code without any computation.

Literals can be assigned to any primitive type variable. For example:

byte a = 68;
char a = 'A'

byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number systems as well.

Prefix 0 is used to indicate octal and prefix 0x indicates hexadecimal when using these number systems for literals. For example:

int decimal = 100;
int octal = 0144;
int hexa =  0x64;

String literals in Java are specified like they are in most other languages by enclosing a sequence of characters between a pair of double quotes. Examples of string literals are:

"Hello World"
"two\nlines"
"\"This is in quotes\""

String and char types of literals can contain any Unicode characters. For example:

char a = '\u0001';
String a = "\u0001";


Java Identifiers:

All Java components require names. Names used for classes, variables and methods are called identifiers.

In Java, there are several points to remember about identifiers. They are as follows:

i.All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
ii.After the first character identifiers can have any combination of characters.
iii.A key word cannot be used as an identifier.
iv.Most importantly identifiers are case sensitive.
v.Examples of legal identifiers: age, $salary, _value, __1_value.
vi.Examples of illegal identifiers: 123abc, -salary

0 comments:

Post a Comment