Introduction To Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

History of JAVA :

Java is a programming language and a platform.

Java is a high level, robust, object-oriented and secure programming language.

Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995.

James Gosling is known as the father of Java. Before Java, its name was Oak. Since Oak was already a
registered company, so James Gosling and his team changed the name from Oak to Java.

Java Applications :
There are mainly 4 types of applications that can be created using Java programming:

1) Standalone Application

Standalone applications are also known as desktop applications or window-based applications. These are
traditional software that we need to install on every machine. Examples of standalone application are Media
player, antivirus, etc. AWT and Swing are used in Java for creating standalone applications.

2) Web Application

An application that runs on the server side and creates a dynamic page is called a web application.
Currently, Servlet, JSP, Struts, Spring, Hibernate, 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. is called an enterprise
application. It has advantages like high-level security, load balancing, and clustering. In Java, EJB is used
for creating enterprise applications.

4) Mobile Application

An application which is created for mobile devices is called a mobile application. Currently, Android and
Java ME are used for creating mobile applications.
Features of Java :

There is given many features of java. They are also known as java buzzwords. The Java Features given
below are simple and easy to understand.

o Simple
o Secure
o Portable
o Object-oriented
o Robust
o Multithreaded
o Interpreted
o High performance
o Dynamic

1 Simple : Java was designed to be easy for the professional programmer to learn and use effectively.
2 Secure : Once the byte code generated, the code can be transmitted to other computer in the world
without knowing the internal details of the source code.

3 Portable : The byte code can be easily carried from one machine to other machine.
4 Object Oriented: Everything in Java is an Object. The object model in Java is simple and easy to extend, While
primitive types, such as integers, are kept as high-performance non-objects.

5 Robust : Java is Robust in terms of memory management and mishandled exceptions. Java provides
automatic memory management and also provides well defined exception handling mechanism.

6 Multithreaded : Java was designed to meet the real-world requirement of creating interactive, networked
programs. To accomplish this, Java supports multithreaded programming, which allows you to write programs that do
many things simultaneously.

7 Interpreted and High Performance: Java enables the creation of cross-platform programs by compiling into
an intermediate representation called Java byte code. This code can be executed on any system that implements the
Java Virtual Machine. Most previous attempts at cross-platform solutions have done so at the expense of
performance. As explained earlier, the Java byte code was carefully designed so that it would be easy to translate
directly into native machine code for very high performance by using a just-in-time compiler.

8 Dynamic: Java programs carry with them substantial amounts of run-time type information that is used to verify
and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safeand expedient
manner.
Java Program Structures
Simple Java Program

class Example
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}

Description of the every line of the program :

The first line contains the keyword class and class name, which actually the basic unit for encapsulation, in
which data and methods are declared.

Second line contains "{" which indicates the beginning of the class.

Third line contains the


public static void main(String args[])

where public is access specifier, when a member of a class is made public it can be accessed by the outside
code also. The main function is the beginning of from where execution starts. Java is case-sensitive. "Main"
is different from the "main". In main there is one parameter, String args, which is used to read the command
line arguments.

Fourth line contains the "{", which is the beginning of the main function. Fifth line

contains the statement

System.out.println("Hello World");

Here "System" is the predefined class, that provides access to the system, and out is the output stream that is
used to connect to the console. The println(), is used to display string passed to it. This can even display other
information to.

Difference between JDK, JRE, and JVM

JVM
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn't physically exist.
It is a specification that provides a runtime environment in which Java bytecode can be executed. It can also run
those programs which are written in other languages and compiled to Java bytecode.

The JVM performs the following main tasks:


o Loads code
o Verifies code
o Executes code
o Provides runtime environment

JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java Runtime Environment
is a set of software tools which are used for developing Java applications. It is used to provide the runtime
environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that
JVM uses at runtime.

JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development
environment which is used to develop Java applications and applets. It physically exists. It contains JRE +
development tools. The JDK contains a private Java Virtual Machine (JVM) and a few other resources.
Java Comments
The java comments are statements that are not executed by the compiler and interpreter. The comments
can be used to provide information or explanation about the variable, method, class or any statement. It
can also be used to hide program code for specific time.

There are 2 types of comments in java.

1. Single Line Comment


2. Multi Line Comment

1) Java Single Line Comment

The single line comment is used to comment only one line.

Syntax: 1. //This is single line comment


Example:

class CommentExample1
{
public static void main(String[]
args)

int i=10;//Here, i is a variable


System.out.println(i);

}
}

2) Java Multi Line Comment

The multi line comment is used to comment multiple lines of code.

Syntax: /* This is
multi line
comment
*/
Example:

class CommentExample2
{
public static void main(String[] args)
{
/* Let's declare and
print variable in java.
*/
int i=10;
System.out.println(i);
}
}
Data Types in Java
Data types specify the different sizes and values that can be stored in the variable. There are
two types of data types in Java:

1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.

Data Type Default Value Default size

Boolean False 1 bit

Char '\u0000' 2 byte

Byte 0 1 byte

Short 0 2 byte

Int 0 4 byte

Long 0L 8 byte

Float 0.0f 4 byte

Double 0.0d 8 byte


Variables in Java
Variable is a name of memory location. There are three types of variables in
java: local, instance and static.
There are three types of variables in java:
o local variable
o instance variable
o static variable
1)Local Variable
A variable which is declared inside the method is called local variable.

2) Instance Variable
A variable which is declared inside the class but outside the method, is called
instance variable . It is not declared as static.

3)Static variable
A variable that is declared as static is called static variable. It

cannot be local. We will have detailed learning of these

Example:

class A

Int data=50;//instance variable

static int m=100;//static variable

void method()

int n=90;//local variable


}
}//end of class
Operators in java
Operator in java is a symbol that is used to perform operations. For example:
+, -, *, / etc. There are many types of operators in

Operator Category Precedence


Type

Increment postfix expr++ expr--


and
Decrement prefix ++expr --expr +expr -expr ~ !

Arithmetic multiplicative */%

additive +-

Relational comparison < > <= >= instanceof

equality == !=

Bitwise bitwise AND &

bitwise exclusive OR ^

bitwise inclusive OR |

Logical logical AND &&

logical OR ||

Ternary ternary ?:

Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=


1) Increment and Decrement
The ++ and the – – are Java’s increment and decrement operators. The increment operator
increases its operand by one. The decrement operator decreases its operand by one. For
example, this statement:
x = x + 1;
can be rewritten like this by use of the increment operator:
x++;
Similarly, this statement:
x = x - 1; is equivalent to x--;

we write increment/decrement operator after the operand such expression is called post
increment decrement expression, if written before operand such expression is called pre
increment/decrement expression

class Main
{
static void main(String[] args)
{

// declare variables
int a = 12, b = 12;
int result1, result2;

// original value
System.out.println("Value of a: " + a);

// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);

System.out.println("Value of b: " + b);

// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
}
2) Arithmetic Operators
Arithmetic operators are used in mathematical expressions
The operands of the arithmetic operations are of numeric type.The basic arithmetic operators
are: addition, subtraction, multiplication, and division.

Example:

class Arithmetic
{
public static void main(String[] args)
{

// declare variables
int a = 12, b = 5;

// addition operator
System.out.println("a + b = " + (a + b));

// subtraction operator
System.out.println("a - b = " + (a - b));

// multiplication operator
System.out.println("a * b = " + (a * b));

// division operator
System.out.println("a / b = " + (a / b));

// modulo operator
System.out.println("a % b = " + (a % b));
}
}
3) Relational Operators
Relational operators are used to check the relationship between two operands.

Example:

class Relational
{
public static void main(String[] args)
{
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}
4) Logical Operators
Logical operators are used to check whether an expression is true or false. They are used
in decision making.

Example

class Logical
{
public static void main(String[] args)
{
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false

// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false

// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}

5) Assignment Operators

Assignment operators are used in Java to assign values to variables. For example,

int a;

a = 20;

Here, = is the assignment operator. It assigns the value on its right to the variable on its left.
That is, 5 is assigned to the variable a
6) Ternary Operator

The ternary operator (conditional operator) is shorthand for the if-then-else statement. For
example, Here's how it works.
• If the Expression is true, expression1 is assigned to the variable.
• If the Expression is false, expression2 is assigned to the variable.
Example

class Ternary
{
public static void main(String[] args)
{

int februaryDays = 29;


String result;

// ternary operator
result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
System.out.println(result);
}
}

Operator Precedence:

Operator precedence determines the order in which the operators in an expression are

evaluated. Before you start reading this article, you should have a basic knowledge of Java

Operators.

12-(4*2)

When two operators share a common operand, 4 in this case, the operator with the highest
precedence is operated first.
In Java, the precedence of * is higher than that of -. Hence, the multiplication is performed
before subtraction, and the value of myInt will be 4.
Operator Precedence Table

The table below lists the precedence of operators in Java; higher it appears in the table, the

higher its precedence

You might also like