INTRODUCTION TO JAVA
INSTALLATION
• Download and Install jdk
– http://www.oracle.com/technetwork/java/javase/
downloads/index.html
• Download Eclipse Classic IDE
– http://www.eclipse.org/downloads/
• Open Eclipse IDE. Create a new folder as it will
prompt for a workspace.
– Eclipse stores your projects in a folder called a
workspace.
FEATURES
• Open Source
• Initially developed for Electronic Products
• Simple – Syntax is been derived from C/C++
programming.
• It is a platform
• Platform Independent
– WORA (Write Once, Run Anywhere) – It can be
compiled only once and can run on any platform
such as Windows, Mac, Unix, Linux, etc
FEATURES
• High Security – No Pointers are used, uses its own
environment JVM for Execution.
• Object Oriented Programming
• Robust – Java is Strong as it has strong memory
management, automatic garbage collection, exception
handling and type checking
• Distributed – allows creating distributed applications
using RMI and EJB technologies.
• Multithreaded –using threads we can execute many
tasks at a time and it shares the same memory.
• Case Sensitive – Eg: “Hello” and “hello” are different.
TYPES OF JAVA APPLICATIONS
• Standalone Applications – Also called Desktop /
Window based Applications. It uses AWT or Swings.
– Eg: Media Player, Anti-virus
• Web Applications – An Application that runs on the
server side and produces dynamic pages. JSP, Servlets,
JSF, Struts, etc are used.
• Enterprise Applications – An Application which is
distributed in nature such as Banking Application
which involves high security, load balancing, clustering,
etc. EJB technology is been used.
• Mobile Application – An Application for mobile
devices. Android and J2ME technologies are used.
FIRST JAVA PROGRAM
• Created via IDE and Command Prompt
• Open Eclipse IDE
• Open File -> New Project -> Java Project
• In Package Explorer, right click Project folder -
> New -> Class
• Specify the Class Name and click Finish
FIRST JAVA PROGRAM
• Type the following:
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!!");
}
}
• Run the project.
EXPLANATION OF SIMPLE PROGRAM IN
DETAIL
• public – keyword : This class can be accessed
by any other class.
• class – keyword : It is used to declare as a class
and can be instantiated with objects or can be
inherited by another class.
• Class name – (HelloWorld) : Class name and
File name must be same.
EXPLANATION OF SIMPLE PROGRAM IN
DETAIL
• main function – starting point of execution of
program:
– public - specifies that main function is accessible
by any other function in other classes.
– static – main function can be called without
creating objects.
– void – specifies that main function will not return
any values
EXPLANATION OF SIMPLE PROGRAM IN
DETAIL
• main function – starting point of execution of
program:
– String args[] – command line argument which is a
string array. This allows main method to be
invoked with array of string objects during the
time of execution.
– System.out.println() – is used to print the data
where data must be given within double quotes.
COMMENTS
• // comment - Single line comment
• /* comment */ - Multi line comment
• /** documentation comment */
TYPES OF VARIABLES
• Local variables – they are declared inside a method
• Instance variables – they are declared inside a class
but outside methods. Separate copies are created for
this variable when they are accessed by many
instances of the class.
• Static variables – they are declared with a keyword
“static” and it can be accessed without any creation
of objects. Only one copy of this variable will be
available for all instances of the class.
DATATYPES
• Primitive Data Type:
– byte – 1 byte
– char – 2 byte
– short – 2 byte
– int – 4 byte
– long – 8 byte
– float – 4 byte
– double – 8 byte
– Boolean – true / false
• Non Primitive Data Type: String, Array, etc (uses
new operator)
OPERATORS
• Arithmetic Operators : +, -, /, *, %, ++, --
• Relational Operators : ==, !=, >, <, >=, <=
• Bitwise Operators : & (Binary AND)
» | (Binary OR)
» ^ (Binary XOR)
» ~ (Binary Ones Complement)
» << (Binary Left Shift)
» >> (Binary Right Shift)
» >>> (Shift right zero fill)
• Logical Operators : && (Logical AND)
» || (Logical OR)
» ! (Logical NOT)
• Assignment Operators : =, +=, -=, *=, /=, %=, <<=, >>=, &=, |=,
^=
KEYWORDS
• Some of the 50 Java keywords are
– public
– abstract
– final
– if
– else
– static
– while, etc..