Content
• JAVA – Introduction
• Why learn JAVA?
• Features of JAVA
• JAVA Environment
• JAVA basic Syntax
• Compiling Program
• Comments in JAVA
• Data Types
• Data Operations
1
JAVA - Introduction
Created by James Gosling in 1995 for Sun Oak
Microsystems.
Platform independent programming
language that follows the logic of
“Write Once, Run Anywhere”
Concurrency: Statements can be executed concurrently.
Saves time. Increases Efficiency.
Class based approach: Everything you write is going to
be inside a Class.
Object Oriented Style: Everything is w.r.t Objects 2
Why JAVA?? TIOBE Programming Index
Survey
by TIOBE
3
Why Learn JAVA?? Server side Applications will be written in JAVA.
Billing Application you see in any retail store is
written in JAVA. Android programs and android OS makes use of
JAVA and its APIs.
Your transaction management and transactional
programs are written in JAVA. Hadoop mainly uses JAVA.
Lots of Applications are developed using JAVA. Most
Lots of algorithms used here are written in JAVA.
preferred and used language in industry today.
4
Calculative as well as operational programs are written in JAVA.
Some Technologies that make use of JAVA
Automation Testing
Java Script and scripting languages
Android Applications and Android OS
Hadoop
Frameworks for standardising 5
applications
Features of JAVA
Secure
Simple
Portable
Object-Oriented Dynamic
Distributed Robust
High
Performance
6
Features of JAVA
Simple
Java was designed to be easy for professional
programmer to learn and use effectively.
Portable
Applications written on one platform of Java can be
easily ported to another platform as it is platform
independent.
7
Features of JAVA
Distributed
Java has a feature called Remote Method Invocation (RMI)
using which a program can invoke a method of another
program across a network and get the output.
Dynamic
Java programs carry with them substantial amounts of
runtime type information that is used to verify and resolve
accesses to objects at runtime.
8
Features of JAVA
Object-oriented
{OOPS} JAVA is an object oriented programming language. Everything
is considered to be an “object” and all operations are
performed using these objects.
Secure
Java does not use the explicit pointer and runs its programs
inside the sandbox to prevents any activities from untrusted
sources.
9
Features of JAVA
Robust
• JAVA checks the code during the compilation time and runtime
also.
• JAVA completely takes care of memory allocation and releasing,
which makes the JAVA program more robust.
High Performance
Java achieves high performance through the use of bytecode which can
easily translated into native machine code..
10
How does Java Work?
Run time environment
Compile time environment
Class Loader
Java Source Byte code
(.java) verifier
Java Class Libraries
Just in Time
Java Compiler Java Interpreter Compiler
Java bytecodes move
locally or through a
network Runtime system
Java Virtual Machine
Operating System
Java Bytecode
(.class) Hardware 11
JAVA Environment
Development
Tools
Java Class Libraries
JVM
Other Files
JRE
JDK
12
Status of Installation….??
13
JAVA Environment
• The programming environment of Java consists of three components mainly:
• JDK
• JRE
• JVM
• JDK(Java Development Kit) : JDK is intended for software developers and
includes development tools such as the Java compiler, Javadoc, Jar, and a debugger.
• JRE(Java Runtime Environment) : JRE contains the parts of the Java libraries
required to run Java programs and is intended for end users. JRE can be view as a
subset of JDK.
• JVM: JVM (Java Virtual Machine) is an abstract machine. It is a specification that
provides runtime environment in which java bytecode can be executed. JVMs are
available for many hardware and software platforms.
14
3 Primary components
JAVA basic Syntax 1. HelloWorld Class Definition
2. The main method
Class
/* Definition
This is a simple Java program. 3. Source code comments
FileName : "HelloWorld.java". */
class HelloWorld In JAVA, every application must contain
{ a main method whose signature is:
public static void main(String[] args)
// Your program begins with a call to
main(). Main Method public: So that JVM can execute the method
// Prints "Hello, World" to the terminal from anywhere.
window. static: Main method is to be called without
public static void main(String args[]) object.
{ The modifiers public and static can be
System.out.println("Hello, World"); written in either order.
void: The main method doesn't return
}
anything.
} main(): Name configured in the JVM.
Output: String[]: The main method accepts a single
Hello, World argument: an array of elements of type
String. 15
JAVA basic Syntax
/* This is a simple Java program.
System.out.println(“Hello, World”)
FileName : "HelloWorld.java". */
class HelloWorld
• This line outputs the string “Hello,
{ World” followed by a new line on the
// Your program begins with a call to screen.
main().
// Prints "Hello, World" to the terminal
window. • Output is actually accomplished by the
public static void main(String args[]) built-in println( ) method.
{
• System is a predefined class that
System.out.println("Hello, World“);
provides access to the system, and
}
} • out is the variable of type output stream
that is connected to the console.
16
Points to Remember:
• Your class name and file name has to be the same.
Filename : HelloWorld.java
Class name: class HelloWorld
{
}
• The class which contains the public main() method.
17
Compiling the program
javac HelloWorld.java
Compiler creates a file called HelloWorld.class.
[Bytecode version]
java HelloWorld
Java calls JVM specifying the class name.
This will print “Hello World” to the terminal
screen.
18
Comments in JAVA It helps to generate a documentation
page for later reference
• Single line comments • Documentation comments
//Comments here( Text in this /**Comment start
line only is considered as *
comment )
*tags are used in order to
• Multi-line comments specify a parameter
/*Comment starts *or method or heading
continues
*HTML tags can also be used
continues
. *such as <h1>
.
*
.
Commnent ends*/ *comment ends*/
19
ment canprogram
//Java be generated by using
to illustrate a tool used
frequently ‘javadoc’ :
* @return int This returns average of numA,
javadoc
// Comment tags FindAvg.java
numB and numC.
/**
* <h1>Find average of three numbers!</h1> */
* The FindAvg program implements an application public int findAvg(int numA, int numB,
that int numC)
* simply calculates average of three integers and {
Prints return (numA + numB + numC)/3;
* the output on the screen. }
*
/**
* @author Pratik Agarwal
* @version 1.0
* This is the main method which makes
* @since 2017-02-18 use of findAvg method.
*/ * @param args Unused.
* @return Nothing.
public class FindAvg */
{
/** public static void main(String args[])
* This method is used to find average of three
{
integers.
* @param numA This is the first parameter to
FindAvg obj = new FindAvg();
findAvg method int avg = obj.findAvg(10, 20, 30);
* @param numB This is the second parameter to
findAvg method System.out.println("Average of 10,
* @param numC This is the second parameter to 20 and 30 is :" + avg);
findAvg method }
} 20
Executable Comments in JAVA
Search it out…..
21
Data types
Primitive Data Type
Integer Float Character Boolean
byte (1 byte) float (4 bytes) char (2 bytes) bool (1 byte,
but makes use
of 1 bit of it)
long (8 bytes) double (8 byes)
short (2 bytes)
int (4 bytes)
22
Data types in JAVA
Mary owns a Retail Department store. Mary needs to create a bill with the
following fields present:
Invoice ID: Integer
Product ID: Integer
Product Cost: Double
Quantity : Integer
Discount: Double
Total Price: Double
Feedback Provided ? : Boolean
How will you declare them in JAVA??
23
retail_shop.java
public class retail_shop {
Invoice ID: Integer public static void main(String[] args) {
Product ID: Integer
Product Cost: Double int invoice_num, product_id, quant;
Quantity : Integer
Discount: Double double cost, discount, price;
Total Price: Double
Feedback Provided ? : Boolean boolean feedback;
}
}
24
Data Operations
Data Operations
Arithmetic Operators Unary Operator Relational Operators Logical Operators
+ : Addition
< : Less than
- : Subtraction ++ : Increment
<= : Less than equal to && : And
*: operator > : Greater than | | : Or
Multiplication -- : Decrement >= : Greater than
! : Not
/ : Division operator equal to
!= : Not equal to
% : Modulus
== : Equals
25
Data Operations
Let us assume the following values for the variables:
Num1 = 10
Num2 = 15
Operation Solution
Num3 = 25
(Num1 + Num2) – Num3 / 5 20
Num4 = true
Num1++ 11
Num3 >= Num1 + Num2 True
Num2 == Num1 False
(Num2 < Num3) && (Num1 > Num3) False
!Num4 False
26
retail_shop.java
public class retail_shop {
public static void main(String[] args) {
int itema=200, itemb=75, itemc=500;
double price;
price = ((itema*2)+(itemb)+(itemc*3));
price = price-(.1*price);
price = price + (0.05*price);
System.out.println(price);
}
}
27
Data Operations in JAVA
28