Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Applications Part I. In this chapter you will learn:  Why Java?  History of Java.  To write simple Java applications. 2.

Similar presentations


Presentation on theme: "Introduction to Java Applications Part I. In this chapter you will learn:  Why Java?  History of Java.  To write simple Java applications. 2."— Presentation transcript:

1 Introduction to Java Applications Part I

2 In this chapter you will learn:  Why Java?  History of Java.  To write simple Java applications. 2

3  Java is the world’s most widely used computer programming language.  more than a billion general- purpose computers and billions more Java-enabled cell phones, smart phones and handheld devices (e.g. tablets, notebook computers).  Java has become the language of choice for implementing Internet-based applications and software for devices that communicate over a network.  Java is Object-Oriented (OO)- today’s key programming methodology. 3

4 4  Java d eveloped by Sun Microsystem.  Originally for intelligent consumer-electronic devices.  Then used for creating web pages with dynamic content.  Now also used to: ◦ Develop large-scale enterprise applications.  Enhance web server functionality

5  Object-Oriented ◦ Combines data and behavior into one unit called objects ◦ Provides data abstraction and encapsulation ◦ Decompose programs into objects. ◦ Programs are collections of interacting and cooperating objects.  Platform-independent ◦ Portable ◦ Architecture independency ◦ ”Write-once, run-anywhere”  Secure ◦ The bytecode verification by the JVM :

6

7

8  Java programs may be divided into:  To writ Standalone/desktop applications (do not need a web browser to run)  Applets (need a web browser to num)  Java programs, you need to download and install the Java Development Kit (JDK), which is a development environment includes tools useful for this purpose.  JDK could be found in various editions.  Java Standard Edition (Java SE)  Java Enterprise Edition (Java EE)  Java Micro Edition (Java ME) 8

9  Java Standard Edition (Java SE)  Used for developing cross-platform, general- purpose applications.  You can download the JDK and its documentation from  www.oracle.com/technetwork/java/javase/downloads/index.html www.oracle.com/technetwork/java/javase/downloads/index.html  Java Enterprise Edition (Java EE)  Geared toward developing large-scale, distributed networking applications and web-based applications.  Java Micro Edition (Java ME)  Geared toward developing applications for small, memory- constrained devices, such as BlackBerry smart phones. 9

10  Java programs normally go through five phases:  Edit: ◦ Using a text editor like Notepad or an(Integrated development environment) IDE like NetBeans to write the program. ◦ Saving the program (source code) in.java file  Compile: ◦ Using the command javac to create a.class file. Which contains the compiled version of the program (Java byte code). 10

11  Load:  Class loader reads bytecodes from.class files into memory.  Verify:  Bytecode verifier examines bytecodes to ensure that they are valid and do not violate security restrictions.  Execute  Using the command java, where Java Virtual Machine (JVM) executes the java byte code in the.class fill 11

12 12

13  The Java compiler produces bytecode (a “.class” file) not machine code from the source code (the “.java” file).  Bytecode is converted into machine code using a Java Interpreter Source Code Bytecode

14  A Java virtual machine (JVM), an implementation of the Java Virtual Machine Specification, interprets compiled Java binary code (called bytecode) for a computer's processor (or "hardware platform") so that it can perform a Java program's instructions.

15

16  The Class Loader  stores bytecodes in memory  Bytecode Verifier  ensures bytecodes do not violate security requirements  Bytecode Interpreter  translates bytecodes into machine language  The class Loader, the Bytecode Verifier and Interpreter constitute the Java Virtual Machine (JVM).  JVM is platform specific.  The interpreter translates the bytecodes into specific machine commands.

17 Class Loader Bytecode Interpreter Bytecode Verifier Hardware Operating System JVM The Bytecode (the “.class” file) Running

18  You can run compiled java programs(bytecode) on an computer that has a Java Interpreter installed “Hello.java”“Hello.class”

19 19  Two basic approaches to programming design:  Structured design.  Object-oriented design.

20 20  A problem is divided into smaller sub-problems.  Each sub-problem is analyzed, solved and a solution for this sub-problem is obtained.  The solutions of all sub-problems are combined to solve the overall problem.  Is called structured programming, top-down design approach, or modular programming.

21  In OOD, a program is a collection of interacting objects.  An object consists of data and operations.  The final program is a collection of interacting objects.

22  Typical Java program may ◦ Display messages. ◦ Obtain information from the user. ◦ Perform arithmetic calculations. 22

23 23  The basic unit of a java program is a class.  Every class consists of one or more methods.  A method is a set of statements that accomplish ( إنجاز ) something.  A java class must contain one main method if it is an application.  Execution always begins with method main in java application program.

24 24 // import Section – import used java libraries public class Program_Name{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables // Input section – Enter required data // Processing section – Processing Statements // Output section – Display expected results } // end main } // end class

25  Sample program ◦ Displays a line of text ◦ Illustrates several important Java language features 25

26  Welcome 1.java 26

27  // single line comment  /* */ multiple line comment  Comments ◦ Explain programs to other programmers ◦ Improve program readability ◦ Ignored by Java compiler ◦ Single-line comment  Begin with //  Example: // This is a text-printing program. ◦ Multi-line comment  Start with /*  End with */

28  Every java program must have at least one class.  Each class begins with a class declaration that defines data and methods for the class.  The class name here is Welcome1, and contains a method main ()  Welcome1 is an identifier.

29 29  Java applications automatically begin executing at main ( ).  The parentheses ( ) after main indicate that main is a method.  Class definitions normally contain one ore more methods.  One of those methods must be called main.  The void before main ( ) means that main will not return any info.

30  Comments start with: // ◦ Comments ignored during program execution ◦ Document and describe code ◦ Provides code readability ◦ Traditional comments: /*... */ /* This is a traditional comment. It can be split over many lines */ ◦ Note: line numbers not part of program, added for reference 30 1 // Fig. 2.1: Welcome1.java

31 ◦ Blank line  Makes program more readable  Blank lines, spaces, and tabs are white-space characters  Ignored by compiler  Use blank lines and space characters to enhance program readability. ◦ Begins class declaration for class Welcome1  Every Java program has at least one user-defined class  class keyword followed by class name like Welcome1  Naming classes: must follow rules of naming. 31 3 4 public class Welcome1

32  Java identifier  Can contain series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ )  Does not begin with a digit, has no spaces  Must not be one of Java keywords.  Examples: Welcome1, $value, _value, Button7  7button is invalid  Java is case sensitive a1 and A1 are different 32

33  By convention, always begin a class name’s identifier with a capital letter and start each subsequent word in the identifier with a capital letter.  Java programmers know that such identifiers normally represent Java classes, so naming your classes in this manner makes your programs more readable. 33

34 ◦ Saving files  File name must be class name with.java extension  Welcome1.java ◦ Left brace {  Begins body of every class  Right brace ends declarations (line 13) 34 4 public class Welcome1 5 { 13} // end clazss Welcome1

35  It is an error for a public class to have a file name that is not identical to the class name (plus the.java extension) in terms of both spelling and capitalization.  It is an error not to end a file name with the.java extension for a file containing a class declaration. If that extension is missing, the Java compiler will not be able to compile the class declaration. 35

36  Part of every Java application ◦ Applications begin executing at main  Parentheses indicate main is a method  Java applications contain one or more methods ◦ Exactly one method must be called main  Methods can perform tasks and return information ◦ void means main returns no information ◦ For now, main 's first line  Left brace begins body of method declaration ◦ Ended by right brace } (line 11) 36 7 public static void main( String args[] ) 8 {

37  Instructs computer to perform an action  Prints string of characters  String – series of characters inside double quotes  White-spaces in strings are not ignored by compiler  System.out  Standard output object ◦ Method System.out.println  Displays line of text ◦ This line known as a statement  Statements must end with semicolon ; 37 9 System.out.println( "Welcome to Java Programming!" );

38  Ends method declaration  Ends class declaration  Can add comments to keep track of ending braces 38 11 } // end method main 13 } // end class Welcome1

39  Modifying programs ◦ Welcome2.java (Fig. 2.3) produces same output as Welcome1.java (Fig. 2.1) ◦ Using different code ◦ Line 9 displays “Welcome to ” with cursor remaining on printed line ◦ Line 10 displays “Java Programming! ” on same line with cursor on next line 39 9 System.out.print( "Welcome to " ); 10 System.out.println( "Java Programming!" );

40 40 System.out.print keeps the cursor on the same line, so System.out.println continues on the same line. (Fig. 2.3)

41  Escape characters ◦ Backslash ( \ ) ◦ Indicates special characters to be output  Newline characters (\n) ◦ Interpreted as “special characters” by methods System.out.print and System.out.println ◦ Indicates cursor should be at the beginning of the next line ◦ Welcome3.java (Fig. 2.4) ◦ Line breaks at \n 41 9 System.out.println( "Welcome\nto\nJava\nProgramming!" );

42 42 A new line begins after each \n escape sequence is output.

43 43

44 public class SimpleJavaProgram { public static void main(String[] args) { System.out.println("My first Java program."); System.out.println("The sum of 2 and 3 = " + 5); System.out.println("7 + 8 = " + (7 + 8)); } Class name Java o/p statement Body of class Heading of method main

45 45  A java output statement causes the program to evaluate whatever is in the parentheses and display the result on screen.  Anything in double quotation marks, called string.  + is used to concatenate the strings. The system automatically converts the number 5 into a string, joins that string with the first string, and displays it. A Java Program 2 – Simple Java Progam

46  The parentheses around 7+8 causes the system to add the numbers 7 and 8,resulting in 15.  The number 15 is then converted to string 15 and joined with string “7+8”= “. A Java Program 2 - SimpleJavaProgam Sample output: My first Java program. The sum of 2 and 3 = 5 7 + 8 = 15

47 47  Testing ◦ Make sure that the output of the program conforms with the input.  Debugging ◦ Find, Understand and correct the error

48 48  There are three types of errors:  Syntax errors are raised if some rules were broken.  The Java compiler generates syntax-error messages when the syntax of a program is incorrect. Each error message contains the file name and line number where the error occurred.  E.g. Welcome1.java:6 indicates that an error occurred in the file Welcome1.java at line 6. The remainder of the error message provides information about the syntax error.  Logical Errors: The program run but provides wrong output.  Runtime errors: The program stop running suddenly when asking the OS executing a non accepted statement (divide by zero, etc).

49  When attempting to compile a program, if you receive a message such as “ bad command or filename, ” “ javac: command not found ” or “ 'javac' is not recognized as an internal or external command, operable program or batch file, ” then your Java software installation was not completed properly. If you are using the Java Development Kit, this indicates that the system ’ s PATH environment variable was not set properly. Please review the installation instructions. On some systems, after correcting the PATH, you may need to reboot your computer or open a new command window for these settings to take effect. 49

50  Windows and dialog boxes ◦ Many Java applications use these to display output ◦ JOptionPane provides prepackaged dialog boxes called message dialogs 50

51  Dialog1.java 51 Show a message dialog with textImport class JOptionPane

52  Package javax.swing ◦ Contains classes to help create graphical user interfaces (GUIs) ◦ Contains class JOptionPane  Declares static method showMessageDialog for displaying a message dialog 52

53  Input dialog ◦ Allows user to input information ◦ Created using method showInputDialog from class JOptionPane 53

54  NameDialog.java 54 Show input dialog Format a String to output to user

55  System.out.printf ◦ Feature added in Java SE 5.0 ◦ Displays formatted data ◦ Format string  Fixed text  Format specifier – placeholder for a value ◦ Format specifier %s – placeholder for a string 55 9 System.out.printf( "%s\n%s\n", 10 "Welcome to", "Java Programming!" );

56 56 System.out.printf displays formatted data.

57  Upcoming program  Write a program to add two integer numbers, and print the summation. ◦ Use Scanner to read two integers from user ◦ Use printf to display sum of the two values ◦ Use packages 57

58 End


Download ppt "Introduction to Java Applications Part I. In this chapter you will learn:  Why Java?  History of Java.  To write simple Java applications. 2."

Similar presentations


Ads by Google