Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled.

Similar presentations


Presentation on theme: "Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled."— Presentation transcript:

1 Introduction to Java Programming

2 History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled Web browser F JDK Evolutions F J2SE, J2ME, and J2EE

3 Characteristics of Java F Java is simple F Java is object-oriented F Java is distributed F Java is interpreted F Java is robust F Java is secure F Java is architecture-neutral F Java is portable F Java’s performance F Java is multithreaded F Java is dynamic

4 Getting Started with Java Programming F A Simple Java Application F Compiling Programs F Executing Applications

5 A Simple Application Example 1.1 //This application program prints Welcome //to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }

6 Creating and Compiling Programs F On command line –javac file.java

7 Executing Applications F On command line –java classname

8 Example javac Welcome.java java Welcome output:...

9 Anatomy of a Java Program F Comments F Package F Reserved words F Modifiers F Statements F Blocks F Classes F Methods F The main method

10 Comments In Java, comments are preceded by F two slashes (//) in a line, F enclosed between /* and */ in one or multiple lines. F Non executable statement.

11 Package java.util— collection data structure classes java.io— file operations java.math— multiprecision arithmetics java.nio— the New I/O framework for Java java.net— networking operations, sockets, DNS lookups,... java.security— key generation, encryption and decryption java.sql— Java Database Connectivity (JDBC) to access databases java.awt— basic hierarchy of packages for native GUI components javax.swing— hierarchy of packages for platform-independent rich GUI componentsGUI java.applet— classes for creating an applet

12 Reserved Words F keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. F class F public F Static F void.

13 Modifiers Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used F public, static, private, F final, abstract, protected

14 Statements A statement represents an action or a sequence of actions. F System.out.println("Welcome to Java!") F "Welcome to Java!“ F Every statement in Java ends with a semicolon (;).

15 Blocks A pair of braces in a program forms a block that groups components of a program.

16 Classes F The class is the essential Java construct. F A class is a template or blueprint for objects. F To program in Java, you must understand classes and be able to write and use them. F a program is defined by using one or more classes.

17 public class Dog { String breed; int age; String color; void barking(){ } void hungry(){ } void sleeping(){ } }

18 Methods F Is a collection of statements that performs a sequence of operations to display a message on the console. F The string argument is enclosed within parentheses.

19 Main Method F The main method provides the control of program flow. F The Java interpreter executes the application by invoking the main method. F The main method looks like this: public static void main(String[] args) { // Statements; }

20 Object F Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class. F Creating an Object: F a class provides the blueprints for objects. So basically an object is created from a class. In Java, the new key word is used to create new objects. There are three steps when creating an object from a class: –Declaration: A variable declaration with a variable name with an object type. –Instantiation: The 'new' key word is used to create the object. –Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

21 class Rectangle { double length; double breadth; } class RectangleDemo { public static void main(String args[]) { Rectangle myrect = new Rectangle(); double area; myrect.length = 10; myrect.breadth = 20; area = myrect.length * myrect.breadth ; System.out.println("Area is " + area); }

22 F C:java>javac RectangleDemo.java F C:java>java RectangleDemo F Area is 200.0


Download ppt "Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled."

Similar presentations


Ads by Google