this ppt contains history and basic facts of object oriented programming language java, difference between JIT, JVM, JRE and JDK. it also having information about different versions of java. advantages over other language, difference between error and exception with its types is also included. explanation of final variable and string to int conversation is also added. in the end some twisted question of it which sharpen the knowledge of its basic are added. beyond this some programming examples with output is there too. hope u find it useful...!! thanku..!!
2. Facts and history
Who invented java?
James Gosling
Where?
Sun lab also known as sun micro
system.
When?
Around 1992, published in 1995.
What is first name at a time of invention?
“oak”, from the name of tree outside
the window of James.
3. Facts and history
Why the name “java” and the symbol a “coffee cup”?
Some issues with the name “oak”.
Seating in the local café.
Wounded up with the name “java”.
From the cup of coffee.
What is the relation with c/c++?
Java was created as a successor to C++ in order
to address various problems of that language
4. Features of java
Compiled and interpreted
Source code byte code and byte code machine code
Platform independent and portable
Can be run on any platform
Secure
Ensures that no virus is communicated with applet
Distributed
Multiple programmers at different remote locations can collaborate and work together
High performance
Faster execution speed
Multi language supported
Dynamic and extensible
New class library, classes and methods can be linked dynamically
5. JIT, Jvm, jre and jdk
JIT
Just-In-Time
Component of JRE
Improves the performance
JVM
Provides runtime environment in which java byte
code is executed.
Compilation + interpretation
Not physically present
JRE
Runtime environment
Implementation of JVM
Contains a libraries + other files
JDK
JRE + development tools
Bundle of softwares
7. Advantages over c/c++
Improved software maintainability
Faster development
Lower cost of development
Higher quality software
Use of notepad makes it easier
Supports method overloading and overriding
Errors can be handled with the use of Exception
Automatic garbage collection
8. STARTING THE BASICS…
File name: Abc.java
Code:
class abc
{
public static void main(String args[])
{
System.out.print("hello, how are you all??");
}
}
9. Meaning of each term
Public: visibility mode
Static: to use without creating object
Void: return type
String: pre-defined class
Args: array name
System: pre-defined class
Out: object
Print: method
Make sure:
No need of saving file with initial capital
letter
File name can be saved with the different
name of class name
10. String to integer and double
class conv
{
public static void main(String a[])
{
int a;
String b="1921";
double c;
a=Integer.parseInt(b);
System.out.println(a);
c=Double.parseDouble(b);
System.out.println(c);
}
}
11. Final variable
Value that will be constant through out the program.
Can not assign another value.
Study following program.
class fin
{
public static void main(String a[])
{
final int a=9974;
System.out.print(a);
a=759;
System.out.print(a);
}
}
12. Errors and exception
What is the difference???
Errors
Something that make a program go wrong.
Can give unexpected result.
Types:
Compile-time errors
Run-time errors
Exception
Condition that is caused by a run-rime error in the program.
Ex. Dividing by zero.
Interpreter creates an exception object and throws it.
13. errors
Compile-time Error
Occurs at the time of compilation.
Syntax errors
Detected and displayed by the interpreter.
.class file will not be created
For successful compilation it need to be fixed.
For ex. Missing semicolon or missing brackets.
14. More examples
Misspelling of identifier or keyword
Missing double quotes in string
Use of undeclared variable
Use of = in place of == operator
Path not found
Changing the value of variable which is declared final
15. errors
Run-time Error
Program compile successfully
Class file also generated.
Though may not run successfully
Or may produce wrong o/p due to wrong logic
Error message generated
Program aborted
For ex. Divide by zero.
16. More examples
Accessing an element that is out of bound of an array.
Trying to store a value into an array of an incompatible class or type.
Passing a parameter that is not in a valid range.
Attempting to use a negative size for an array.
Converting invalid string to a number
Accessing a character that is out of bound of a string.
17. class Err
{
public static void main(String bdnfs[])
{
int a=50,b=10,c=10;
int result=a/(b-c);
System.out.print(result);
int res=a/(b+c);
System.out.print(res);
}
}
WHICH ONE IS THIS??
18. exception
Caused by run-time error in the program.
If it is not caught and handled properly, the interpreter will display an error message.
Ex. ArithmeticException
ArrayIndexOutOfBoundException
FileNotFoundException
OutOfMemoryExcepion
SecurityException
StackOverFlowException
19. Exception HANDLING
In previous program , if we want to continue the execution with the remaining
code, then we should try to catch the exception object thrown by error condition
and then display an appropriate message for taking correct actions.
This task Is known as Exception Handling.
The purpose of this is to provide a means to detect and report circumstances.
So appropriate action can be taken
It contains 4 sub tasks.
Find the problem(Hit)
Inform that error has occurred(Throw)
Receive the error Information(Catch)
Take corrective action(Handle)
21. example
class Err2
{
public static void main(String bdnfs[])
{
int a=50,b=10,c=10;
int result,res;
try
{
result=a/(b-c);
}
catch (ArithmeticException e)
{
System.out.println("can not divided by zero ");
}
res=a/(b+c);
System.out.print(res);
}
}
22. Multiple catch statements
………………………….
………………………….
Try
{
statements; // generates an Exception
}
Catch (Exception-type-1 e)
{
statements; // processes the Exception type 1
}
Catch (Exception-type-2 e)
{
statements; // processes the Exception type 2
}
.
.
.
.
Catch (Exception-type-N e)
{
statements; // processes the Exception type N
}
………………………....
…………………………
23. Finally statement
Finally statement is supported by Java to handle a type of exception
that is not handled by catch statement.
It may be immediately added after try block or after the last catch
block.
Guaranteed to execute whether the exception Is thrown or not.
Can be used for performing certain house-keeping operation such a
closing files and realizing system resources.
Syntax for using finally statement is shown in next slide.
24. Syntax
Try
{
…………..
…………..
}
Catch (……….)
{
…………..
…………..
}
Finally
{
…………..
…………..
}
Decide according to
program that
whether to use catch
block or not…
25. class Err3
{
public static void main(String bdnfs[])
{
int a[]={50,100};
int x=5;
try
{
int p=a[2]/(x-a[0]);
}
finally
{
int q=a[1]/a[0];
System.out.println(q);
}
}
}
example
26. Some puzzles..
String mesg = “Answer is “; int sum = 1 + 2; System.out.println(mesg + sum);
Output: “Answer is 3”
int sum = 5; sum = sum + sum *5/2; System.out.println(sum);
Output: 17
int limit = 25; int count = 30; int total = 200; count *=5; limit -=5; total +=count
+ limit; System.out.println("total =" + total);
Output: 370
String str1 = "Java"; String str2 = "Java program"; String str3 = "program";
char c = ' '; String s1 = str1 + str3; String s2 = str1 + "c"; String s3 = str1 + c;
String s4 = “ “; s4 += str1; String s5 = s4 + str3;
Output: “Javac”