Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
70 views

Assignment In: Core Java

This document provides an overview of Java applet programming. It discusses the history and origins of Java, how Java applets are secure and sandboxed, the basics of applet programming syntax and operators, how applets are created and referenced in web pages, and how transactions are handled using JDBC.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Assignment In: Core Java

This document provides an overview of Java applet programming. It discusses the history and origins of Java, how Java applets are secure and sandboxed, the basics of applet programming syntax and operators, how applets are created and referenced in web pages, and how transactions are handled using JDBC.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

ASSIGNMENT IN

CORE JAVA

Java Applet Programming


History

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.

Basics of Applet Programming


These are the following operators table shown below. are listed in precedence: the higher in the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with a relatively lower precedence.
postfix operators [] . (params) expr++ expr-unary operators creation or cast multiplicative additive shift relational equality bitwise AND bitwise exclusive OR bitwise inclusive OR logical AND logical OR conditional assignment
++expr --expr +expr -expr ~ ! new (type)expr * / % + << >> >>> < > <= >= instanceof == != & ^ | && || ? : = += -= *= /= %= &= ^= |= <<= >>= >>>=

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

Whats an API? Whats a tabular data source?

access virtually any data source, from relational databases to spreadsheets and flat files. JDBC Documentation Well focus on accessing Oracle databases

Transactions and JDBC


JDBC allows SQL statements to be grouped together into a single transaction Transaction control is performed by the Connection object, default mode is auto-commit, I.e., each sql statement is treated as a transaction We can turn off the auto-commit mode with con.setAutoCommit(false); And turn it back on with con.setAutoCommit(true); Once auto-commit is off, no SQL statement will be committed until an explicit is invoked con.commit(); At this point all changes done by the SQL statements will be made permanent in the database.

Basic steps to use a database in Java


1.Establish a connection 2.Create JDBC Statements 3.Execute SQL Statements 4.GET ResultSet 5.Close connections

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.

Establishes connection to database by obtaining a Connection object

Create JDBC statement(s)


Statement stmt = con.createStatement() ; Creates a Statement object for sending SQL statements to the database

Handling Errors with Exceptions


Programs should recover and leave the database in a consistent state. If a statement in the try block throws an exception or warning, it can be caught in one of the corresponding catch statements How might a finally {} block be helpful here? E.g., you could rollback your transaction in a catch { } block or close database connection and free database related resources in finally {} block

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 class definition can contain instance/class variables and


instance/class methods, including both the signature and the body

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

Determining the interface


before writing a class definition, determine the interface the set of services we offer to clients similarly, if defining data structures, we should first determine the interface stacks support a constructor, push, pop, size, isEmpty, and top queues offer a constructor, enqueue, dequeue, size, isEmpty and front data structures people refer to the interface as the abstract data type

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

using Java interfaces


Java allows us to tell the compiler that a class will implement an interface regard it as a contract stating that you will meet the specification any class that implements an interface must provide implementations of the public methods (or it must be abstract, and its subclasses provide them) the compiler will check, and if the bodies are not provided, it won't compile

Exception Handling in Java Introduction

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

Errors and Error Handling


An Error is any unexpected result obtained from a program during execution. Unhandled errors may manifest themselves as incorrect results or behavior, or as abnormal program termination. Errors should be handled by the programmer, to prevent them from reaching the user.

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.

AWT Using Inheritance


It is possible to use inheritance to allow TwoButtons to become a frame in its own right Note that the UML diagram will show TwoButtons as a subclass of Frame You will need to use super()to set the frame title

Java AWT Event Model


Including reactive program components involves: 1. Having the class header declare itself as implementing the ActionListener interface 2. Typically in the class constructor the class instance registers itself as being interested in listening for events from a newly created component 3. One method (e.g. actionPerformed) of the ActiveListener interface is defined

Frames

A frame is a window with a title bar and a border

The Frame class is a subclass of Container class

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); } }

AUTOBOXING & UNBOXING


Within Java 5.0, wrapper classes have become easier to use. Java 5.0 introduced automatic conversion between a primitive type and the corresponding wrapper class. From primitive type to it corresponse wrapper class is called autoboxing, the reverse process is called unboxing. Autoboxing and unboxing also apply to methods calls. For example, you can pass an argument of type int to a method that has a formal parameter of type Integer. A NullpointerException exception occurs When unboxing an null wrapper class's reference to its primitive type. For example, the code will compile but it will throw aNullpointerException at runtime. ... Long L = null; long l = L; ... Boxing conversion converts values of primitive type to corresponding values of reference type. But the primitive types can not be widened/Narrowed to the Wrapper classes and vice versa. For example, byte b = 43; Integer I1 = 23; //Constant integer value Integer I2 = (int)b; //Cast to int type Long Long Long Long L1 = 23; //compile error because 23 is integer value L2 = (Long)23; //can not cast integer value to Long wrapper class L3 = 23L; L4 = (long)23;

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.

SUBMITTED BY :Vidyanshu Narayan I D :- 08BTCSE61 SEM :- V

You might also like