COS 9 AND COS 3 REVIEWER BRIEF HISTORY OF JAVA 1990 – Sun - TopicsExpress



          

COS 9 AND COS 3 REVIEWER BRIEF HISTORY OF JAVA 1990 – Sun Microsystems began an internal project known as the Green Project to work on a new technology. 1992 - The Green Project was spun off and its interest directed toward building highly interactive devices for the cable TV industry. This failed to materialize. 1994 – The focus of the original team was re-targeted, this time to the use of Internet Technology. A small web browser called HOT JAVA was written. Oak was renamed to Java after learning that Oak already been trademarked. JAVA – an acronym for the names of the team members. 1. James Gosling 2. Arthur Vann Hoff 3. Andy Bechtolsheim 1995 – Java was publicly released 1996 – Java Development Kit (JDK 1.0) was released. 2002 - Java Development Kit (JDK 1.4) was released. (Code name: Merlin, the most widely known version) 2004 – Java Development kit (JDK 5.0) was released. (Code name: Tiger) James Gosling - He was the first designer of Java. He is the currently the Chief Technical Officer of Sun Microsystems. JAVA FEATURES: 1. Platform Independent - (“Write-Once-Run-Anywhere”) The programs written on the platform can run on any platform provided the platform must have a Java Virtual Machine. 2. Simple - Programs are easy to write and debug. 3. Object Oriented – Everything in Java is object even the primitive data types can also be converted into object. 4. Robust – It provides the powerful exception handling and type checking mechanism as compare to other programming languages. 5. Portable – The feature “Write-Once-Run-Anywhere” makes the language portable provided that the system must have interpreter like Java Virtual Machine. OBJECT ORIENTED DESIGNS: - Focuses on object and classes based on the real world scenarios. - Emphasizes, state, behavior and interaction of object. CLASS - acts as a template from which an instance of an object is created. Defines “Write-Once-Run-Anywhere”) as the blueprint of an object. It is also defined as the data type of an object OBJECT – Actual instance of a class, created every time you instantiate a class using the new keyword. E.g. Purchase Ordering System Attributes Methods P.O. Numbers get PO Buyer get buyer Seller get seller List of Items get no. of items ATTRIBUTES – Data element of an object. Stores information about an object. Data members, instance variable, property, data field. METHOD – It describe the behavior of an object. Also called a function or procedures. CONSTRUCTOR –For creating and initializing a new object. Not members of an Object. A method like. PACKAGE – Organizing java classes into namespace, similar to folders on your PC. ENCAPSULATION – Defined as “Information Hiding”. An object should expose only on what is necessary, and only at the appropriate level. E.g: To driver: Only steering wheel, pedals are exposed. Drivers should not expose to engine. INHERITANCE – sub classes become “sub type” of the super types. A new way to create classes by deriving from another class. INTERFACE – Contract in the form of a collection of method declarations. Implementing class promises to follow the contract. POLYMORPHISM – poly means “many” morph means “forms”. It means existing many forms. UNDERSTANDING THE EXCEPTION HANDLING What is meant by Exception? - Exceptions are objects that define an abnormal condition that interrupts the normal flow of a program. - ‘Exception refers to an ‘Error’ - In Java, Errors are identified during compile – time and run time • Compile-time errors are syntax errors • Run-time error are exceptional Hierarchy of Exception Types of Exceptions 1. Unchecked Exceptions • It extend RuntimeException class (or) ArithmeticException class (or) NullPointerException class. • It occurs anywhere in the program but not compulsorily. • Exceptions which extend the RuntimeException class are called Unchecked. 2. Checked Exceptions • It don’t extend the RuntimeExceptions class. • Used to avoid a compile – time error. • IOException is a checked exception Exception Handling - Is a mechanism for handling exceptions by detecting and responding to exceptions in a uniform and reliable manner - Catching Exception - Five keywords: o Try o Catch o Throw o Throws o Finally Important Java Exceptions Exception Caused by ArithmeticException Math errors such as division by 0 ArrayIndexOutOfBounds Bad array index. FileNotFoundException An attempt to access a nonexistent file IOException General I/O failures, such as inability to read from a file. NullPointerException Referencing a null object. NumberFormatException A failing conversion between strings and numbers. OutOfMemoryException Too little memory to allocate a new object. StackOverflowException The system running out of stack space. Advantages of Exception Handling • Separating error handling code from “Regular” Code • Propagating errors up the ‘Call Stack’ then grouping Error types and Error differentiation. Syntax: • Try try { …. } • Catch catch { …. } • Throw throw new Throwable (); • Finally finally { …. } VARIABLES AND IDENTIFIERS A variable is a location in memory capable of storing a value. Variables are place holders for data. In reality, we deal with different types of data. Sometimes we use whole numbers (they have no fractional part!). For example, the number of students in my Java class, the number of items in stock, the number of books I own, the number of copies of The Monk and the Philosopher in your library, and the like are all represented by whole numbers. Such numbers are called integers. Floating point numbers such as the tax rate, the interest earned, amount of money in an account, etc. have a fractional part. Item descriptions, names of people, etc. may be represented as Strings. In summary, a variable must have a name, should represent a type, which in turn indicates a size. Java is a strongly typed language. This means that every variable you use in your program must have a type. Primitives represent very fundamental types of data. These are typically available in programming languages. Some of the primitives in Java are: Integers Name Size Range of values byte 8 bits -128 to 127 short 16 bits -32768 to 32767 int 32 bits -2147483648 to 2147483647 long 64 bits ………….. Floating Point float 32 bits double 64 bits In addition to the above, you can represent true/false using boolean. The data type char is used to store characters. The size of char in Java is 16 bits ( 0 to 65535). IDENTIFIERS Names of variables, classes and methods must start with a letter (a-z, A-Z), dollar sign ($) or an underscore (_). Names may not start with a number. However, numbers may follow a letter, $ or an underscore. Also, make sure that the name is not one of Java’s keywords (i.e. reserved words). Examples of keywords in Java are: class, interface, int, private, try, throw, etc. Follow the conventions listed below: a. Class names: Start with an uppercase letter. Do not use names that already exist in the Java libraries. For example, String is a Java class. Do not use String as the name of your class. b. Method and variable names: Start with a lowercase letter and capitalize the beginning of each new word. Some examples are: getCustomerName(), taxRate, balance, minimumBalance, getMinimumBalance(), computePay() c. Names of constants: Constants are usually in uppercase. Example: PI DECLARING VARIABLES Examples: int classSize; //declares a variable called classSzie that can store an integer value classSize = 30; //here 30 is assigned to classSize Note that the variable is to the left of the “=” and the literal value (30 in this example) is to the right of the “equal to” sign. You could combine the two statements into one single statement as shown below: int classSize = 30; Will the following declaration and assignment work? byte b = 128; The answer is “No”. The highest value that byte can accommodate is 127. Hence, the compiler objects to your assigning 128. Now, suppose we do the following: byte b = 127; //this is ok Let us increment b by 1: b++; Will the compiler permit this? If yes, what will the value of b be after the statement is executed? In this case, the compiler does permit it. However, b’s value is now -128! In other words, it works like an odometer. Curiously, when b++ is replaced by b = b + 1 (the two are supposed to have the same effect), the compiler does object. Examples of variable declarations: double taxRate = 5.25; int number = 23; float interestRate = 5.75f; //note the suffix f. Without the suffix Java assumes a double Note: You need the suffix f (or F) boolean isAvailable = false; boolean isPrime = true; Mixing types You can assign a smaller sized type to a larger one. For example, an int can be assigned to a double, a byte to an int, a short to an int and so on. short s = 10; int i = s; //this is ok because s has fewer bits than what int uses However, larger types cannot be directly assigned to smaller type. Consider the following: int i = 25.35; //this will not work The literal (i.e. what is being assigned) is 25.35 which is a double (occupying 8 bytes or 64 bits). The variable on the left hand side of ‘=’ is an int that takes up 4 bytes or 32 bits. Java recognizes that there is a potential loss of information when a larger data type is assigned to a smaller one. Hence, the compiler flags an error. You may force Java to go ahead with the assignment by casting or converting the double (i.e. 25.35) to an integer before assigning it. This is done as follows: int i = (int) 25.35; //i will be equal to 25 Using char A character is enclosed in single quotes. Character assignments are done as follows: char ch = ‘A’; //assigns the letter A to ch Internally the character is mapped on to an integer value. You may display the integer value as follows: System.out.println((int) ch); //note that ch is first cast to an integer and then displayed What does the following display? char ch = ‘A’ + 1; System.out.println(ch); This displays the character ‘B’. DECLARING A CONSTANT Suppose we wish to declare a constant called PI (remember it is customary to use all caps for constants): final double PI = 3.14; PI cannot be changed, because the assignment is final. ESCAPE SEQUENCES There are some special characters referred to as “escape sequences”. Some of them are: \b for backspace, \t for tab, \n for linefeed (newline), \r for carriage return, \” for double quote, \’ for single quote, \\ for backslash Examples: System.out.println(“Hello!\nNext Line\nLast line”); The display will be: Hello! Next Line Last Line The character ‘\n’ stands for newline character and has the effect of inserting a newline in the string. ‘\t’ inserts a tab. Suppose we wish to display “The Jungle Book” (with the quotes). The following WILL NOT work: System.out.println(“”The Jungle Book””); //gives an error The right way to do it is: System.out.println(“\”The Jungle Book\””); Note that preceding “ with a backslash forces System.out.println to display the double quote. READING IN DATA FROM THE KEYBOARD A. COMMAND WINDOW For applications that use the DOS window, data may be read in from the keyboard using a class called Scanner. The Scanner class is in the package java.util. Therefore, this package has to be imported in applications that use Scanner. The following program reads in a name and displays a greeting to the user. import java.util.Scanner; public class DisplayGreeting { public static void main(String[] args) { String name; Scanner input = new Scanner(System.in); System.out.print(“Enter name: “); name = input.nextLine(); System.out.println(“Hello “ + name + “! Have a wonderful Java “ + “experience”); } } import java.util.Scanner; public class HelloFriend { public static void main(String[] args) { String name; Scanner input = new Scanner(System.in); System.out.print(Enter name: ); name = input.nextLine(); System.out.println(name + ! That is a nice name); } } //Read in a centigrade temperature, convert it to //fahrenheit and display the results import java.util.*; public class TemperatureDOS { public static void main(String[] args) { double centigrade, fahrenheit; Scanner input = new Scanner(System.in); System.out.print(Enter temperature in centigrade:); centigrade = input.nextDouble(); fahrenheit = 1.8 * centigrade + 32.0; System.out.println(centigrade + degrees C = + fahrenheit + degrees F); } }
Posted on: Mon, 28 Jul 2014 14:53:49 +0000

Trending Topics



ody" style="min-height:30px;">
William Luiz copiei da internet: Pessoal, questoes de filosofia

Recently Viewed Topics




© 2015