Basic of Java PDF
Basic of Java PDF
Introduction to Java
History of Java
Java applications:
A. Web Application:
Web applications are the applications that run on web browser using servlet, JSP, struts
technologies. These technologies create java web applications and deploy them on server.
B. Mobile Application:
These are mobile applications created using java.
C. Standalone Application:
Standalone applications are executed by themselves without the need of other programs and
files. Example of such an application is antivirus.
D. Enterprise Application:
Some applications are designed for corporate organizations with the intent to control major
process in real time. Such applications are called enterprise applications.
1
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
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.
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.
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
3
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
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.
4
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
class Example
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
1. The first line contains the keyword class and class name, which actually the basic
unit for encapsulation, in which data and methods are declared.
2. Second line contains "{" which indicates the beginning of the class.
main function.
5
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
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.
Java Comments
Comments in any programming language are ignored by the compiler or the
interpreter. A comment is a part of the coding file that the programmer does
not want to execute, rather the programmer uses it to either explain a block
of code or to avoid the execution of a specific part of code while testing.
1) Single-line comment
2) Multi-line comment
To write a single-line comment just add a ‘//’ at the start of the line.
Example:
class simple
{
public static void main(String[] args) {
//This is a single line comment
System.out.println("Hello World!!!");
}
}
6
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
To write a multi-line comment just add a ‘/*…….*/’ at the start of the line.
Example:
class simple
/*This
is
Multiline
Comment
*/
System.out.println("Hello World!!!");
}
}
7
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
Variables
Literals
Java Variables
Variables are containers that store information that can be manipulated and referenced later by
the programmer within the code. Java variables have a data type associated with them which can
tell us about the size and layout of the variable’s memory.
8
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
Local variable
Instance variable
Class/Static variable
Local Variables:
A variable that is declared inside the body of the method or constructor is called a local
variable. It is called so because the extent of a local variable lies only within the method
or constructor within which it is created and other methods in the class cannot access it.
Inside the method body, local variable is declared using the static keyword.
Example:
class variableType
v.localVariable();
9
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
Instance Variables:
An instance variable is declared inside the class but outside a method or a constructor. It is
similar to a static variable except that it is declared without using the keyword static. These
variables are accessible by all methods or constructors that are inside the class.
Example:
class variableType
{
String name = "Ram";
int marks = 95;
void instanceVariable()
{
System.out.println(name + " Scored " + marks + "%.");
}
public static void main(String[] args)
{
variableType v = new variableType();
v.instanceVariable();
}
}
Static Variables:
An static variable is declared inside the class but outside a method or a constructor. It is similar
to a instance variable except that it is declared using the keyword static. These variables are
accessible by all methods or constructors that are inside the class.
Example:
class variableType
{
10
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
Instance variables
12
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
Data
Size Value Range
Type
-2,147,483,648 to
Int 2 byte
2,147,483,647
3.40282347 x 1038 to
Float 4 byte
1.40239846 x 10-45
-9,223,372,036,854,775,808
Long 8 byte
to 9,223,372,036,854,775,807
1.7976931348623157 x 10308,
double 8 byte
4.9406564584124654 x 10-324
Depends
boolean True or False
on JVM
13
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
Method Description
nextLine() Accepts string value
next() Accept string till whitespace
nextInt() Accepts int value
nextFloat() Accepts float value
nextDouble() Accepts double value
nextLong() Accepts long value
nextShort() Accepts short value
nextBoolean() Accepts Boolean value
nextByte() Accepts Byte value
Example
import java.util.Scanner;
class Example
{
Public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println(“Enter Name, RollNo”);
String name = s.nextLine(); //used to read line
Int RollNo = s.nextInt(); //used to read int
System.out.println(“Name:” +name);
System.out.println(“RollNo” +RollNo);
S.close();
}
}
15
Prof. Madhuri V. Desai
Object-Oriented Programming in Java
Program 1.
Write a program to calculate the percentage of a given student in board exam. His marks from 5
subjects must be taken as input from the keyboard. (Marks are out of 100)
import java.util.Scanner;
17
Prof. Madhuri V. Desai