2. Course Objective
To inculcate knowledge about the fundamentals of OOPs(Object
oriented programming)
To provide knowledge about developing java applications and
applets
Creating GUI based Java apps and applets that run in a consistent
manner across all platforms. (swings).
3. 1. Balagurusamy.E. Programming with JAVA a Primer.
NewDelhi : Tata McGraw- Hill Publishing Company
Limited.
2.Herbert Schildt. The Complete Reference- Java (7th ed.).
New Delhi : Tata McGraw-Hill Publishing Company
Limited.
Refer : http://javabeginnerstutorial.com/core-java/
Books & References
5. • Object oriented programming concepts
• Structure of Java programs
• Compiling and running the program
• Printing messages to the screen
Learning outcome of this unit
6. 6
UNIT1:IntroductiontoJavaandFeaturesofJava10hrs
History; Evolution of Java; How Java changed internet; Fundamentals of object-Oriented
programming; Basic concepts of object-oriented programming; Benefits of object-oriented
programming; Features of Java; How Java differs from C And C++; Overview of Java; Simple
Java program; Structure; Java development kit (JDK); Java interpreter; Java virtual machine.
Data types; Variable; Type conversion and casting; Operators and expressions; Programming
structure; Operators and expressions; Decision-making and branching: If; If.. Else; Nested If;
Switch; ?: Operator; Looping: While; Do; For – Jumps In Loops - Labeled loops; Array – Types
ofarrays;Stringhandling.
7. Some Basics
Q. What is a program?
Ans. A sequence of instructions that a computer can interpret
and execute.
Q. Why Java and not Hindi / Marathi / English?
Ans. Since, so far, computer is not intelligent enough to
understand natural languages.
8. Contents
History
First Java Application
Data types
Variables
Strings
Assignments
Math, Boolean expressions
Relational operations
If statements
System.exit
8
9. What is java?
A general-purpose object-oriented language.
Write Once Run Anywhere (WORA).
Designed for easy Web/Internet applications.
Widespread acceptance.
10. Very Brief History
Started in 1991 by SUN Microsystems
Targeted at consumer electronics. Wanted reliable programming language.
Integrated into browsers
Evolved into write once run anywhere, integrates into Netscape
General purpose libraries released
10
11. 11 A programming language specifies the words and
symbols that we can use to write a program
A programming language employs a set of rules that
dictate how the words and symbols can be put together to
form valid program statements
The Java programming language was created by Sun
Microsystems, Inc.
It was introduced in 1995 and it's popularity has grown
quickly since
13. Cont..
Java Editions.
J2SE(Java 2 Standard Edition) - to develop client-side standalone applications or
applets.
J2ME(Java 2 Micro Edition ) - to develop applications for mobile devices such as cell
phones.
J2EE(Java 2 Enterprise Edition ) - to develop server-side applications such as Java
servlets and Java ServerPages.
14. Development Environments
There are many programs that support the development of Java software, including:
Sun Java Development Kit (JDK)
Sun NetBeans
IBM Eclipse
Borland JBuilder
MetroWerks CodeWarrior
BlueJ
Jgrasp
Though the details of these environments differ, the basic compilation and execution
process is essentially the same
1-14
15. Java Attributes/Features
Familiar, Simple, Small
Compiled and Interpreted
Platform-Independent and Portable
Object-Oriented
Robust and Secure
Distributed
Multithreaded and Interactive
High Performance
Dynamic and Extensible
16. 16
Object-Oriented Software Development
problem solving
program design, implementation, and testing
object-oriented concepts
classes
objects
Data Abstraction & encapsulation
Dynamic Binding
inheritance
Polymorphism
Message communication
graphical user interfaces
17. 17
BENEFITS OF OOPS
1. Data hiding helps the programmer to build secure programs.
2. Possible to have multiple objects to coexist without any interference.
3. Easy to partition the work in a project based on objects .
4. Message passing between objects makes the interface description with external
systems much simple.
5. Software complexity can be easily managed.
APPLICATIONS OF OOP``
1. Real time systems
2. Simulation and modeling
3. Object oriented DB
4. Hypertext, hypermedia
5. AI & expert systems
6. Neural networks and parallel programming
7. Office automation system
18. 18
In the Java programming language:
A program is made up of one or more
classes
A class contains one or more methods
A method contains program statements
These terms will be explored in detail
throughout the course
A Java application always contains a method
called main
19. Programming Languages
Each type of CPU executes only a particular machine language
A program must be translated into machine language before it can be executed
A compiler is a software tool which translates source code into a specific target
language
Often, that target language is the machine language for a particular CPU type
The Java approach is somewhat different
1-19
20. Java Translation
The Java compiler translates Java source code into a special representation called
bytecode
Java bytecode is not the machine language for any traditional CPU
Another software tool, called an interpreter, translates bytecode into machine
language and executes it.
Therefore the Java compiler is not tied to any particular machine
Java is considered to be architecture-neutral
1-20
21. Java Translation (JVM)
Java source
code
Machine
code
Java
bytecode
Bytecode
interpreter
Bytecode
compiler
Java
compiler
• JAVA VIRTUAL MACHINE
• JUST IN TIME (JIT) EXECUTION.
• PLATFORM INDEPENDENT.
• INTERNET LANGUAGE ??
22. Java is Compiled and Interpreted
Text Editor Compiler Interpreter
Programmer
Source Code
.java file
Byte Code
.class file
Hardware and
Operating System
Notepad,
emacs,vi
javac java
appletviewer
netscape
24. Total Platform Independence
JAVA COMPILER
JAVA BYTE CODE
JAVA INTERPRETER
Windows 95 Macintosh Solaris Windows NT
(translator)
(same for all platforms)
(one for each different system)
25. Architecture Neutral & Portable
Java Compiler - Java source code (file with extension .java) to
bytecode (file with extension .class)
Bytecode - an intermediate form, closer to machine representation
A interpreter (virtual machine) on any target platform interprets the
bytecode.
26. Architecture Neutral & Portable
Porting the java system to any new platform involves writing an interpreter.
The interpreter will figure out what the equivalent machine dependent code to run
27. Java better than C++ ?
No Typedefs, Defines, or Preprocessor
No Global Variables
No Goto statements
No Pointers
No Unsafe Structures
No Multiple Inheritance
No Operator Overloading
No Automatic Coercions
No Fragile Data Types
28. Java Applications
We can develop two types of Java programs:
Stand-alone applications
Web applications (applets)
29. Applications v/s Applets
Different ways to run a Java executable are:
Application- A stand-alone program that can be invoked from command line . A
program that has a “main” method
Applet- A program embedded in a web page , to be run when the page is browsed . A
program that contains no “main” method
30. Applets v/s Applications
Different ways to run a Java executable are
Application- A stand-alone program that can be invoked from command line . A
program that has a “main” method
Applet- A program embedded in a web page , to be run when the page is browsed . A
program that contains no “main” method
Application –Executed by the Java interpreter.
Applet- Java enabled web browser.
31. Syntax and Semantics
The syntax rules of a language define how we can put together symbols, reserved
words, and identifiers to make a valid program
The semantics of a program statement define what that statement means (its
purpose or role in a program)
A program that is syntactically correct is not necessarily logically (semantically)
correct
A program will always do what we tell it to do, not what we meant to tell it to do
1-31
32. Errors
A program can have three types of errors
The compiler will find syntax errors and other basic problems (compile-time errors)
If compile-time errors exist, an executable version of the program is not created
A problem can occur during program execution, such as trying to divide by zero,
which causes a program to terminate abnormally (run-time errors)
A program may run, but produce incorrect results, perhaps using an incorrect formula
(logical errors)
1-32
34. How is Java different from C…
C Language:
Major difference is that C is a structure oriented language and Java is
an object oriented language and has mechanism to define classes and
objects.
Java does not support an explicit pointer type
Java does not have preprocessor, so we cant use #define, #include and
#ifdef statements.
Java does not include structures, unions and enum data types.
Java does not include keywords like goto, sizeof and typedef.
Java adds labeled break and continue statements.
Java adds many features required for object oriented programming.
35. How is Java different from C++…
C++ language
Features removed in java:
Java doesn’t support pointers to avoid unauthorized access of memory locations.
Java does not include structures, unions and enum data types.
Java does not support operator over loading.
Preprocessor plays less important role in C++ and so eliminated entirely in java.
Java does not perform automatic type conversions that result in loss of precision.
36. Cont…
Java does not support global variables. Every method and variable is
declared within a class and forms part of that class.
Java does not allow default arguments.
Java does not support inheritance of multiple super classes by a sub class
(i.e., multiple inheritance). This is accomplished by using ‘interface’ concept.
It is not possible to declare unsigned integers in java.
In java objects are passed by reference only. In C++ objects may be passed
by value or reference.
37. Cont …
New features added in Java:
Multithreading, that allows two or more pieces of the same program to execute
concurrently.
C++ has a set of library functions that use a common header file. But java replaces it
with its own set of API classes.
It adds packages and interfaces.
Java supports automatic garbage collection.
break and continue statements have been enhanced in java to accept labels as targets.
The use of unicode characters ensures portability.
38. Cont …
Features that differ:
Though C++ and java supports Boolean data type, C++ takes any
nonzero value as true and zero as false. True and false in java are
predefined literals that are values for a boolean expression.
Java has replaced the destructor function with a finalize() function.
C++ supports exception handling that is similar to java's. However,
in C++ there is no requirement that a thrown exception be caught.
39. Characteristics of Java
Java is simple-C & C++ features are
adapted.
Java is object-oriented-classes as
packages and objects.
Java is distributed-it can share both
data and programs.
Java is compiled & interpreted
Java is robust
Java is architecture-neutral
Java is portable
Java’s high performance-
Multithreading
Java is multithreaded-multiple
Java is dynamic-supports functions of
c & c++
Java is secure
40. Java Environment
Java includes many development tools, classes and methods
Development tools are part of Java Development Kit (JDK) and
The classes and methods are part of Java Standard Library (JSL), also
known as Application Programming Interface (API).
JDK constitutes of tools like java compiler, java interpreter and
many.
API includes hundreds of classes and methods grouped into
several packages according to their functionality.
41. First Application
/**
*Hello World, first application, only output.
*/
public class hello{
public static void main (String [] args) {
System.out.println(“Hello Worldn”);
} //end main
}//end class
41
42. public is the visibility. This can be public, private, protected or (if you omit a value)
default.
static is a special [optional] keyword that indicates that this method can be called
without creating an instance of this class. Without it, you have to instantiate this class
and call this method from the resulting object.
void is the return type of this method, indicating that this method doesn't return
anything. Methods must have a return type.
42
43. main( ... ) is the name of this method. Methods have to be named. The
parentheses indicate that this is a method.
String[] args is a single parameter for the method. String[] is the type of the
parameter, indicating an array of Strings. args is the name of the parameter.
Parameters must be named.
43
44. How to get it running
Text in hello.java file
Why?
To compile:
javac hello.java
To run:
java hello
44
45. Case Sensitivity - Java is case sensitive, which means identifier Hello and hello
would have different meaning in Java.
Class Names - For all class names the first letter should be in Upper Case.
If several words are used to form a name of the class, each inner word's first letter
should be in Upper Case.
Example class MyFirstJavaClass
Method Names - All method names should start with a Lower
Case letter.
If several words are used to form the name of the method,
each inner word's first letter should be in Upper Case.
Example public void myMethodName()
46. Notice:
Java is CASE SENSITIVE!!
Whitespace is ignored by compiler
Whitespace makes things easier to read…hence it affects your grade
File name has to be the same as class name in file.
Need to import necessary class definitions
46
47. Program File Name - Name of the program file should exactly match the class
name.
When saving the file, you should save it using the class name (Remember Java is
case sensitive) and append '.java' to the end of the name (if the file name and
class name do not match your program will not compile).
Example : Assume 'MyFirstJavaProgram' is the class name. Then the file should
saved as 'MyFirstJavaProgram.java'