Assignment In: Core Java
Assignment In: Core Java
CORE JAVA
It was originally called Oak, developed by SUN Microsystems, in a project called FirstPerson for smart appliances, such as set-top-box (interactive TV) 1992, lead by James Gosling. It adopted C++ syntax but get rid of pointer, memory management, and multiple inheritance, which are main causes of programming errors. It failed to be adopted by Timer Warner in Spring 93's ITV trial. 1993 Marc Andreesen developed Mosaic Web Browser, started the web revolution. Oak found its reason for existence. A Web browser called WebRunner was developed for demonstration. January 1995, Sun adopted Java name due to trademark issue. Marc Andreesen saw the demo and license it for use in its browser. Nov. 1995, 1st Beta release with developer kit and source code.
Java is safe
When a Java applet is downloaded, the bytecode verifier of the JVM verifies to see if it contains bytecodes that open, read, write to local disk.
Java applet can open new window but they have Java log to prevent them from being disguised as system window (for stealing password). Java applet is not allowed to connect back to other servers except that hosts itself. This secure execution environment is called sand box model. JDK 1.1 extends this tight security with digitally signed applet using jar file. More detailed fine grained security levels are planned for future release.
When operators of equal precendence appear in the same expression, some rule must govern which is evaluated first. In Java, all binary operators except for the
assignment operators are evaluated in left to right order. Assignment operators are evaluated right to left.
Java Applet"
Java is a general purpose programming language.
A Java program is created as a text file with .java extension. It is compiled into one or more files of bytecodes with .class extension. Bytecodes are a set of instructions similar to machine code of a specific machine but it can be run on any machine that has a Java Virtual Machine (JVM). JVM interprets bytecodes. JVM was first implemented on Sparc/Solaris and PC/Win32. Now ported to many other platforms. Java applets are Java programs that are retrieved and executed by web browsers through the JVM under tight secure environment. Web browsers retrieve Java applets according to following tags in the web pages: Create a web page with the object or applet tag to reference the Java Applet.
JDBC
An API that lets you access virtually any tabular data source from the Java programming language JDBC Data Access API JDBC Technology Homepage
access virtually any data source, from relational databases to spreadsheets and flat files. JDBC Documentation Well focus on accessing Oracle databases
1.
Establish a connection
1. import java.sql.*; Load the vendor specific driver a. Class.forName("oracle.jdbc.driver.OracleDriver"); i. What do you think this statement does, and how? ii. Dynamically loads a driver class, for Oracle database 2. Make the connection a. Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@oracleprod:1521:OPROD", username, passwd); i. What do you think this statement does?
ii.
INTERFACES
the interface only shows the public methods and data it does not show private data or methods it does not state how the class is implemented
interfaces vs classes
a java interface contains only the signatures of public instance methods (and named constants) a java interface acts like a specification it says what it means to be e.g. a stack to be a stack, an object must offer at least those methods in its public interface
Java Interfaces
Java allows us to take this one stage further, by formally recording the interface as a Java interface a java interface is just a collection of abstract methods (i.e. we state the signatures, but not the bodies)
public interface MyStack { public int size();
public boolean isEmpty(); public Object top(); public void push(Object elt); public Object pop(); }
Important points
using Java interfaces polymorphically gives you client code that is much easier to modify how much effort would be involved to change from an ArrayStack to an ALStack if we hadn't used an interface? In program design and development, you will probably frequently change the data structures a program uses, so interfaces gives a significant improvement in maintainability
Users have high expectations for the code we produce. Users will use our programs in unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected ways during execution
Exceptions
What are they? An exception is a representation of an error condition or a situation that is not the expected result of a method. Exceptions are built into the Java language and are available to all program code. Exceptions isolate the code that deals with the error condition from regular program logic. How are they used? Exceptions fall into two categories: Checked Exceptions Unchecked Exceptions
Checked exceptions are inherited from the core Java class Exception. They represent exceptions that are frequently considered non fatal to program execution Checked exceptions must be handled in your code, or passed to parent classes for handling.
Frames
Container class objects may have other components (e.g. Buttons) added to them using the add method A typical Java GUI will create and display one or more frames To make a frame visible the message setVisbible(true) must be sent to the frame
Layout Managers
Governs how components are arranged inside a Container object The Container method setLayout allows the programmer to specify which layout manager (e.g. FlowLayout) is desired for a particular container object The size of a Container object should be set explicitly by the programmer as well Can use pack() to set size to accommodate preferred sizes of components)
Frame Example
public class TwoButtons { Frame f; Button redButton, blueButton; public TwoButttons() { f = new Frame(Two Buttons Frame); redButton = new Button(Red); blueButton = new Button(Blue); f.setLayout(new Flowlayout()); f.add(redButton); f.add(blueButtons);
f.pack(); f.setVisible(true); } }
This restriction is also applied to method invocation: public class MyClass { public void method(Long i) { System.out.println("Here"); } public static void main(String[] args) { MyClass s = new MyClasslass(); //s.method(12); // error s.method(12L); // ok } } When invoking a method from multiple overloading methods, For the matching method process, the Java compiler will perferance the order of primitive types (Widening Primitive Conversion), wrapper class (Boxing Conversion), and var-args. For example, public class MyClass { public void method(Long x, Long y) { System.out.println("method(Long x, Long y)"); } public void method(long x, long y) { System.out.println("method(long x, long y)"); } public void method(long... x) { System.out.println("method(long... x)"); } public static void main(String[] args) { long x, y; x = y = 0; MyClass s = new MyClass(); s.method(x, y); } } The result is "method(long x, long y)". The Java compiler will check for the matching primitive types, then it will search for the Wrapper types. public class MyClass { public void method(Long x, Long y) {
System.out.println("method(Long x, Long y)"); } public void method(int x, int y) { System.out.println("method(int x, int y)"); } public void method(long... x) { System.out.println("method(long... x)"); } public static void main(String[] args) { long x, y; x = y = 0; MyClass s = new MyClass(); s.method(x, y); } } The result is "method(Long x, Long y)". The Java compiler gives preferance to the matching Wrapper class method signature other than the primitive varargs method. public class MyClass { public void method(Double x, Double y) { System.out.println("method(Double x, (Double y)"); } public void method(int x, int y) { System.out.println("method(int x, int y)"); } public void method(long... x) { System.out.println("method(long... x)"); } public static void main(String[] args) { long x, y; x = y = 0; MyClass s = new MyClass(); s.method(x, y); } } The result is "method(long ...x)". The compiler will not narrow down "long" primitive value to "int"; Also, it can not winden long to Double class. Only the var-args method can be used.
public class MyClass { public void method(Long x, Long y) { System.out.println("method(Long x, Long y)"); } public static void main(String[] args) { int x, y; x = y = 0; MyClass s = new MyClass(); s.method(x, y); } } The arguments can not winden to "long" and then box to "Long". You will get compile error.