Lecture Notesjava 250430 093314
Lecture Notesjava 250430 093314
UNIT –I
I. INTRODUCTION TO JAVA
What is Java
Application of java
1. Desktop Applications
2. Web Applications
3. Mobile
4. Enterprise Applications
5. Smart Card
6. Embedded System
7. Games
8. Robotics etc
History of Java
James Gosling, Patrick Naughton and Mike Sheridan initiated the Java
language project in 1991. Team of sun engineers designed for small,
embedded systems in electronic appliances like set-top boxes. Initially it was
called "Greentalk" later it was called Oak .
Features of Java:
Object means a real word entity such as pen, chair, table etc. Object-
Oriented Programming is a methodology or paradigm to design a program
using classes and objects. It simplifies the software development and
maintenance by providing some concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
If any language fallows the OOPS concepts that language we call it as object
oriented language
To create a simple java program, you need to create a class that contains
main method. Let's understand the requirement first.
Notepad − On Windows machine, you can use any simple text editor
like Notepad (Recommended for this tutorial), TextPad.
Netbeans − A Java IDE that is open-source and free which can be
downloaded from Eclipse − A Java IDE developed by the eclipse open-
source community and can be downloaded from
What is JVM
It is:
What it does
Loads code
Verifies code
Executes code
Provides runtime environment
Memory area
Class file format
Register set
Garbage-collected heap
Fatal error reporting etc.
Java complier translates the java source code into byte code or intermediate
code ,not the executable file .JVM take the byte code and convert into
executable code corresponding to Operating system
type methodname1(parameter-list)
{ // body of method }
type methodname2(parameter-list)
{ // body of method }
{ // body of method } }
The data, or variables, defined within a class are called instance variables.
The code is contained within methods. Collectively, the methods and
variables defined within a class are called members of the class. In most
classes, the instance variables are acted upon and accessed by the methods
defined for that class
Simple Class
Class Sample
void get()
{ // body
} }
Object in Java
Object is the physical as well as logical entity whereas class is the logical
entity only.
Object Definitions:
1. class Sample{
2. public static void main(String args[]){
3. System.out.println("How are you ");
4. }
5. }
All Java components require names. Names used for classes, variables, and
methods are called identifiers.
In Java, there are several points to remember about identifiers. They are as
follows −
There are eight primitive data types supported by Java. Primitive data types
are predefined by the language and named by a keyword. Let us now look
into the eight primitive data types in detail.
byte
short
long
float
double
boolean
char
Java Literals
String literals in Java are specified like they are in most other languages by
enclosing a sequence of characters between a pair of double quotes.
Examples of string literals are −
Example
"Hello World" "two\nlines" "\"This is in quotes\""
String and char types of literals can contain any Unicode characters. For
example − char a = '\u0001'; String a = "\u0001";
Java language supports few special escape sequences for String and char
\n Newline (0x0a)
\f Formfeed (0x0c)
\b Backspace (0x08)
\s Space (0x20)
\t Tab
\\ Backslash
1. class Sample{
2. public static void main(String[] args){
3. int i=50;
4. int j=60;
5. int k=a+b;
6. System.out.println(k);
7. } }
1. class Sample{
2. public static void main(String[] args){
3. int j=10;
4. float k=a;
5. System.out.println(i);
6. System.out.println(j);
7. }}
Unicode System
Unicode is a universal international standard character encoding that is
capable of representing most of the world's written languages.
Java Tokens
Variable
local variable
instance variable
static variable
1) Local Variable
2) Instance Variable
3) Static variable
There are many types of operators in java which are given below:
Unary Operator,
Arithmetic Operator,
shift Operator,
Relational Operator,
Bitwise Operator,
Logical Operator,
Ternary Operator and
Assignment Operator.
Java If-else Statement
The Java if statement is used to test the condition. It
checks boolean condition: true or false. There are various
types of if statement in java.
Java IF Statement
1. if(condition){
2. //code to be executed
3. }
IF-else Statement
Syntax:
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
Syntax:
1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
Switch Statement
The switch statement in java executes one
statement from multiple conditions. It is
like if-else-if ladder statement.
Syntax:
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matched;
12. }
Example:
Syntax:
1. for(initialization;condition;incr/decr){
2. //code to be executed
3. }
Example:
Syntax:
1. while(condition){
2. //code to be executed
3. }
}while(condition);
main(String[]
args) { int
j=1;
do{
Sys
tem.out
.println
(j); j++;
}while(j<=10);
} }
Example:
Example:
Array in java
Single Dimensional
Array in java Syntax
to Declare an Array
in java
1. arrayname=new datatype[size];
Example
We can declare, instantiate and initialize the java array together by:
1. class Testarray1{
2. public static void
main(String args[]){ 3.
4. int a[]={33,3,4,5};//declaration, instantiation
and initialization 5.
6. //printing array
7. for(int i=0;i<a.length;i++)//length is the property of array
8. System.out.println(a[i]);
9.
10. }}
1. arr[0][0]=1;
2. arr[0][1]=2;
3. arr[0][2]=3;
4. arr[1][0]=4;
5. arr[1][1]=5;
6. arr[1][2]=6;
7. arr[2][0]=7;
8. arr[2][1]=8;
9. arr[2][2]=9;
1. class Ex{
2. public static void
main(String args[]){ 3.
4. //declaring and initializing 2D array
5. int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
6.
7. //printing 2D array
8. for(int i=0;i<3;i++){
9. for(int j=0;j<3;j++){
10. System.out.print(arr[i][j]+" ");
11. }
12. System.out.println();
13. }
14. } }
Java Comments
Syntax:
Example:
Syntax:
1. /*
2. This
3. is
4. multi line
5. comment
6. */
Example:
Syntax:
1. /**
2. This
3. is
4. documentation
5. comment
6. */
VI CONSTRUCTORS
c
l
a
s
s
S
a
m
p
l
e
{
S
a
m
p
l
e
(
)
{
System.out.println("Sample is created");
}
public static void main(String args[])
{
Sample b=new Sample();
} }
S
t
u
d
{
i
n
t
r
o
l
l
n
o
;
S
t
r
i
n
g
n
a
m
e
;
static
String
college
="ITS";
Stud(int
r,String
n){
r
o
l
l
n
o
=
r
;
n
a
m
e
n
;
}
void display (){System.out.println(rollno+" "+name+" "+college);}
2.
3. class Stud{
4. int rollno;
5. String name;
6. static
String college =
"BEC"; 7.
8. static void change(){
9. college = "JBIEIT";
10. }
11. Stud(int r, String n){
12. rollno = r;
13. name = n;
14. }
15.
16. void display (){System.out.println(rollno+" "+name+"
"+college
);}
17. public static void main(String args[]){
18. Stud.change();
19. Stud s1 = new Stud (11,"Kiran");
20. Stud s2 = new Stud (22,"Arjun");
21. Stud s3 = new Stud (33,"srinu");
22. s1.display();
23. s2.display();
24. s3.display();
25. }
26. }
S
t
u
d
e
n
t
{
i
n
t
r
o
l
l
n
o
;
S
t
r
i
n
g
n
a
m
e
;
f
l
o
a
t
f
e
e
;
Student(int rollno,String
name,float fee){
this.rollno=rollno;
this.name=
name;
this.fee=fee
;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void
main(String args[]){
Student s1=new
Student(111,"ankit",5000f);
Student s2=new
Student(112,"sumit",6000f)
; s1.display();
s2.display(); }}
2) this: to invoke current class method
1. class B{
2. void m(){System.out.println("hello m");}
3. void n(){
4. System.out.println("hello n");
5. //m();//same as this.m()
6. this.m();
7. }
8. }
9. class TestThis4{
10. public static void main(String args[]){
11. B b=new B();
12. b.n();
13. }}
1. class B{
2. B(){System.out.println("hello ");}
3. B(int x){
4. this();
5. System.out.println(x);
6. }
7. }
8. class Sample{
9. public static void main(String args[]){
10. B a=new B(10);
11. }}
1. class Student{
2. int rollno;
3. String name,course;
4. float fee;
5. Student(int rollno,String name,String course){
6. this.rollno=rollno;
7. this.name=name;
8. this.course=course;
9. }
10. Student(int rollno,String name,String course,float fee){
11. this(rollno,name,course);//reusing constructor
12. this.fee=fee;
13. }
14. void display(){System.out.println(rollno+" "+name+"
"+course+" "
+fee);}
15. }
16. class SamplTest{
17. public static void main(String args[]){
18. Student s1=new Student(111,"ankit","java");
19. Student s2=new Student(112,"sumit","java",6000f);
20. s1.display();
21. s2.display();
22. }}
1. class S2{
2. void m(S2 obj){
3. System.out.println("method is invoked");
4. }
5. void p(){
6. m(this);
7. }
8. public static void main(String args[]){
9. S2 s1 = new S2();
10. s1.p();
11. }
12. }
1. class B{
2. A4 obj;
3. B(A4 obj){
4. this.obj=obj;
5. }
6. void display(){
7. System.out.println(obj.data);//using data member of A4 class
8. }
9. }
10.
11. class A4{
12. int data=10;
13. A4(){
14. B b=new B(this);
15. b.display();
16. }
17. public static void main(String args[]){
18. A4 a=new A4();
19. }
20. }
1. return_type method_name(){
2. return this;
3. }
1. class A{
2. A getA(){
3. return this;
4. }
5. void msg(){System.out.println("Hello java");}
6. }
7. class Test1{
8. public static void main(String args[]){
9. new A().getA().msg();
10. }
11. }