Lecture 1 - Java - What Is Java
Lecture 1 - Java - What Is Java
1
What is java?
• Based on C/C++
• Widespread acceptance
2
Java Features (1)
• Simple
– fixes some clumsy features of C++
– no pointers
– automatic garbage collection
– rich pre-defined class library
http://java.sun.com/j2se/1.4.2/docs/api/
• Object oriented
– focus on the data (objects) and methods manipulating
the data
– all functions are associated with objects
– almost all datatypes are objects (files, strings, etc.)
– potentially better code organization and reuse
3
Java Features (2)
• Interpreted
– java compiler generate byte-codes, not native machine
code
– the compiled byte-codes are platform-independent
– java bytecodes are translated on the fly to machine
readable instructions in runtime (Java Virtual Machine)
• Portable
– same application runs on all platforms
– the sizes of the primitive data types are always the same
– the libraries define portable interfaces
4
Java Features (3)
• Reliable
– extensive compile-time and runtime error checking
– no pointers but real arrays. Memory corruptions or
unauthorized memory accesses are impossible
– automatic garbage collection tracks objects usage over
time
• Secure
– usage in networked environments requires more security
– memory allocation model is a major defense
– access restrictions are forced (private, public)
5
Java Features (4)
• Multithreaded
– multiple concurrent threads of executions can run
simultaneously
– utilizes a sophisticated set of synchronization primitives (based
on monitors and condition variables paradigm) to achieve this
• Dynamic
– java is designed to adapt to evolving environment
– libraries can freely add new methods and instance variables
without any effect on their clients
– interfaces promote flexibility and reusability in code by
specifying a set of methods an object can perform, but leaves
open how these methods should be implemented
– can check the class type in runtime 6
Java Disadvantages
7
8
About Java
For most computer languages, the name of the file
that holds the source code to a program is
immaterial. However, this is not the case with
Java.
The first thing that you must learn about Java is
that the name you give to a source file is very
important.
For this example, the name of the source file
should be Example.java.
In Java, a source file is officially called a
compilation unit.
The Java compiler requires that a source file use
the .java filename extension.
9
About Java
12
About Java
13
About Java
Any information that you need to pass to a method
is received by variables specified within the set of
parentheses that follow the name of the method.
These variables are called parameters.
In main( ), there is only one parameter, a
complicated one.
String args[ ] declares a parameter named args,
which is an array of instances of the class String.
Arrays are collections of similar objects. Objects
of type String store character strings. In this
case, args receives any command-line arguments
present when the program is executed. 14
About Java
15
About Java
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 the
class name do not match your program will not
compile).
20
Java Identifiers
All Java components require names. Names used
for classes, variables and methods are called
identifiers.
combination of characters.
Java Identifiers
23
Sample Examples
24
Sample Examples
class Simple{
public static void main(String[] arg
s){
int a=10;
float f=a;
System.out.println(a);
System.out.println(f);
}}
Output: 10 10.0
25
Sample Examples
Type casting
class Simple{
public static void main(String[] args
){
float f=10.5f;
//int a=f;//Compile time error
int a=(int)f;
System.out.println(f);
System.out.println(a);
}}
Output: 10.5 10
26
Thank you
27