Download presentation
Presentation is loading. Please wait.
Published byKelly James Modified over 9 years ago
1
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
2
Outline i. Create Java source file in DOS environment. ii. Compile the Java source file at the DOS prompt. iii. Run the Java byte code file at the DOS prompt. iv. Analyze the Java program. v. Determine the structure of the Java program. Compiled By: Edwin O. Okech [Tutor, Amoud University]
3
Steps to Creating a Java Application Compiled By: Edwin O. Okech [Tutor, Amoud University] Create/ Modify the Source Code using Text Editor Compile Source Code using Java Compiler Run Byte Code using Java Virtual Machine Source Code (.java) Byte Code (.class) Program Execution Saves on Disk Is Read By Is Interpreted By Produces Results If Error If Run Time Error or Incorrect results
4
Create a Source File Compiled By: Edwin O. Okech [Tutor, Amoud University] 1. Create a new directory in drive C to place all your Java source files. 2. Open a text editor i.e. Notepad. 3. Type the Java code. 4. Save the code to a file. The name of the file must be the same as the class name and with the extension name.java.
5
Demonstrate how to open a Java IDE e.g. Netbeans Compiled By: Edwin O. Okech [Tutor, Amoud University] Start - Programs – Netbeans Example Code // This is my first Java program. public class HelloApp { public static void main(String [] args) { System.out.println("Hello there"); System.out.println("Welcome to Java"); }
6
Analyzing the Example HelloApp.java Compiled By: Edwin O. Okech [Tutor, Amoud University] // This is my first Java program. public class HelloApp { } This is a Java comment, it is ignored by the Java compiler This is the class header for the HelloApp class This are is the body of the class HelloApp. All of the data and methods for this class will be between the curly braces
7
Analyzing the Example HelloApp.java Compiled By: Edwin O. Okech [Tutor, Amoud University] // This is my first Java program. public class HelloApp { public static void main (String args[]) { } This is Java main method. Every application must have a main method. This area is the body of the main method. All of the actions to be completed during the main method will be placed between these curly braces
8
Analyzing the Example HelloApp.java Compiled By: Edwin O. Okech [Tutor, Amoud University] // This is my first Java program. public class HelloApp { public static void main (String args[]) { System.out.println(“Hello there”); System.out.println(“Welcome to Java”); } This is the Java statement that is executed when the program runs
9
Structure of Java programme Compiled By: Edwin O. Okech [Tutor, Amoud University] Comments Keywords Modifiers Statements Blocks Classes Methods The main method Package
10
Comments Compiled By: Edwin O. Okech [Tutor, Amoud University] Help the programmers to communicate and understand the program. Not a programming statement, thus ignored by the compiler. Three Types of Comments: 1. Multiline comments 2. Single line comments 3. Javadoc comments
11
Multi line Comments Compiled By: Edwin O. Okech [Tutor, Amoud University] /* This is a comment with four lines of text. Make sure you have the pair. */
12
Single line comments Compiled By: Edwin O. Okech [Tutor, Amoud University] // This is a single line comment // Each line must have the double back slash
13
Javadoc comments Compiled By: Edwin O. Okech [Tutor, Amoud University] /** * This program converts the temperature value in * Celsius into Fahrenheit. */
14
Java keywords Compiled By: Edwin O. Okech [Tutor, Amoud University] Words that have a specific meaning to the Java compiler. Key words are lower case (Java is a case sensitive language). Key words cannot be used as a programmer ‐ defined identifier.
15
Java keywords Compiled By: Edwin O. Okech [Tutor, Amoud University] abstract else long synchronized booleanbreak byte case extendsfinal finally native new package private this throw throws transientcatch char class floatfor goto if protectedpublic return tryvoidvolatile const continuedefaultdo implements import instanceofint short static strictfpsuper while double interfaceswitch
16
Modifiers Compiled By: Edwin O. Okech [Tutor, Amoud University] Specify the properties of the data, methods, and classes and how they can be used. Example of modifiers: - public: data, method or class can be accessed by other classes. - private: data, method or class cannot be accessed by other classes. - protected - final - static - abstract
17
Modifiers: examples Compiled By: Edwin O. Okech [Tutor, Amoud University] public class ClassA { public static void main (String[] args) { System.out.println ("Try your best"); }
18
Statements Compiled By: Edwin O. Okech [Tutor, Amoud University] Represents an action or a sequence of actions. Example of statement: System.out.println("Welcome to Java!") is a statement to display the greeting: "Welcome to Java!" Every statement in Java ends with a semicolon (;).
19
Statement blocks Compiled By: Edwin O. Okech [Tutor, Amoud University] Groups the components of the program using the braces { and } in the program. Every class has a class block that groups the data and the methods of the class. Every method has a method block that groups the data and the methods of the class. Block may be nested, meaning that one block can be placed within another.
20
Classes Compiled By: Edwin O. Okech [Tutor, Amoud University] A class is the blue of an object. A class is the template of an object. Class is the essential Java construct. Classes are central to Java. Programming in Java consists of defining a number of classes: – Every program is a class (a program is defined by using one or more classes). – All programmer ‐ defined types are classes.
21
Example of a class Compiled By: Edwin O. Okech [Tutor, Amoud University] public class ClassA { public static void main (String[] args) { System.out.println ("Try your best"); }
22
Methods Compiled By: Edwin O. Okech [Tutor, Amoud University] A collection of statements that performs a sequence of operations. Contained in a class. If a method is intended to be used to communicate with or pass information to an object, it should be declared public. Example: Method println()is an instance method that belongs to an object instance and is applied to an object (System.out).
23
main Method Compiled By: Edwin O. Okech [Tutor, Amoud University] Every Java application must have a main method that is declared in the following way: public class ClassName { public static void main(String[] args) { // Statements; }
24
main Method Compiled By: Edwin O. Okech [Tutor, Amoud University] Every Java application must have main a method that is declared in the following way: public class ClassName { public static void main(String[] args) { // Statements; } This method is public i.e. visible from anywhere that can see this class.
25
main Method Compiled By: Edwin O. Okech [Tutor, Amoud University] Every Java application must have main a method that is declared in the following way: public class ClassName { public static void main(String[] args) { // Statements; } The main method is always static, meaning that this method can be run without creating an instance of the class
26
main Method Compiled By: Edwin O. Okech [Tutor, Amoud University] Every Java application must have main a method that is declared in the following way: public class ClassName { public static void main(String[] args) { // Statements; } The void keyword indicates that the data returned from this method is nothing or no value i.e. it does not return a value
27
main Method Compiled By: Edwin O. Okech [Tutor, Amoud University] Every Java application must have main a method that is declared in the following way: public class ClassName { public static void main(String[] args) { // Statements; } This is the parameter of the main method. It takes arguments of an array of Strings. The data type String starts with an uppercase S. the square brackets indicate an array.
28
Libraries Compiled By: Edwin O. Okech [Tutor, Amoud University] Java programs are usually not written from scratch. There are hundreds of library classes for all occasions. Library classes are organized into packages. For example: java.util — miscellaneous utility classes java.awt — windowing and graphics toolkit javax.swing — GUI development package
29
Packages Compiled By: Edwin O. Okech [Tutor, Amoud University] Java is a package ‐ centric language; for good organization and name scoping, put all classes into packages. A class with default access can be seen only by classes within the same package. If class A and class B are in different packages, and class A has default access, class B won't be able to create an instance of class A, or even declare a variable or return type of class A.
30
import statement Compiled By: Edwin O. Okech [Tutor, Amoud University] Full library class names include the package name. For example: java.awt.Color javax.swing.JButton import statements at the top of the source file let you refer to library classes by their short names: import javax.swing.JButton; Fully-qualified... JButton go = new JButton("Go");
31
Import statements Compiled By: Edwin O. Okech [Tutor, Amoud University] You can import names for all the classes in a package by using a wildcard.*: import java.awt.*; import java.awt.event.*; import javax.swing.*; The above Imports all classes from awt, awt.event and swing packages. java.lang is imported automatically into all classes; defines System, Math, Object, String, and other commonly used classes.
32
THE END Compiled By: Edwin O. Okech [Tutor, Amoud University] END JAVA PROGRAMMING - LESSON 2 (CREATING A JAVA APPLICATION)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.