Why Java?
A Brief History
Dc. 1990: James Gosling began working on a new language at Sun Microsystems, iniially called Oak
- 1996: Java 2.0 released
- 1998: Java 1.2 released (Java 2)
In Comparison
- C was developed between 1983 and 1985
- C was developed around 1972
- Fortran was developed around 1955
Why Java is Popular
- Compiled Java is platform independent.
- Giving someone C++ code built into an executable is hard to utilize because it is not platform-dependent. Compiled C++ will not function the same on different OS' while compiled java will produce the same output on Mac, Linux, etc.
- Completely object-oriented.
- C++ is a superset of C, and is often used as C+.
- Java is programmer friendly.
- Big difference between C, C++ and Java and C# is automatic garbage collection, difficult to have direct access to memory.
- Pros no use of malloc and new, no need to create own memory on stack because everything in Java is put on the heap.
- Cons: No direct reference to memory, abstracted from memory layer, everything is on heap, can't be optimized like C++ and C.
- Prebuilt functionality: lots of libraries, huge API
- Good for developing user interfaces.
Platform Independence
- Platform specific compilers generate byte codes that can be interpreted by any platform's Java Virtual machine.
- This is then interpreted on the specific platform with the Java Virtual Machine Interpreter.
Complete Object-Orientation
- Everything in Java is either:
- An object
- A member of an obect or a class
- Local to a method that is a member of an object or a class
Programmer Friendly
- Automatic garbage collection, takes a lot of edious programming tasks off programm's shoulders
Con
- Leads to reputation of being slow
- In C and C++, you don't necessarily have to do bound checking for a dynamic array, while for every array in Java must have that check done. These checks are built into the compiler, so all these checks make Java inherently slower.
JRE and J2SE
- The Java Runtime Environtment (JRE) allows a Java program to be executed on a platform.
- Consists of:
- Java Virtual Machine (JVM) - Byte code interepreter
- Java run-time library - functionality needed by programs as they run
- Memory management
- Call stack maintenance
- etc.
Allocation Libraries - functionality utilized by program's programmer
Java 2
- J2SE is the new necessary component to run Java programs.
- Used to be called JDK, still some call is because J2SE is complicated.
Java vs. C++
- No pointers (kind of)
- Pointers are still there, because everything is a pointer. In C++, you can make dynamic and static variables, while everything in Java is a reference.
- Automatic garbage collection
- No need to delete or free dynamic memory
- Automatic array bounds checking
- You can't index out of an array out of bounds
- No globals
- Everthing's a member, even the main() function which belongs to a class. This is called automatically by the compiler which will look for a main function.
- Configurable security
- Allows user to set security levels
- For example, don't let users touch your file system or disc can be configured
OOP Properties
- Encapsulation
- Inheritence
- Polymorphism
Encapsulation
- Group data and functiaonlity together
- C uses structs to group data together - why not group functionality along with it?
- Allows a programmer to explicelty provide the interface to an object
- Allows hiding of implementation details
- Programmer thinks in an OO way
Inheritence
Allows one data type on encapsulate another.
Polymorphism
- Allows one common interface for many implementations
- Allows objects to act different under different circumstances
OOP Basic Building Blocks
The class
- defines what objects of a class are
- Usually contains functionality in addition to attributes
The Object
- an instances of a class
- A variable declared to be a class type
Class vs. Object
- Classes ndo not have memory associated with them, a class is only a definition a data type
- Objects do have memory associated with them
- Like structure variables
- Each object has its own set of attributes in memory