2.0 Structure Of Java Program V2.0 - Presentation Transcript
Java Knowledge Level: Basic Your Text Here
About the Author Cognizant Certified Official Curriculum Sun Certified Java Programmer Credential Information: JAVA/PPT/0605/2.0 Version and Date: Ramasamy Subramanian 112064 Created By:
Questions A welcome Break Coding Standards Demo Key contacts Reference Test your understanding Hands on exercise Icons used
Module Information Programming concepts Prerequisites Beginners in JAVA Target Audience
This module will provide you a solid grounding to start learning the Java language
Course Description
Module Objective & Outline
Module Objective:
After completing this Module, you will get a good understanding about JAVA.
Module Outline:
4. Object Orientation and Packaging 2. Structure Of Java Program 3. Java Fundamentals 5. Access Control 1. Introduction to Java 6. Exception Handling 7. Threads 8. Java Coding Standards and Best Practices
After completing this chapter, you will get a good understanding the structure of a java program.
Objectives:
After completing this chapter, you will be able to,
Write “main” method
Write command Line Arguments
Compile and run a java program
Import Packages
Explain types of Errors
“ main” method
Consider the following sample:
/*
* Sample program to print "Hello World"
*/
class HelloWorld {
// Declare class HelloWorld
public static void main(String argv[]) {
System.out.println("Hello World!");
}
}
Key things to be noted here:
Declaration for the class called HelloWorld
You can think of a class as a collection of variables and pieces of executable code called methods
The HelloWorld class contains a single method named main()
“ main” method
When you ask the Java interpreter to run a Java program, you tell it what code to run by giving it the name of a class
The Java interpreter then loads the class and searches it for a method named main() that has the same attributes and parameters
The interpreter then calls that main() method
The main() method of an application should always be declared with these three keywords – public, static and void
The main() method here contains a single line of executable code that calls the println() method of the object System.out
Passing the argument "Hello World!" to the println() method results in "Hello World!" being output
System.out is an object that encapsulates an application's standard output
Command Line Arguments
A Java application can accept any number of arguments from the command line.
Ex: java Sort friends.txt
numberOfArgs = args.length;
The following simple application displays each of its command-line arguments on a line by itself:
public class Echo {
public static void main (String[] args) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
Hands-on
HANDS ON SESSION A
For Command Line Arguments – Refer 3.2.1 in Hands On document
Command Line Arguments
Parsing Numeric Command-Line Arguments:
int firstArg;
if (args.length > 0)
firstArg = Integer.parseInt(args[0]);
To compile the "Hello World" program, issue the following command:
C:> javac HelloWorld.java
The Java compiler, javac, requires that the name of a Java source file end with a .java extension
The case of the filename should exactly match the case used in the public class or interface declaration
The compiler produces a compiled class file with the same name as the public class or interface declaration
The file extension used for a compiled Java file is .class
If the javac compiler complains that it is unable to find some classes, it may mean that an environment variable named CLASSPATH has not been set properly
Compilation
Running a Java Program
To run a Java application, you invoke the Java interpreter, java, with one or more arguments
The first argument is always the name of a Java class
To run the ‘HelloWorld’ application, issue a
C:> java HelloWorld
The interpreter loads the specified class and then calls its main() method
Any additional arguments specified on the command line are passed to the main() method in its String[] parameter
If, there are command-line arguments, the first array element, String[0], would correspond to the first command-line argument specified after the class name, String[1] would correspond to the next command-line element, and so on
The name of the class does not appear as an element in the array of parameters passed to the main() method
Packaging
Declaring packages:
Java provides a powerful means of grouping related classes and interfaces together in a single unit: packages
Provide a convenient mechanism for managing a large group of classes and interfaces
By placing classes into a package, you also allow them to benefit from the default access modifier
The syntax for the package statement follows:
package Identifier;
Must be placed at the beginning of a compilation unit (source file), before any class declarations
Every class located in a compilation unit with a package statement is considered part of that package
Can be nested within other packages
Import
The import statement enables you to import classes from other packages into a compilation unit
You can import individual classes or entire packages of classes at once
The syntax for the import statement follows:
import Identifier;
Identifier is the name of the class or package of classes you are importing
import java.*; wont work because you can’t import nested packages with the * specification
Hands-on
HANDS ON SESSION A
For main() and packaging concept – Refer 3.2.2 in Hands On document
Types of Errors
The basic types of error are:
Syntax Errors
Runtime Errors
Logic Errors
Incorrect Operator Precedence Errors
Common Java Errors
Spelling and typo errors.
Types of Errors
Syntax Errors
These are errors in grammar and punctuation such as mismatched quotes or missed commas. These errors are caught by the javac compiler
Runtime Errors
These errors only show up as the script is executed. Common examples are calling a function that hasn't been declared (typing error or case-sensitivity issue) or division by zero.
Logic Errors
These are basic errors in the programmer's algorithms or procedural errors. Diagnosis only comes when incorrect results occur and solution requires mapping out the flow for test cases. The wrong scoping of a variable is an example of this kind of error.
Incorrect Operator Precedence Errors
These are basic mathematical grouping errors. The best way to avoid them is with brackets to force the order that you want operations to occur explicitly.
Types of Errors
Common Java Errors
Errors can take some of the fun out of programming but experience will gradually eliminate (or at least make you more watchful for) certain commonly made ones. Some of these are:
Spelling and typo errors.
Remember case sensitivity and avoid hard to find look-alikes (1 and l, 0 and O, etc.).
Not matching brackets or quotes correctly.
Wrong type of bracket. Each has its own use.
Not using the escaper backslash when required.
Uninitialized variables may be referenced accidentally.
Arrays have bounds. Don't stray outside them.
The type of the variable may be critical.
Casting a variable can cause loss of accuracy.
The scope of a variable can affect its current value.
Types of Errors
Hands-on
HANDS ON SESSION A
For this module – Refer 3.2.3 in Hands On document.
Structure of Java Program: Summary
The main() method of an application should always be declared with these three keywords – public, static and void
A Java application can accept any number of arguments from the command line.
Java provides a powerful means of grouping related classes and interfaces together in a single unit: packages
The import statement enables you to import classes from other packages into a compilation unit
The basic types of error are: Syntax Errors, Runtime Errors, Logic Errors, Incorrect Operator Precedence Errors, Common Java Errors, Spelling and typo errors.
Test Your Understanding
Question 1:
1. class Basics1 {
2. public void main(String[] args) {}
3. }
4. class Basics2 {
5. public void main(String []args) {}
6. }
7. class Basics3 {
8. public void main(String args[]) {}
9. }
What is the result of attempting to compile and run the above programs?
a. Compiler error at line 2.
b. Compiler error at line 5.
c. Compiler error at line 8.
d. Run time error at line 2.
e. Run time error at line 5.
f. Run time error at line 8.
g. None of the Above
Test Your Understanding
Question 2: What for is a package statement used?
Question 3: When is a main method used?
Question 4: Give some examples for various types of errors in java?
1 comments
Comments 1 - 1 of 1 previous next Post a comment