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
Lecture 1 Concepts of Java programming, Data types, Variables and Expressions 5/24/2017 IICT, BUET 1 Java’s lineage Java is related to C++, which is direct descendent of C From C, Java derives its syntax Many of Java’s object-oriented features were influenced by C++ OOP is a programming methodology that helps organize complex programs through the use of inheritance, encapsulation and polymorphism 5/24/2017 IICT, BUET 2 The creation of JAVA Java was developed at Sun Microsystems at 1991, after an effort of 18 months The original impetus for Java was not Internet! Instead the primary motivation was the need for a platform-independent language that could be used to create software to be implemented in various consumer electronic devices, such as microwave ovens and remote controls The trouble with other languages is that they are designed to be complied for a specific target 5/24/2017 IICT, BUET 3 The creation of JAVA (Contd.) We know, compilation of a language requires a compiler targeted for that CPU, which is expensive and time consuming to create (.i.e windows and linux C compiler) So effort was given to develop a platform-independent language that could be used to produce code that would run on a variety of CPUs under differing environments. The ultimate result is Java A second force worked behind the creation of Java, that is the emergence of World Wide Web or Internet The code for internet required portability and this realization caused the focus of Java to switch from consumer electronics to Internet programming 5/24/2017 IICT, BUET 4 Why Java is Important to the Internet In a network, two very broad categories of objects are transmitted between server and your PC passive information (i.e. email, program’s code) Dynamic, active programs (i.e. self-executing program). Such a program is an active agent on the client computer, yet is initiated by the server (i.e. applets) An applet is an application designed to be transmitted (dynamically downloaded) over the Internet and executed by a Java-compatible Web browser. Applet is an intelligent program (not just an animator or image file or media file) that can react to user input and dynamically change 5/24/2017 IICT, BUET 5 Security Whenever you download a program, you are risking a viral infection So most users don’t download executable programs or scan them prior to execution Another type of malicious programs gather private information (i.e. credit card numbers, bank account balances, passwords etc) Java provides a “firewall” between a networked application and your computer Also Java achieves the protection by confining a Java program to the Java execution environment and not allowing it access to other parts of the computer 5/24/2017 IICT, BUET 6 Java’s Magic: The Bytecode The secret behind the security and portability of Java is that the output of a Java compiler is not executable code. Rather, it is bytecode Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM) JVM is an interpreter of bytecode 5/24/2017 IICT, BUET 7 Basics of a Typical Java Environment Java systems contain 5/24/2017 Environment Language APIs Class libraries IICT, BUET 8 Basics of a Typical Java Environment (cont.) Java programs normally undergo five phases Edit Compile Verifier ensures bytecodes do not violate security requirements Execute 5/24/2017 Class loader stores bytecodes in memory Verify Compiler creates bytecodes from program Load Programmer writes program (and stores program on disk) Interpreter translates bytecodes into machine language IICT, BUET 9 Fig: A typical Java environment. Pha se 1 Edit or Pha se 2 Com piler Disk Disk Prog ram is c reat ed in the edit or and sto red on d isk. Com piler c rea te s byt ec odes and st ores the m on disk. Prim ary Mem ory Pha se 3 Class Lo ad er Class loa de r p ut s byt ec o des in m em ory. Disk . . . . . . Prim ary Mem ory Pha se 4 Byt ec ode Ve rifier . . . . . . Prim ary Mem ory Pha se 5 Int erp ret er . . . . . . 5/24/2017 IICT, BUET By te c ode ve rif ier c onf irms t hat a ll byt ec odes are v alid and do not violat e Ja va ’ s sec urit y rest ric t ions. Int erpret er read s byt ec odes and translat es them into a la nguag e t hat t he c om put er c an und ersta nd, possib ly st oring d at a va lues a s the program exe c ut es. 10 5/24/2017 IICT, BUET 11 Benefits of bytecode Translating a Java program into bytecode helps makes it much easier to run a program in a wide variety of environments Only JVM needs to be implemented for each platform Once the run-time package exists for a given system, any java program can run on it 5/24/2017 IICT, BUET 12 An overview of JAVA 5/24/2017 IICT, BUET 13 Object Oriented Programming Two paradigms of programming Process-oriented model: characterizes a program as a series of linear steps (i.e. C) What is happening? Code acting on data Object-oriented programming: organizes a program around its data (that is, objects) and a set of well-defined interfaces to that data Who is being affected? Data controlling access to code 5/24/2017 IICT, BUET 14 Essential elements of OOP Abstraction (the way humans manage complexity) i.e. people don’t think of a car as a set of tens of thousands individual parts. They think of it as a welldefined object with its own unique behavior. They are free to utilize the object as a whole A complex system can be broken into more manageable pieces, which are hidden by hierarchical abstraction The three OOP Principles 5/24/2017 Encapsulation Inheritance Polymorphism IICT, BUET 15 Inheritance 5/24/2017 IICT, BUET 16 A First Simple Program /* This is a simple Java program. Call this file "Example.java". */ class Example { // Your program begins with a call to main(). public static void main(String args[]) { System.out.println("This is a simple Java program."); } } 5/24/2017 IICT, BUET 17 Entering the program The name of a source file is very important In Java, all code must reside inside a class. By convention, the name of that class should match the name of the file that holds the program A source file use the .java filename extension Java is case-sensitive 5/24/2017 IICT, BUET 18 Compiling the Program C:>\javac Example.java The javac compiler creates a file called Example.class, that contain the bytecode version of the program To actually run program, you must use the Java interpreter, called java C:>\java Example When java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension 5/24/2017 IICT, BUET 19 A closer look at the first program All Java application begin execution by calling main() A complex program will have dozens of classes, only one of which will need to have a main() method to get things started Java applets don’t have any main() method Java interpreter run only the class that have a main method println() is a built-in method System is a predefined class that provides access to the system, and out is the output stream that is connected to the console 5/24/2017 IICT, BUET 20 Control statements if (condition) statement for(initialization;condition;iteration) statement 5/24/2017 IICT, BUET 21 Lexical issues White space between tokens Identifiers Literals (the way constant value is created) Integer Floating point Character String Comments Separators Keywords 5/24/2017 IICT, BUET 22 Data types Integers Floating-pointer numbers long (64 bit) int (32 bit) short (16 bit) byte (8 bit) double (64 bit) float (32 bit) Characters (char, 8 bit) Booleans (boolean, true or false) 5/24/2017 IICT, BUET 23 Arrays Arrays in Java work differently than they do in other languages First declare the array int month_days[]; Then allocate memory month_days=new int[12]; Arrays can be initialized during declaration Multidimensional arrays int twoD[][]=new int[4][5]; Alternative array declaration int [] a2=new int[3]; String type object 5/24/2017 IICT, BUET 24