JAVA FUNDAMENTALS ● Know the history and philosophy of Java ● Understand Java’s contribution to the Internet ● Understand the importance of bytecode ● Know the Java buzzwords ● Understand the foundational principles of objectoriented programming • many different types of CPUs are used as controllers • most computer languages were designed to be compiled into machine code • targeted for a specific type of CPU • E.g, consider the C++ language. • required a full C++ compiler targeted for that CPU • expensive and time consuming to create • code that would run on a variety of CPUs under differing environments • led to the creation of Java • The second force was the World Wide Web • emergence of the Web, Java was propelled to the forefront of computer language • design, because the Web, too, demanded portable programs • After all, the Internet consisted of a diverse, distributed universe • populated with many types of computers, operating systems, and CPUs • Though the desire for an architecture neutral programming was initial spark • It was the Internet that ultimately led to Java’s large scale success. Java’s Lineage: C and C++ • The two languages that form Java’s closest ancestors are C and C++ • From C, Java inherits its syntax • Java’s object model is adapted from C++ • First, at the time of Java’s creation, many programmers were familiar with the C/C++ syntax • Second, Java’s designers did not “reinvent the wheel • The modern age of programming began with C • It moved to C++ and then to Java. • By inheriting and building on that rich heritage • Java provides a powerful, logically consistent programming environment that • takes the best of the past and adds new features related to the online environment • C, C++, and Java define a common, conceptual framework for theprofessional programmer • Although C++ and Java are related, especially in their support for object oriented programming, • Java is not simply the “Internet version of C++.” • Java has significant practical and philosophical differences from C++. • Java is not an enhanced version of C • Java was not designed to replace C++. • Java was designed to solve a certain set of problems. • C++ was designed to solve a different set of problems. • They will coexist for many years to come How Java Impacted the Internet • First, the creation of Java simplified Internet programming drew programmers to the Web. • Second, Java innovated a new type of networked program called the applet • that changed the way the online world thought about content. • Finally, Java addressed some of the thorniest issues associated with the Internet: portability and security. Java Simplified Web-Based Programming
• its ability to create portable, cross platform
programs and Java’s support for networking • Its library of ready to use functionality to easily write programs • It also provided mechanisms that enabled programs to be readily delivered over the Internet Java Applets • one of its most exciting features was the applet • is a special kind of Java program that is designed to be transmitted over the • Internet and automatically executed inside a Java compatible web browser • key feature of applets is that they execute locally, rather than on the server Security • In order for Java to enable programs to be safely downloaded and executed on the client computer, • it was necessary to prevent them from launching such an attack • Java achieved this protection by enabling you to confine an application to the Java execution environment and • prevent it from accessing other parts of the computer Portability • mechanism that allows the same application to be downloaded and executed by a wide • variety of CPUs, operating systems, and browsers is required • not practical to have different versions of the same application for different computers The Bytecode • The key to address both the security and the portability problems • 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 Virtual Machine (JVM), which is part of the Java Runtime Environment (JRE). • JVM was designed as an interpreter for bytecode • The fact that a Java program is executed by the JVM also helps to make it secure. • it is possible for the JVM to create a restricted execution environment, called the sandbox • that contains the program, preventing unrestricted access to the machine • Because bytecode has been highly optimized, the use of bytecode enables • the JVM to execute programs much faster than you might expect The Java Buzzwords OBJECT-ORIENTED PROGRAMMING • OOP is a powerful way to approach the job of programming • programming was done by toggling in the binary machine instructions • Assembly language was invented • High level languages were introduced • The first was FORTRAN • The 1960s gave birth to structured programming(C and PASCAL) • for standalone subroutines, local variables, rich control constructs Cont… • Object oriented programming took the best ideas of structured programming and • combined them with new concepts • A program can be organized in one of two ways: • around its code (what is happening) or around its data (what is being affected) • organized around data, • with the key principle being “data controlling access to code.” Cont… • In an object oriented language, • you define the data and the routines that are permitted to act on that data. • A data type defines precisely what sort of operations can be applied to that data • All OOP languages, including Java, have three traits in common: encapsulation, polymorphism, and inheritance Encapsulation • Encapsulation is a programming mechanism that binds together code and the data it • manipulates, and that keeps both safe from outside interference and misuse • In an object oriented language, code and data can be bound together • self contained black box is created. • Within the box are all necessary data and code Cont… • Within an object, code, data, or both may be private to that object or public. • Java’s basic unit of encapsulation is the class • A class defines the form of an object. • It specifies both the data and the code that will operate on that data. • Java uses a class specification to construct objects. • Objects are instances of a class • A class is essentially a set of plans that specify how to build an object Cont… • The code and data that constitute a class are called members of the class. • the data defined by the class are referred to as member variables or instance variables. • The code that operates on that data is referred to as member methods or just methods. • Method is Java’s term for a subroutine. • know that what a Java programmer calls a method, a C/C++ programmer calls a function Polymorphism • Polymorphism is the quality that allows one interface to access a general class of actions • The specific action is determined by the exact nature of the situatio • “one interface, multiple methods.” • Polymorphism helps reduce complexity by allowing the • same interface to be used to specify a general class of action • It is the compiler’s job to select the specific action (i.e., method) as it applies to each situation Inheritance • Inheritance is the process by which one object can acquire the properties of another object. • This is important because it supports the concept of hierarchical classification • most knowledge is made manageable by hierarchical (i.e., top down) classifications. Cont… • Using inheritance, an object need only define those qualities that make it unique within its class. • It can inherit its general attributes from its parent. • Thus, it is the inheritance mechanism that makes it possible • for one object to be a specific instance of a more general case long Floating point Char Boolean One-Dimensional Arrays • A one-dimensional array is, essentially, a list of like-typed variables. • To create an array, you first must create an array variable of the desired type. • The generalform of a one-dimensional array declaration is • type var-name[ ]; practical Improved version Multidimensional Arrays • To declare a multidimensional array variable, specify each additional index using • another set of square brackets. • For example, the following declares a twodimensional array variable called twoD: • int twoD[][] = new int[4][5]; • This allocates a 4 by 5 array and assigns it to twoD. • Internally, this matrix isimplemented as an array of arrays of int Array two by two practical Out put • This program generates the following output: • 01234 • 56789 • 10 11 12 13 14 • 15 16 17 18 19 Out put • When you run this program, you will get the following output: • 0.0 0.0 0.0 0.0 • 0.0 1.0 2.0 3.0 • 0.0 2.0 4.0 6.0 • 0.0 3.0 6.0 9.0 3 by 4 by 5, three-dimensional array Out put • This program generates the following output: • 00000 • 00000 • 00000 • 00000