0% found this document useful (0 votes)
8 views46 pages

Java Unit1

Java, developed by Sun Microsystems in 1991 and later acquired by Oracle, is a simple, object-oriented programming language that emphasizes modularity and code reusability. It is platform-independent, supports various applications including mobile and web development, and has a robust community. Key features include being compiled and interpreted, dynamic, and multithreaded, with a rich set of built-in libraries and tools for developers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views46 pages

Java Unit1

Java, developed by Sun Microsystems in 1991 and later acquired by Oracle, is a simple, object-oriented programming language that emphasizes modularity and code reusability. It is platform-independent, supports various applications including mobile and web development, and has a robust community. Key features include being compiled and interpreted, dynamic, and multithreaded, with a rich set of built-in libraries and tools for developers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Introduction o Java

JAVA was developed by Sun Microsystems Inc in 1991, later

acquired by Oracle Corporation.

It was developed by James Gosling and Patrick Naughton.

Initially named as OAK later renamed as JAVA around 1995.

It is a simple programming language.

Writing, compiling and debugging a program is easy in java.

It helps to create modular programs and reusable code.


Differences b/w C & Java
C Programming Language Java Programming Language
1. It is a procedural (Structured )
1. It is an Object Oriented
oriented programming Language Programming language
2. Emphasis on structure (How to 2. Emphasis is on data
implement ) 3. Program Breaks down into Objects
3. Program Breaks down into 4. Follows Bottom-Up Approach
functions 5. Supports Compiler and Interpreter
4. Follows Top-Down Approach 6. Platform Independent (Uses JVM
for translation of code to Byte
5. Compiler Supported code)
6. Platform Dependent 7. File Generates in the form of
7. File Generates in the form of .exe Generates .class files
files 8. Uses Import option imports the
packages needs to compile the
8. Supports Pre-processor Directives program
by #tag headers
Uses of Java
✔Java works on different platforms (Windows, Mac, Linux,
Raspberry Pi, etc.)
✔It is one of the most popular programming language in the world
✔It is easy to learn and simple to use
✔It is open-source and free
✔It is secure, fast and powerful
✔It has a huge community support (tens of millions of developers)
✔Java is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development
costs.
Applications of Java
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
Object Oriented Programming
• Note:
• All Computer programs consists of two
elements: Code and Data
• All Programs are conceptually organized
around its code or data.
• What is happening Or Who is being effected?
Two Paradigms to construct a Program
• Process-Oriented Model
– This Characterizes a program as a series of linear
steps (Code)
– Can be thought of code acting on data
• Object-Oriented Programming Model
– Program can be characterized as Data controlling
access to code
– Organizes the program around its data (Objects)
– Sets a well defined interfaces to that data.
Principles of OOP
• Data Encapsulation
Mechanism that binds data and code together
Uses: keeps the both from outside interfaces
and misuse.

Method Method

DATA

Method Method
• Inheritance: It is the Process by which one
Object acquires the features of another
object.
• The knowledge is made manageable by
hierarchical manner. (Top-Down)
Class A

Class B

Class C
• Polymorphism: This feature allows one
interface to perform more than one action.
• This action is performed by the exact nature of
the situation (Dynamically).
• Overriding
Features of Java
A list of most important features of Java language is given
below.

1. Compiled and Interpreted


2. Platform independent and Portable
3. Object Oriented
4. Robust and Secured
5. Distributed
6. Familiar, Simple and small
7. Multithreaded and Interactive
8. High Performance
9. Dynamic an Extensible
Types of Java Program
1. Java stand-alone applications: The Java
stand-alone applications are the programs written
in Java to carry out certain tasks. These applications
run directly by the Java interpreter.
2. Java Applets: Java applets are the Java applications
that are embedded inside HTML files and can be
downloaded into a Java-capable browser (E.g.
Netscape and Hot Java).
Tools of Java Program
1. JDK- Java Development Kit:

It is a software development environment to develop


java applications and applets
It includes: Javac
javap
javah
Java
Javadoc
JRE Runtime Environment
JAR java archiver ect..
2. JSL /API- Java Standard Library/ Application Programming
Interface.
• Hundreds of methods and classes are into placed into various
groups called as packages – which are also known as built-in
packages
1) java.lang: Contains language support classes(e.g classed which defines primitive data
types, math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.

JVM- Java Virtual Machine
• JVM is an engine that provides a runtime
environment to drive the Java Code or
applications.

• JVM is a part of Java Run Environment (JRE)

• It converts Java byte-code into machine


language.
Execution Process of Java Program
Step 1:

Java Virtual
Java Program Java Compiler
Machine

Source Code Byte Code

Step 2:

Java Virtual
Java Interpreter Machine Code
Machine
Byte Code
Specific Machine Code
(windows, Linux, Mac etc..)
Java Virtual Machine
Structure of Java Program
Documentation Section Suggested

Package Section Optional

Import Statements
Optional
Interface Statements Optional

Class Definitions Optional

Main Method Class


{
Main Method Definition Essential
}
Lexical Issues –
Interpretation of atomic elements in java
Java Programs are collection of
• Whitespace
• Identifiers
• Keywords
• Comments
• Literals
• Operators
• Separators
Whitespaces:
Whitespace is a space, tab or newline.
Java is a freeform language . No need to follow any indentation
rules.
Ex: \n, \t

Identifiers:
Each identifier must have at least one character.
• Identifiers are the name given to variables.
• Can be sequence of uppercase and lowercase
• Letters, numbers or underscore(_) and dollar-sign characters
classes, methods.
• Must not begin with numbers
Ex: count,

Keywords: Java has a set of keywords that are reserved words that
cannot be used as variables, methods, classes, or any other
identifiers:
Ex: int, float, static ,if, else….
• Comments :
Can be used to explain Java codeto make it more readable.
It can also be used to prevent execution when testing
alternative code.
Single-line comments start with two forward slashes ( // ).
Multiple-Line Comments start with /* and end with */.

• Literals:

Constant values in java are represented by using


literal representation.
These are represented as a sequence of characters
(digits, letters & other characters) that represent a
constant value
Literals in java are of 5 types
✔ Integer Literal Ex: 100
✔ Floating point Literal Ex: 29.08
✔ Character Literal Ex: ‘a’, ‘N’
✔ Boolean Literal Ex: true, false
✔ String Literal Ex: “Chennai”

Operators: Operators are the symbols that takes one or


more arguments that operate on them to produce a
result.
Various Operators in java are
• Separators:
These are the symbols used to divide and
arrange the group of coding.

(), { }, [ ], ;,., Comma


Data Types in Java

Primitive Data Type


Built-In Non-Primitive Data Type
Derived –Data Types

Numeric Non-Numeric
Classes

Integer Arrays
Character

Boolean Interface
Floating
Point
String
Data type Memory Size Range
Integer
-128 to 127
byte 1 Bytes Ex: byte n, a;

-32,768 to 32767
short 2 Bytes Ex: short n= 3435

-2147483648 to 2147483647
int 4 Bytes Ex: int num = 5464564;

-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
long 8 Bytes
Ex: long num = 15000000000L;
Floating Point
3.4e−038 to 3.4e+038
4 Bytes
Float float num =67;

1.7e−308 to 1.7e+308
8 Bytes
double double num = 79.678d;

2 Bytes char a = 65, b = 66, c = 67;


Character

boolean a=true;
Boolean 1 Bit boolean b = false;
Variables
Variables are the names of storage locations.
- tells the compiler what the variable name is
- Specifies What type of data the variable holds
- Place of declaration specifies the scope of the variable
- must be declared before use.
To declare a variable
Syntax: data_type var1, var2,…..varn;

Ex: int count; double p1; char letter


float x,y; byte num; short n;
Giving values to variables
1. By assigning values:
Syntax: Variable_Name = value;
Ex: int i = 10;
char letter=‘c’;
2. By using read statement:
Using Java BufferedReader Class
Scanner Class
Console Class
Using Java BufferedReader Class
public class Program1
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String name = br.readLine();
System.out.println(name);
}
}
Scanner Class in Java
import java.util.Scanner;
class Program2
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered string "+s);
int a = in.nextInt();
System.out.println("You entered integer "+a);
float b = in.nextFloat();
System.out.println("You entered float "+b);
}
}
Console Class in Java
public class Program3
{
public static void main(String[] args)
{
String name = System.console().readLine();
System.out.println(name);
}
Scope of Variables
Java variables are classified into 3 kinds
• Instance variables

• Class variables/Static variables

• Local variables
Instance variables
✔Instance variables are
crated when objects
are created

✔These variables are


associated with the
objects

✔They take different


values for each object

✔Each variable has


separate memory
location
Class variables Or Static Variables
Class Variables are global variables to a class and they belong to
the entire set of objects that the class creates.
Note:
• Static variables are declared using the static keyword
within a class outside any method constructor or
block.
• Only One memory will be created for each class
variable.
• Static variables are created at the start of program
execution and destroyed automatically when
execution ends.
• To access static variables, we need not create an object
of that class, we can simply access the variable as
class_name.variable_name;
class program4
// class/static variable
{
public static void main(String args[])
{
Static int i=10; // class/static variable
static int count;
Int j=12; // instance variable
--------
----------
}
}
Local variables
Local variables are used inside the methods. These variables
are not available for use outside the methods.

int area()
{
int length=10; //local variable
int breadth = 5; //local variable
int rectarea = length*breadth; //local variable
return rectarea;
}
Literals in JAVA
• A constant value in java is created by using Literal
representation of it.
• Types of literals:
1. Integer
• Decimal - Base is 10 1,2,3…45..
• Octal (0 to 7) & Base is 8 denoted by 0 –begins with 0
• Hexa Decimal (0 to 15)& Base is 16 denoted by 0x- begins with
0x
2. Floating Point Literals (whole number – with fractional part)
• Float - Number appends with F or f folat f=24.5f
• Double - Number appends with D or d double d=0.00006d
3. Character Literals:
Single character literals- a single char enclosed in
single quote marks
‘x’, ‘5’, ‘;’

4. String Literal – A sequence of characters enclosed in


double quotes marks
“1997’, “CHENNAI”, “5+3”, “X”

5. Boolean Literals:
Values to represent true or false
In Java these values could not be converted to numeric 1 or
0 since they are declared as boolean data type.
Arrays
• A group of like-typed variables that refers a
common name is called array.
• Types:
One Dimensional & Multi-Dimensional
One Dimensional Arrays:
Declaration syntax:
step 1:
type var_name[ ];
Ex: int roll_no[ ];
the above ex. represents array with no value
To physically link with memory use new operator
and assign it to the array variable.
Step 2:
var_name = new type[size];
ex: roll_no=new int[12];
In the above example the array roll_no consists
of 12 elements which are initialized to ZERO.
i.e: roll_no[0]=0, roll-no[1]=0……roll_no[11]=0
Both setps can be combined together as follows
type var_name[]=new type[size];
ex: type roll_no[]=new int[12];
Example Program
class ArrayExample {
public static void main(String[] args)
{
// create an array of length 5
int[ ] age = new int[5]; // access each element of the array using the index number
System.out.println(age[0]);
System.out.println(age[1]);
System.out.println(age[2]);
System.out.println(age[3]);
System.out.println(age[4]);
}}
Example Program
class ArrayExample
{
public static void main(String[] args)
{ // create an array of length 5
int[] age = new int[5]; // accessing elements using for loop
for (int i = 0; i < 5; ++i)
{
i=i+1;
System.out.println(age[i]);
}}}
Java Multidimensional Arrays
• A multidimensional array is an array of arrays. Each element
of a multidimensional array is an array itself. For example,
• int[][] a = new int[3][4];

You might also like