Java programming
-
Upload
trinity-dwarka -
Category
Internet
-
view
55 -
download
1
Transcript of Java programming
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Affiliated to Institution of G.G.S.IP.U, Delhi
BCA
Java Programming
20206
Ms. Bharti DewaniAssistant Professor
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
What is Java?
A simple, object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, multithreaded, dynamic language.
From: Java: An Overview
James Gosling, Sun Microsystems, February 1995.
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Why Java?
• Java is a general-purpose, high-level programming language.
• It’s almost entirely object-oriented• It has a vast library of predefined objects and
operations called The Java Application Programming Interface OR JAVA API
• It’s more platform independent– this makes it great for Web programming
• It’s more secure• It’s a software-only platform running on top of other,
hardware-based platforms.
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Features of Java
• Simple
• Architecture-neutral
• Object-Oriented
• Distributed
• Compiled
• Interpreted
• Write once, run anywhere
• Statically Typed
• Multi-Threaded
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Features of Java (Contd.)
• Portable
• High-Performance
• Robust
• Secure
• Extensible
• Well-Understood
• Garbage Collected
That’s why they say …
“ Java has changed programmer’s life . ”
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Java Applications and Java … lets
• Stand-alone Applications
– Just like any programming language
• Applet
– Run under a Java-Enabled Browser
• Midlet
– Run in a Java-Enabled Mobile Phone
• Servlet
– Run on a Java-Enabled Web Server
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Java Developer's Kit
• Java's programming environment
– Core Java API
– compiler
– interpreter
– debugger
– dis-assembler
– profiler
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Java Developer's Kit (Contd.)
Java source(.java)--java compiler-->java bytecode(.class)
Java bytecode(.class)--java dis-assembler-->java source
Java bytecode--java interpreter-->machine code (O/P)
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Java API
• Collection of ready-made i.e pre-defined software components that provide many useful capabilities.
• Grouped into libraries , packages of related components i.e collection of inter-related or similar classes and interfaces
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Core API
– Essentials: Object, String, Input and Output...– Applets– Networking– Internationalization– Security– Software Components– Object Serialization– Java Database Connectivity (JDBC)
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Building Standalone JAVA Programs (on UNIX)
The “Hello World” application
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Create a Java Source File
• Pepare the file HelloWorld.java using an editor
public class HelloWorld {public static void main(String[] args) {System.out.println("Hello World!");} }
• Since Java is object-oriented, programs are organized into modules called classes, which may have data in variables and subroutines called methods.
• Note that String is built-in and• println is a member function for the System.out class
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Compile and Run
• Compile
– javac HelloWorld.java
• A file named HelloWorld.class is created if the compilation succeeds.
• Run
– java HelloWorld
Output:- Hello World!
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Comparing Java with C , C++
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Java and C++
Comments are almost like C++• /* This kind of comment can span multiple lines
*/
• // This kind is to the end of the line
• /**
* This kind of comment is a special
* ‘javadoc’ style comment
*/
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Java and C
Primitive data types are like C• Main data types are int, double, boolean, char
• Also have byte, short, long, float
• boolean has values true and false
• Declarations look like C, for example,
– double x, y;
– int count = 0;
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Java and C
Control statements are like C• if (x < y) smaller = x;
• if (x < y){ smaller=x;sum += x;}
else { smaller = y; sum += y; }
• while (x < y) { y = y - x; }
• do { y = y - x; } while (x < y)
• for (int i = 0; i < max; i++) sum += i;
• BUT: conditions must be boolean !
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Java Vs. C
• C is procedural whereas Java is object oriented.
• In C, almost everything is in functions
• In Java, almost everything is in classes
• There must be only one public class per file
• The file name must be the same as the name of that public class, but with a .java extension
• In Java, there is nothing called Pointers
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Java Vs. C++
• In Java,no preprocessor directives required
• In Java, header files are not included but Predefined classes and interfaces can be imported to achieve Reusability.
• Destructor() method is replaced by finalize() method
• Only two contructors viz. default and parameterisedare allowed in java
• Multiple inheritence is achieved via a new concept called interface