Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Introduction to Java Programming
R.Saravanakumar., MCA
Developer, Trainer, Consultant
Introduction
• JAVA was developed by Sun Microsystems Inc in 1991, later
acquired by Oracle Corporation.
• It was conceived by James Gosling and Patrick Naughton. It is
a simple programming language.
• Writing, compiling and debugging a program is easy in java. It
helps to create modular programs and reusable code.
Features of JAVA
• Java is a platform independent language
• Java is an Object Oriented language
– Object oriented programming is a way of organizing programs as
collection of objects, each of which represents an instance of a class.
– 4 main concepts of Object Oriented programming are:
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
Features of JAVA
• Simple
• Robust Language
• Secure
• Java is distributed
• Java is distributed
• Multithreading
• Portable
Compilation and Execution of Java Program
• To create a java code an editor such as
notepad, text pad or an IDE like eclipse can be
used.
• In the above program the class FirstJavaProgram
has public access and hence declared public.
• ‘class’ is the keyword used to create a class.
• For running stand-alone programs ‘main’ method
is needed which has a signature similar to the
one defined in the above program.
• ‘Main’ method takes an array of strings as an
argument. The name of the array can be
anything.
• To display the output, pass the string as an
argument to the method System.out.println.
Steps for compilation and Execution
• Step1: Save the source file as WelcomeApp.java.
• Step2: Open command prompt and navigate to
the directory where you have stored the file.
• Step 3: To compile, type javac
FirstJavaProgram.java and press Enter.
• Step 4: On successful compilation, you will see the command
prompt and FirstJavaProgram.class file in the same folder where
WelcomeApp.java is stored. This is the byte code of the program.
• Step 5: To execute, type java FirstJavaProgram. Do not type the
extension while executing.
• Step 6: See the output “This is my first program in java” displayed
on the console.
Common programming Errors in Java
• ‘javac’ is not recognized as an internal or external
command, operable program or batch file
• Exception in thread
“main”java.lang.NoClassDefFoundError:
FirstJavaProgram
Another Example
Interpreting the code
a) Line 1. The package FirstCode creates a folder to store the class
files generated after compilation
b) Line2. It imports the class library java.lang and its subsequent
classes
c) Line 3. Initiates a class with the name WelcomeMessage
d) Line 5. Declares a method with name printMessage
Interpreting the code
e) Line 7. Defines the actual working code of the method
f) Line 10. Initiates the class having the main method; it should bear the name of the file :
Myclass.java
g) Line 12. Declares the main method
h) Line 14. Initiates the creation of the object
i) Line 15. Calls the method printMessage () with the help of the object
j) The above code is saved and compiled to run on JVM
The programming structure
1) The programming pattern is divided into classes which has meth0d definitions
2) This assists in distributing the code into smaller units
3) The libraries can be used over and over again
4) These codes generated here can be called in another program if required
5) The memory allocation is done only after the execution of the new keyword
6) It gets easier to collect memory that does not has any future use
Java Tutorial – JVM (Java virtual Machine)
1) Class loader accepts class files
2) Compilation creates class files
3) The interim memory is required during execution
4) It consists of heaps, stacks and registers to store data
5) JRE has native methods and libraries
6) JVM runs two main threads
a) demon
b) Non-demon threads
Demon Threads
• It has been Run by JVM for itself. Used for
garbage collection. JVM decides on a thread
for being a demon thread
Non-demon threads
• main() is the initial and non-demon thread.
Other implemented threads are also non-
demon threads. The JVM is active till any non-
demon thread is active.
Execution on JVM
• 1) JVM executes Java byte codes
2) Other programming language codes if converted to adequate Java byte
code can be executed on JVM
3) JVM is different for different platforms and can also act as a platform
itself
4) JVM supports automatic error handling by intercepting the errors
which can be controlled
5) This feature is useful in platform independency and multi user ability of
Java.
Compilation
• 1) The compiler requires to know the TYPE of every CLASS used in
the program source code
2) This is done by setting a default user environment variable
CLASSPATH
3) The Javac (Java Compiler) reads the program and converts it into
byte code files called as class files
Introduction to Java Programming
Java Source code
• 1) It essentially consists of a main() method
2) This method is public and thus can be called by any object
3) This method is also static and so can be called without instantiating the object
of the class
4) It does not return any value
5) The controlling class of every Java application usually contain a main method
6) This can be avoided to allow the class to be tested in a stand-alone mode.
7) Other methods can subsequently be called in main()
End

More Related Content

Introduction to Java Programming

  • 1. Introduction to Java Programming R.Saravanakumar., MCA Developer, Trainer, Consultant
  • 2. Introduction • JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. • It was conceived by James Gosling and Patrick Naughton. It is a simple programming language. • Writing, compiling and debugging a program is easy in java. It helps to create modular programs and reusable code.
  • 3. Features of JAVA • Java is a platform independent language • Java is an Object Oriented language – Object oriented programming is a way of organizing programs as collection of objects, each of which represents an instance of a class. – 4 main concepts of Object Oriented programming are: • Abstraction • Encapsulation • Inheritance • Polymorphism
  • 4. Features of JAVA • Simple • Robust Language • Secure • Java is distributed • Java is distributed • Multithreading • Portable
  • 5. Compilation and Execution of Java Program • To create a java code an editor such as notepad, text pad or an IDE like eclipse can be used.
  • 6. • In the above program the class FirstJavaProgram has public access and hence declared public. • ‘class’ is the keyword used to create a class. • For running stand-alone programs ‘main’ method is needed which has a signature similar to the one defined in the above program.
  • 7. • ‘Main’ method takes an array of strings as an argument. The name of the array can be anything. • To display the output, pass the string as an argument to the method System.out.println.
  • 8. Steps for compilation and Execution • Step1: Save the source file as WelcomeApp.java. • Step2: Open command prompt and navigate to the directory where you have stored the file. • Step 3: To compile, type javac FirstJavaProgram.java and press Enter.
  • 9. • Step 4: On successful compilation, you will see the command prompt and FirstJavaProgram.class file in the same folder where WelcomeApp.java is stored. This is the byte code of the program. • Step 5: To execute, type java FirstJavaProgram. Do not type the extension while executing. • Step 6: See the output “This is my first program in java” displayed on the console.
  • 10. Common programming Errors in Java • ‘javac’ is not recognized as an internal or external command, operable program or batch file • Exception in thread “main”java.lang.NoClassDefFoundError: FirstJavaProgram
  • 12. Interpreting the code a) Line 1. The package FirstCode creates a folder to store the class files generated after compilation b) Line2. It imports the class library java.lang and its subsequent classes c) Line 3. Initiates a class with the name WelcomeMessage d) Line 5. Declares a method with name printMessage
  • 13. Interpreting the code e) Line 7. Defines the actual working code of the method f) Line 10. Initiates the class having the main method; it should bear the name of the file : Myclass.java g) Line 12. Declares the main method h) Line 14. Initiates the creation of the object i) Line 15. Calls the method printMessage () with the help of the object j) The above code is saved and compiled to run on JVM
  • 14. The programming structure 1) The programming pattern is divided into classes which has meth0d definitions 2) This assists in distributing the code into smaller units 3) The libraries can be used over and over again 4) These codes generated here can be called in another program if required 5) The memory allocation is done only after the execution of the new keyword 6) It gets easier to collect memory that does not has any future use
  • 15. Java Tutorial – JVM (Java virtual Machine)
  • 16. 1) Class loader accepts class files 2) Compilation creates class files 3) The interim memory is required during execution 4) It consists of heaps, stacks and registers to store data 5) JRE has native methods and libraries 6) JVM runs two main threads a) demon b) Non-demon threads
  • 17. Demon Threads • It has been Run by JVM for itself. Used for garbage collection. JVM decides on a thread for being a demon thread
  • 18. Non-demon threads • main() is the initial and non-demon thread. Other implemented threads are also non- demon threads. The JVM is active till any non- demon thread is active.
  • 19. Execution on JVM • 1) JVM executes Java byte codes 2) Other programming language codes if converted to adequate Java byte code can be executed on JVM 3) JVM is different for different platforms and can also act as a platform itself 4) JVM supports automatic error handling by intercepting the errors which can be controlled 5) This feature is useful in platform independency and multi user ability of Java.
  • 20. Compilation • 1) The compiler requires to know the TYPE of every CLASS used in the program source code 2) This is done by setting a default user environment variable CLASSPATH 3) The Javac (Java Compiler) reads the program and converts it into byte code files called as class files
  • 22. Java Source code • 1) It essentially consists of a main() method 2) This method is public and thus can be called by any object 3) This method is also static and so can be called without instantiating the object of the class 4) It does not return any value 5) The controlling class of every Java application usually contain a main method 6) This can be avoided to allow the class to be tested in a stand-alone mode. 7) Other methods can subsequently be called in main()
  • 23. End