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 Daniul Byrd CSC 415: Programming Languages Dr. Lyle October 22, 2013 History of Java Java’s development started in the early 1990’s with Patrick Naughton, James Gosling, and Mike Sheridan at Sun Microsystems wanting to create a language similar to C++, while making it safer and getting rid of some tedious tasks that came with it (Reginald n. pag). Originally designed to be used with embedded systems, Java needed to be more lightweight than C++. In 1993, the team started focusing on creating their embedded systems technology for use in web browsers which they called applets, for small applications. From the Java Programming wikibook, Java’s goals were to: “…be simple and gather tested fundamentals and features from earlier languages, … have standard sets of APIs bundled with the language, … get rid of concepts requiring direct manipulation of memory to make the language safe, … be platform independent, … be able to manipulate network programming outof-the-box, … be embeddable in web browsers, and … have the ability for a single program to multi-task and do multiple things at the same time.” Java was first publicly released in 1995 (oracle.com), and promised programmers to be able to write their program once, and run it on any platform. Java applets were soon supported by major web browsers of the time, and Java quickly became a popular language (Wikipedia.org). Throughout the following years Java continued to be developed and has become one of the most widely used programming languages. Data Types Primitive data types Java provides include Boolean, integer, char, floating point, and double. There are many sub-types of integer which can be used to store larger integers, such as long, and to store smaller integers more efficiently in memory with byte or short. Variables in Java are strongly typed but allow coercion. Other data types are also provided in Java’s standard libraries. Strings can easily be defined and used with the Strings class. Enumeration types were added to Java in 2004 in Java 5.0 (Sebesta 257). Arrays are included in Java, which have a set length when defined. The length of the array cannot be changed, however there is a class called ArrayList which functions like an array but allows the length to be changed. Associative arrays are also possible in Java using the HashMap class. Programmers can define their own data types using classes. Using the private modifier on a variable or method, programs can be prevented from accessing variables or methods which were declared private from outside the class. Program Structure Java programs are comprised of classes. At the beginning of each class can optionally be the name of a package, followed by imports of other Java classes. Next the class declaration appears in the format: <modifier> class <class_name>. The code for the class then appears between opening and closing curly brackets. The class can contain any number of statements, variables, and methods. The main or initial class of a Java program must contain a “public static void main(String[] args)” method, which is where execution begins. Method bodies are contained in curly brackets. Naming, Bindings, and Scopes In Java, identifies can be an unlimited length string of letters and numbers, but they must begin with a letter. An identifier cannot be one of the Java’s keywords or reserved words. All Java objects are use explicit heap dynamic binding. A method’s variables are stack-dynamically bound. Any method, class, or variable using the “static” modifier will be statically bound. Java uses static scoping, and variables are available in the scope they are declared in and disposed of when the scope ends. Expressions and Assignment Statements Assignment statements in Java are written as variable = value. For the order of operations postfix operators (expr++ or expr--) have the highest priority, followed by unary operators such as ++expr, -expr, ~ or !. Next is the multiplicative operators, then additive. The remaining order is shift, relational, equality, and, or, ternary and finally assignment operators (docs.oracle.com operators). Expressions in Java have left to right associativity. The language definition guarantees operands are evaluated in left to right order, which handles the problem of side effects (Sebesta 327). Expressions in Java support coercion, meaning that a data type can broaden but not narrow unless explicitly stated. An integer can be assigned to a double without an error, but to assign a double to an integer the programmer must explicitly state so with (int). Statement Level Control Structures The simplest of Java’s selection statements are if statements. The format is if(condition) statement;, multiple statements can be enclosed in curly brackets after the condition if necessary. After an if statement, an else statement can appear containing an alternate block of statements. If and else statements can be nested inside curly brackets to create else-if statements. The other selection statement in Java is the switch statement. The switch statement allows the conditional expression to be evaluated once, and then compared to each case. If a case does not contain a break, control will fall through to the next case. A default case can also be included to catch other cases not explicitly stated. For iterative statements, Java has while/do while and for loops. A while loop will continue to execute until the condition is false. The do while loop is like the while loop, but a do{} block precedes the while loop which will also execute at least once. The for loop is very similar to C++’s, allowing the programmer to declare a variable, the condition and the step of the variable after each execution. An example of a for loop would be: For(int i = 1; i <= 10; i++){ System.out.println(“”+i); } Abstract Data Types and Encapsulation Constructs Programmers can easily use classes to create abstract data types and to encapsulate data. Classes can have each of their variables and methods declared public, private, or protected. When a data member is declared private, it cannot be accessed from outside of the class directly. This allows the programmer to either deny access to that particular member completely, or allow access through public methods written by the programmer. Java allows users to create packages, which can allow similar classes to work together. Object-Oriented Programming Support Object-oriented programming is highly supported by Java. As stated earlier, Java programs are made up of a collection of classes. Objects are created from the classes. Data types such as strings are objects in Java. Only primitive scalar types are nonobjects in Java, for the sake of efficiency (Sebesta 552). Java allows inheritance, allowing similar code to be used for multiple subclasses and polymorphism. Generally Java only allows single inheritance, but by way of using Interfaces, Java programs can have a form of multiple inheritance. The use of Interfaces allows Java classes to avoid some of the problems of having multiple parents, such as conflicting variable/method names. Objects are dynamically bound in Java. They are declared with new, and automatically disposed of when they go out of scope by the garbage collection. Concurrency Concurrency in Java is provided through the Thread class. Subclasses of the Thread class must provide a run() method which contains the code that is executed when the thread is started. The Threads are started by having the thread call it’s start() method, for example aThread.start(). When the program needs to wait for the thread to complete the join() method is called, so from the previous example it would be aThread.join(). Java Threads will use round robin style to give each thread time slices when they are of the same priority (Sebesta 605). Threads have a sleep() method which can take milliseconds as an argument. Threads can also be interrupted if necessary using its interrupt() method, which throws an InterruptedException. Java also provides a Semaphore class and different methods of synchronization that can be used if desired. Exceptions and Event Handling Exceptions in Java are implemented as classes. There are several types of predefined exceptions included in java such as IOException or a NullPointerException. Users can define their own exceptions by creating a class that is a subclass of the Exception class. To make use of exception handling, programmers need to write trycatch blocks. In Java, the type of exception needs to be a parameter in the catch() blocks. The code that may throw an exception codes in side of the try{} block. User defined exceptions can be thrown with: throw new UserException(); or by declaring an object of the exception class and using: throw ExceptionObject. After the try-catch block, the programmer can add a finally{} block which contains code that will be executed regardless if an exception is thrown or not, useful for any clean up necessary after the try block. After an exception is thrown, the code will continue executing starting after the try-catch blocks. Java’s event handling support is included in the java.awt and Swing libraries. Event listeners are added to objects that may trigger an event, such as a button being clicked. The event will be called and executed when the event is triggered during runtime. The event handlers can be named methods or anonymous methods inline. Evaluation Readability Java, when well formatted at least, is a fairly easy to read language. Java’s special words typically have straight forward names which are easy to understand such as “if”, “else”, “for”, and “while”, instead of odd names like “elsif”. Nested else if’s however can become very difficult to read. The curly braces of the code make identifying blocks of code easy. A downside of the curly braces is when many blocks of code get nested, in situations such as many try-catch blocks and exception handling. The simplicity of Java in comparison to C++ aids in its readability. There is no need for a Java programmer to worry about whether or not a particular variable is a pointer; it will usually either be a primitive type or an object. Since Java is case sensitive, an irresponsible programmer could also cause problems by naming several variables the same thing with in different case. Writability Some inexperienced Java programmers may struggle with Java code having to be written in classes. It may not always be easily apparent to which class a method should belong to if it deals with different kinds of classes. While all programmers may not agree, the use of curly brackets throughout the program can make it easier to write. This way there is no need for a programmer to have to write out “begin”, “end”, “end if”, or “end case” when a simple { or } can generally do the same job and be easily apparent what it belongs to. The use of coercion can make it easier to write expressions without needing to always be explicit, though the cost is not always being able to rely on the compiler to find an error related to typing. Reliability Java’s use of strong typing makes it more reliable than C/C++. Although coercion does weaken the reliability so much, it can still prevent many errors that would otherwise go undetected in a language without type checking. Since Java does type checking at compile time, it eliminates many would be runtime errors. Java features exception handling, which allows programmers to anticipate run-time errors and handle them gracefully. Java’s decision to not include C++’s pointers prevents many of the problems associated with them. Java programmers do not have to worry about not de-allocating memory causing memory leaks, since it is handled by the built in garbage collection. Java being run in its own runtime environment allows it to provide some additional security. Cost Since Java is a common language, the cost of learning it should be fairly low. It’s similarities to C++ and C# will make it easy for a programmer familiar with either of those languages to pick it up. Java being platform independent makes it a cost effective language to learn, as opposed to languages with very limited platforms. The cost of compiling programs in Java is very low, since there are many free compilers and IDEs available. Overall Java is a widely used and successful language. Basing it on C++ while removing some of the complexity makes it easier to learn and use while also make it safer, if somewhat less powerful. The designers made a wise decision to design and maintain the language as platform independent allowing it to be used on different operating systems and various devices undoubtedly contribute to its use and success. The many quality free compilers and IDEs available for Java are a great boon to the language. Bibliography Sebesta, Robert. Concepts of Programming Languages. 10 th ed. Pearson,2012. Print. Reginald, Arun, et all. “History of the Java Programming Language”. Web Sep. 2013. <http://en.wikibooks.org/wiki/Java_Programming/History> “The History of Java Technology”. Oracle. Web Oct. 2013. <http://www.oracle.com/technetwork/java/javase/overview/javahistory-index198355.html> “Java (programming language)”. Wikipedia. Web. Oct. 2013. <http://en.wikipedia.org/wiki/Java_(programming_language)>