Unit1 Notes
Unit1 Notes
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 .
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
like Notepad (Recommended for this tutorial), TextPad.
Netbeans -source and free which can be
downloaded from 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
class classname
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:
Methods
methods. It is in methods where the logics are written, data is manipulated
and all the actions are executed.
1. class Sample{
2. public static void main(String args[]){
3. System.out.println("How are you ");
4. }
5. }
Java Identifiers
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
Java Modifiers:
Access Modifiers
Non-access Modifiers
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.
Example
"Hello World" "two\nlines" "\"This is in quotes\""
String and char types of literals can contain any Unicode characters. For
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 are the smallest individual building block or smallest unit of a
Java program, it is used by the Java compiler for constructing expressions
and statements. Java program is collection different types of tokens,
comments, and white spaces.
Java Supports Five Types of Tokens:
Variable
local variable
instance variable
static variable
1) 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
Operators in java
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
Java IF Statement
The Java if statement tests the condition. It executes the if block if condition
is true. The following is the syntax
1. if(condition){
2. //code to be executed
3. }
IF-else Statement
The if-else statement in java tests the condition. It executes the if block if
condition is true otherwise else block is executed.
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. }
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:
The Java for loop is used to iterate a part of the program several times. If the
number of iteration is fixed, it is recommended to use for loop.
The simple for loop is same as C/C++. We can initialize variable, check
condition and increment/decrement value.
Syntax:
1. for(initialization;condition;incr/decr){
2. //code to be executed
3. }
Example:
The Java while loop is used to iterate a part of the program several times. If
the number of iteration is not fixed, it is recommended to use while loop.
Syntax:
1. while(condition){
2. //code to be executed
3. }
The Java do-while loop is used to iterate a part of the program several times.
If the number of iteration is not fixed and you must have to execute the loop
at least once, it is recommended to use do-while loop.The Java do-while loop
is executed at least once because condition is checked after loop body.
Syntax: do{ /code to be executed
}while(condition);
int j=1;
do{ System.out.println(j);
j++;
}while(j<=10);
} }
The Java break is used to break loop or switch statement. It breaks the
current flow of the program at specified condition. In case of inner loop, it
breaks only inner loop.
Example:
Example:
Array in java
1. arrayname=new datatype[size];
Example
Let's see the simple example of java array, where we are going to declare,
instantiate, initialize and traverse an array.
1. class Testarray{
2. public static void main(String args[]){
3. int a[]=new int[5];//declaration and instantiation
4. a[0]=10; a[1]=20; a[2]=70; a[3]=40; a[4]=50;
5. for(int i=0;i<a.length;i++)//length is the property of array
6. System.out.println(a[i]);
7. }}
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. }}
In such case, data is stored in row and column based index (also known as
matrix form).
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;
Let's see the simple example to declare, instantiate, initialize and print the
2Dimensional array.
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
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.
Syntax:
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
Constructor is special member function ,it has the same name as class
name. It is called when an instance of object is created and memory is
allocated for the object.
class Sample{
Sample()
{
System.out.println("Sample is created");
}
public static void main(String args[])
{
Sample b=new Sample();
} }
There are many differences between constructors and methods. They are
given below.
The static keyword in java is used for memory management mainly. We can
apply java static keyword with variables, methods, blocks and nested class.
The static keyword belongs to the class than instance of the class.
The static variable can be used to refer the common property of all
objects (that is not unique for each object) e.g. company name of
employees,college name of students etc.
The static variable gets memory only once in class area at the time of
class loading.
If you apply static keyword with any method, it is known as static method.
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. }
There can be a lot of usage of java this keyword. In java, this is a reference
variable that refers to the current object.
class Student{
int rollno;
String name;
float fee;
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
You may invoke the method of the current class by using the this keyword.
If you don't use the this keyword, compiler automatically adds this keyword
while invoking the 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. }}
The this() constructor call can be used to invoke the current class
constructor. It is used to reuse the constructor. In other words, it is used for
constructor chaining.
Calling default constructor from parameterized constructor:
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. }
We can pass the this keyword in the constructor also. It is useful if we have
to use one object in multiple classes. Let's see the example:
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. }
We can return this keyword as an statement from the method. In such case,
return type of the method must be the class type (non-primitive). Let's see
the example:
1. return_type method_name(){
2. return this;
3. }
Example of this keyword that you return as a statement
from the method
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. }