Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Introduction 1 Computers and Computer Languages • Computers are everywhere – how many computers do you own? • Computers are useful because they run various programs – program is simply a set of instructions to complete some task – how many different programs do you use in a day? Introduction to Java Programming 2 Definitions • Program: A set of instructions that are to be carried out by a computer. • Program execution: The act of carrying out the instructions contained in a program. – this is done by feeding the instructions to the CPU • Programming language: A systematic set of rules used to describe computations, generally in a format that is editable by humans. Introduction to Java Programming 3 Java – An Introduction • Programming language developed by Sun Microsystems in 1991. • Originally called Oak by James Gosling • Originally created for consumer electronics ( TV, VCR, Mobile Phone, etc.) • Internet and Web was just emerging, so Sun turned it into a language of Internet Programming. • Pure Object oriented language • Java is a whole platform, with a huge library, containing lots of reusable code, and an execution environment that provides services such as security portability across operating systems automatic garbage collection. 4 Need for Java • Many different types of controllers with different set of CPU are used in electronic devices. • The problem with C and C++ is that designed to be compiled for a specific target. • An attempt to solve these problems, Gosling and others began work on a portable, platform-independent language that on a variety of CPUs under differing environments. • Second force was the introduction of World wide web demand a language that could useful in creating portable application. 5 Java “White Paper” Buzzwords • Simple • Object Oriented - technique for programming that focuses on the data (= objects) and on the interfaces to that object. • Portable & Architecture Neutral - By generating bytecode instructions, make it executable on many processors, given the presence of the Java runtime system. • Interpreted - Java interpreter can execute Java bytecodes directly on any machine. • Network-Savvy - Java has an extensive library of routines for coping with TCP/IP protocols like HTTP and FTP. • High Performance – JIT Compiler monitor the code and optimize the code for speed. • Robust - Emphasis on early checking for possible problems, later dynamic (runtime) checking, and eliminating situations that are error-prone. • Multithreaded • Secure - intended to be used in networked/distributed environments. • Dynamic 6 Architectural Neutral & Portable • Porting the java system to any new platform involves writing an java interpreter • The interpreter will convert the byte code to machine level instructions 7 Object oriented Programming 8 Why OOP? • Greater Reliability o Break complex Software projects into small, self-contained, and modular objects. • Maintainability o Modular objects make locating bugs easier, with less impact on the overall project. • Greater Productivity o By reusing the components we increase productivity • Faster Design and Modelling 9 OOP Constructs' • Objects – Real time entity • Classes – Blue print of an object. It gives structure to the objects • Inheritance – Deriving base class properties and behavior to child class • Polymorphism – One object exists in different forms. • Encapsulation – Hiding the irrelevant details to irrelevant entity • Abstraction – Revealing the relevant details 10 Java Milestones Year Milestones 1990 Sun decided to develop a software that could be used for consumer electronics. A project called Green Project created and head by James Gosling. 1991 Explored Possibility of using C++, with some updates announced a new language named “Oak” 1992 Demonstrate a new language to control a list of home appliances 1994 Team developed a new Web browser called “Hot Java” to locate and run applets. 1995 Oak was renamed to Java. Companies such as Netscape, Microsoft announced their support for Java 1996 Java became the language for Internet Programming and General purpose OO language. 11 Java Applications Java is used to develop two types of application program: • Stand-alone applications • Web applications (applets) 12 Java Environment JDK - Java Development Kit ( Program enable users to create java applications) JRE IDE - Java Runtime Environment ( Software to run java programs) - Eclipse, Jcreator, NetBeans Setting Path MyComputer -> Environment Variables ->Advanced -> Path -> C:\Jdk1.7\bin 13 Structure of Java Program Package declaration; Import statements Class Declaration { //comments Variable declaration / definition Methods declaration and definition Main methods - // entry point of java program } 14 Sample Helloworld application /* Simple Helloworld Java application */ class test { public static void main(String[] args) { System.out.println(“Helloworld Java”); } } // save it as test.java Compile : javac test.java Execute : java test Helloworld Java 15 Process of Building and Running Java Applications 16 A Picture is Worth… The output of the compiler is .class file The Interpreter's are sometimes referred to as the Java Virtual Machines Introduction to Java Programming 17 Sample Java Execution 18 More Definitions • Code or source code: The sequence of instructions in a particular program. – The code in this program instructs the computer to print a message of Hello, world! on the screen. • Output: The messages printed to the computer user by a program. • Console: The text box or window onto which output is printed. Introduction to Java Programming 19 Java is Compiled and Interpreted 20 How different from compiled languages? 21 Compiling and Running • Compiler: a program that converts a program in one language to another language – compile from C++ to machine code – compile Java to bytecode • Bytecode: a language for an imaginary cpu • Interpreter: A converts one instruction or line of code from one language to another and then executes that instruction – When java programs are run the bytecode produced by the compiler is fed to an interpreter that converts it to machine code for a particular CPU – on my machine it converts it to instructions for a Pentium cpu Introduction to Java Programming 22 The command line To run a Java program using your Command Prompt: • change to the directory of your program cd • compile the program javac Hello.java • execute the program java Hello source code (Hello.java) compile byte code (Hello.class) execute output CS305j Introduction to Computing Introduction to Java Programming 23 Platform Independency 24 JVM • Provides platform independent way of executing code, by abstracting differences between operating systems and CPU. • JVM is Write Once-Run Anywhere(WORA) software • JVM forms the larger part of JRE • JVM role is to interpret the java byte code and translate the actions to OS calls. • JVM is OS dependent which makes java source code to machine independent form. 25 JVM • JVM is the interpreter used to convert the byte code to machine code on the fly and execute it. • Byte code is optimized instruction set which independent of machine. JVM Architecture 26 JVM Advantages • Enable java program run in protected environment • Write once, run anywhere (one size fits all) • Browser can cache the downloaded code and reuse it later 27 Input Processing import java.util public class InputDemo { Scanner in; public static void main(String[] args) { in=new Scanner(System.in); System.out.println(“Enter a value”); int i=in.nextInt(); System.out.println(“User has Entered :” + i ); } } nextLine, next, nextDouble, hasNext, hasNextInt, hasNextDouble 28 Reading Input Using Console class – Java.io import java.util public class InputDemo { public static void main(String[] args) { Console con= System.Console(); String name=con.readLine(); String pass=con.readPassword(); System.out.println(“User Name : “+name +” and Password :” + pass); } } 29 Another Java program public class Hello2 { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); } } • The code in this program instructs the computer to print four messages on the screen. Introduction to Java Programming 30 Java terminology • class: (a) A module that can contain executable code. (b) A description of a type of objects. (seen later) • statement: An executable piece of code that represents a complete command to the computer. – every basic Java statement ends with a semicolon ; • method: A named sequence of statements that can be executed together to perform a particular action or computation. Introduction to Java Programming 31 Syntax and syntax errors • syntax: The set of legal structures and commands that can be used in a particular programming language. • syntax error or compiler error: A problem in the structure of a program that causes the compiler to fail. – If you type your Java program incorrectly, you may violate Java's syntax and see a syntax error. public class Hello { pooblic static void main(String[] args) { System.owt.println("Hello, world!")_ } } Introduction to Java Programming 32