What is Java?
Java is a Object
Oriented Programming platform independent language
Java is a high level,
robust, secured and object-oriented programming language.
Platform: Any hardware or
software environment in which a program runs is known as a platform. Since Java
has its own runtime environment (JRE) and API, it is called platform.
Uses Of JAVA:
- Desktop Applications such as acrobat reader,
media player, antivirus etc.
- Web Applications such as irctc.co.in.
- Enterprise Applications such as banking applications.
- Mobile
- Embedded System
- Smart Card
- Robotics
- Games etc.
Types of Java Applications
There are mainly 4 types
of applications that can be created using java programming:
1)
Standalone Application
It is also known as
desktop application or window-based application. An application that we need to
install on every machine such as media player, antivirus etc.
2) Web Application
An application that runs
on the server side and creates dynamic page, is called web application.
Currently, servlet, jsp, struts, spring, jsf etc. technologies are used for
creating web applications in java.
3) Enterprise Application
An application that is
distributed in nature, such as banking applications etc. It has the advantage
of high level security, load balancing and clustering. In java, EJB and Spring
are used for creating enterprise applications.
4) Mobile Application
An application that is
created for mobile devices. Currently Android and Java ME are used for creating
mobile applications.
History of Java
The history of java
starts from Green Team. Team initiated a revolutionary task to develop a
language for digital devices such as
set-top boxes, televisions etc.
For the green team
members, it was an advance concept at that time. But, it was suited for internet programming. Later, Java
technology as incorporated by Netscape.
Currently, Java is used
in internet programming, mobile devices, games, e-business solutions etc. There
are given the major points that describes the history of java.
1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java
language project in June 1991. The small team of sun engineers called Green Team.
2) Originally designed
for small, embedded systems in electronic appliances like set-top boxes.
3) Firstly, it was
called "Greentalk" by James Gosling and
file extension was .gt.
4) After that, it was
called Oak and was developed as a
part of the Green project.
Why Oak name for java language?
5) Oak is a symbol of
strength and choosen as a national tree of many countries like U.S.A., France,
Germany, Romania etc.
6) In 1995, Oak was
renamed as "Java".
Why Java name for java language?
7) Why they choosed java
name for java language? The team gathered to choose a new name.
The suggested words were "dynamic", "revolutionary",
"Silk", "jolt", "DNA" etc. They wanted something
that reflected the essence of the technology: revolutionary, dynamic, lively,
cool, unique, and easy to spell and fun to say. Most of the team members preferred java.
8)
Java is an island of Indonesia where first coffee was produced (called java
coffee).
9) Notice that Java is
just a name not an acronym.
10) Originally developed
by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle
Corporation) and released in 1995.
11) In 1995, Time
magazine called Java one of the Ten Best Products of 1995.
12) JDK 1.0 released in (January
23, 1996).
There are many java
versions that have been released. Current stable release of Java is Java SE 8.
- JDK Alpha and Beta (1995)
- JDK 1.0 (23rd Jan, 1996)
- JDK 1.1 (19th Feb, 1997)
- J2SE 1.2 (8th Dec, 1998)
- J2SE 1.3 (8th May, 2000)
- J2SE 1.4 (6th Feb, 2002)
- J2SE 5.0 (30th Sep, 2004)
- Java SE 6 (11th Dec, 2006)
- Java SE 7 (28th July, 2011)
- Java SE 8 (18th March, 2014)
Features of Java
Below are the features of Java.
1. Simple
2. Object-Oriented
3. Platform
independent
4. Secured
5. Robust
6. Portable
7.
Dynamic
8. High
Performance
9. MultiThreading
10. Distributed
1. Simple
According to Sun, Java language is simple
because:
Syntax
is based on C++ (so easier for programmers to learn it after C++).
Removed
many confusing topics and/or rarely-used features e.g., explicit pointers,
operator overloading, Multiple Inheritance etc.
No
need to remove unreferenced objects because there is Automatic Garbage
Collection in java.
2. Object-oriented
JAVA
follows all basic concepts of OOPs:
1. Inheritance
2. Polymorphism
3. Encapsulation
4. Abstraction
3. Platform
Independent
A platform is the hardware or software environment
in which a program runs. There are two types of platforms software-based and
hardware-based. Java provides software-based platform. The Java platform
differs from most other platforms in the sense that it's a software-based
platform that runs on top of other hardware-based platforms.
Java
code can be run on multiple platforms e.g.Windows,Linux,Sun Solaris,Mac/OS etc.
Java code is compiled by the compiler and converted into bytecode. This
bytecode is a platform independent code because it can be run on multiple
platforms i.e. Write Once and Run Anywhere(WORA).
4. Robust
Robust
simply means strong. All these points makes java robust.
Java
uses strong memory management.
There
are lack of pointers that avoids security related problems.
There
is automatic garbage collection, it maintains good memory management.
There
is exception handling
Type
checking mechanism
5. Secured
Java
is secured because:
No
explicit pointer
Programs
run inside virtual machine sandbox.
Classloader-
adds security by separating the package for the classes of the local file
system from those that are imported from network sources.
Bytecode
Verifier- checks the code fragments for illegal code that can violate access
right to objects.
Security
Manager- determines what resources a class can access such as reading and
writing to the local disk.
These securities are provided by java
language. Some security can also be provided by application developer through
SSL,JAAS,cryptography etc.
6. Portable
We
may carry the java bytecode to any platform.
7. High-performance
Java is faster than traditional
interpretation since byte code is "close" to native code still
somewhat slower than a compiled language (e.g., C++)
8. Distributed
We can create distributed applications in
java. RMI, EJB and Spring are used for creating distributed applications. We
may access files by calling the methods from any machine on the internet.
9. Multi-threaded
A thread is like a separate program,
executing concurrently. We can write Java programs that deal with many tasks at
once by defining multiple threads. The main advantage of multi-threading is
that it shares the same memory. Threads are important for multi-media, Web
applications etc.
|
Understanding first java program
public class FirstJava{
public static void main(String[] args){
System.out.println(“Just for test”);
}
}
Let's see what is the meaning of class,
public, static, void, main, String[], System.out.println().
- class keyword is used to declare a
class in java.
- public keyword is an access modifier
which represents visibility, it means it is visible to all.
- static is a keyword, if we declare any
method as static, it is known as static method. The core advantage of
static method is that there is no need to create object to invoke the
static method. The main method is executed by the JVM, so it doesn't
require to create object to invoke the main method. So it saves memory.
- void is the return type of the
method, it means it doesn't return any value.
- main represents startup of the
program.
- String[] args is used for command line argument.
- System.out.println() is used print statement.
Valid java main method signature
1. public
static void main(String[] args)
2. public
static void main(String []args)
3. public
static void main(String args[])
4. public
static void main(String... args)
5. static
public void main(String[] args)
6. public
static final void main(String[] args)
7. final
public static void main(String[] args)
Invalid java main method signature
1. public
void main(String[] args)
2. static
void main(String[] args)
3. public
void static main(String[] args)
4. abstract
public static void main(String[] args)
Classloader: is the subsystem of
JVM that is used to load class files.
|
Bytecode
Verifier: checks
the code fragments for illegal code that can violate access right to objects.
|
Interpreter: read bytecode stream
then execute the instructions.
|
JDK, JRE and JVM:
JVM
It is a specification
that provides runtime environment in which java bytecode can be executed.
JVM, JRE and JDK are
platform dependent because configuration of each OS differs. But, Java is
platform independent.
The JVM performs
following main tasks:
- Loads code
- Verifies code
- Provides runtime environment
- Executes code
JRE
JRE is an acronym for
Java Runtime Environment which is used to provide runtime environment. It is
the implementation of JVM and physically exists. It contains set of libraries +
other files that JVM uses at runtime.
JDK
JDK is an acronym for
Java Development Kit. It is physically exists. It contains JRE + development tools.
Java If-else Statement
The Java if
statement is
used to test the condition. It returns true or false. There are various
types of if statement in java.
- if statement
- if-else statement
- nested if statement
- if-else-if ladder
Java if statement
If statement tests the
condition. It executes if statement if condition is true.
Syntax:
if(condition){
//code to be executed
}
Java if-else statement
The if-else statement
also tests the condition. It executes if block if condition is true
otherwise else block.
Syntax:
if(condition){
//code if condition is true
}else{
//code if condition is false
}
Java if-else-if ladder statement
The if-else-if ladder
statement executes one condition from multiple statements.
Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Java Switch statement
The Java switch
statement executes
one statement from multiple conditions. It is like if-else-if ladder statement.
Syntax:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Ex:
public class Ex {
public static void main(String[] args) {
int number=20;
switch(number){
case 10: System.out.println("10");break;
case 20: System.out.println("20");break;
case 30: System.out.println("30");break;
default:System.out.println("Not in 10, 20 or 30");
}
}
}
Java For Loop
The Java for
loop is
used to iterate a part of the program several times.
There are three types of
for loop in java.
- Simple For Loop
- For-each or Enhanced For Loop
- Labeled For Loop
Java Simple For Loop
We can initialize
variable, condition verification and increment/decrement value.
Syntax:
for(initialization;condition;incr/decr){
//code to be executed
}
Java For-each Loop
The for-each loop is
used to traverse array or collection in java. It is easier to use than simple
for loop because we don't need to increment value and use subscript notation.
It works on elements
basis not index. It returns element one by one in the defined variable.
Syntax:
for(Type var:array){
//code to be executed
}
Example:
public class EX {
public static void main(String[] args) {
int arr[]={12,23,44,56,78};
for(int i:arr){
System.out.println(i);
}
}
}
Java Labeled For Loop
We can have name of each
for loop. To do so, we use label before for loop. It is useful if we have
nested for loop so that we can break/continue specific for loop.
Syntax:
labelname:
for(initialization;condition;incr/decr){
//code to be executed
}
Example:
Example:
public class EX {
public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break aa;
}
System.out.println(i+" "+j);
}
}
}
}
Java Infinitive for Loop
If you use two
semicolons ;; in the for loop, it will be infinitive for loop.
Syntax:
for(;;){
//code to be executed
}
Java While Loop
The Java while
loop is
used to iterate a part of the program several times.
Syntax:
while(condition){
//code to be executed
}
Java Infinitive While Loop
If you pass true in the while loop, it
will be infinitive while loop.
Syntax:
while(true){
//code to be executed
}
Java do-while Loop
The Java do-while
loop is
used to iterate a part of the program several times. If you must have to
execute the loop at least once, it is recommended to use do while loop.
It is executed at least
once because condition is checked after loop body.
Syntax:
do{
//code to be executed
}while(condition);
Java Break Statement
The Java break is used to break loop or
switch statement. It breaks the current flow of the program at specified
condition. In case of inner loop, it breaks only inner loop.
Syntax:
jump-statement;
break;
public class EX {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
if(i==5){
break;
}
System.out.println(i);
}
}
}
}
Java Break Statement with Inner Loop
It breaks inner loop
only if you use break statement inside the inner loop.
public class Ex2 {
public static void main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break;
}
System.out.println(i+" "+j);
}
}
}
}
Java Continue Statement
The Java continue
statement is
used to continue loop. It continues the current flow of the program and skips
the remaining code at specified condition. In case of inner loop, it continues
only inner loop.
Syntax:
jump-statement;
continue;
Java Continue Statement Example
public class EX {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
if(i==5){
continue;
}
System.out.println(i);
}
}
}
Java Continue Statement with Inner Loop
It continues inner loop
only if you use continue statement inside the inner loop.
public class EX {
public static void main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i==2&&j==2){
continue;
}
System.out.println(i+" "+j);
}
}
}
}
No comments:
Post a Comment