Java Training File
Java Training File
Jhanjeri, Mohali-140307
1
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
2
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
3
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
CONTENT
1. INTRODUCTION.
2. A HELLO WORLD PROGRAM.
3. DATA TYPES IN JAVA.
4. WHILE LOOP.
5. FOR LOOP.
6. IF STATEMENT.
7. DO WHILE LOOP.
8. SWITCH STATEMENT.
9. ARRAYS.
10. NESTED FOR LOOPS.
11. CLASSES METHODS AND OBJECTS.
12. METHOD OVERLOADING.
13. CONSTRUCTORS.
14. STATIC AND FINAL.
15. INHERITANCE
16. POLYMORPHISM
17. ENCAPSULATION
4
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
CONTENTS OF MY COURSE
INTRODUCTION
What is java?
Java is a cross-platform object-oriented programming language that was released by
Sun Microsystems in the year 1995. Today, Java is needed to run various
applications such as games, social media applications, audio and video applications,
etc.
How it works ?
How Java Works (in a nutshell) Java works by first compiling the source code into
bytecode. Then, the bytecode can be compiled into machine code with
the Java Virtual Machine (JVM). Java's bytecode can run on any device with the
JVM which is why Java is known as a “write once, run anywhere” language.
5
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
Data types specify the different sizes and values that can be stored in the
variable. There are two types of data types in Java:
PROGRAM
Syntax
The syntax of a while loop is −
while(Boolean_expression) {
// Statements
}
6
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
FOR LOOP
For performs the same function as while loop. When you know exactly how many
times you want to loop through a block of code, use the for loop instead of
a while loop:
Syntax
for (statement 1; statement 2; statement 3) {
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been
executed.
7
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
PROGRAM
IF STATEMENT
The Java if statement tests the condition. It executes the if block if condition is true.
SYNTAX:
1. if(condition){
2. //code to be executed
3. }
PROGRAM
8
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
DO WHILE LOOP:
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:
1. do{
2. //code to be executed
3. }while(condition);
PROGRAM:
SWITCH STATEMENT
The Java switch statement executes one statement from multiple conditions. It is like if-
else-if ladder statement. The switch statement works with byte, short, int, long, enum
types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you
can use strings in the switch statement.
In other words, the switch statement tests the equality of a variable against multiple
values.
9
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
SYNTAX:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
PROGRAM
ARRAY
Normally, an array is a collection of similar type of elements which has contiguous
memory location.
Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure
where we store similar elements. We can store only a fixed set of elements in a Java
array.
Array in Java is index-based, the first element of the array is stored at the 0th index,
2nd element is stored on 1st index and so on.
10
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
SYNTAX:
1. dataType[] arr; (or)
2. dataType []arr; (or)
3. dataType arr[];
PROGRAM
Nested Loops in C
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the
looping of statements inside another loop. Let's observe an example of nesting loops in
C.
Any number of loops can be defined inside another loop, i.e., there is no restriction for
defining any number of loops. The nesting level can be defined at n times. You can
define any type of loop inside another loop; for example, you can define 'while' loop
inside a 'for' loop.
1. for (initialization; condition; update)
11
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
2. {
3. for(initialization; condition; update)
4. {
5. // inner loop statements.
6. }
7. // outer loop statements.
8. }
o Fields
o Methods
o Constructors
o Blocks
o Nested class and interface
Method in Java
In Java, a method is like a function which is used to expose the behavior of an object.
Advantage of Method
o Code Reusability
o Code Optimization
12
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
For Example, Pen is an object. Its name is Reynolds; color is white, known as its state. It
is used to write, so writing is its behavior.
Object Definitions:
CODE
//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance variable
String name;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
13
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
If we have to perform only one operation, having same name of the methods increases
the readability of the program.
Suppose you have to perform addition of the given numbers but there can be any
number of arguments, if you write the method such as a(int,int) for two parameters, and
b(int,int,int) for three parameters then it may be difficult for you as well as other
programmers to understand the behavior of the method because its name differs.
14
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
Constructors in Java
In Java a constructor is a block of codes similar to the method. It is called when an
instance of the class is created. At the time of calling constructor, memory for the object
is allocated in the memory.
Every time an object is created using the new() keyword, at least one constructor is
called.
It calls a default constructor if there is no constructor available in the class. In such case,
Java compiler provides a default constructor by default.
There are two types of constructors in Java: no-arg constructor, and parameterized
constructor.
Note: It is called constructor because it constructs the values at the time of object
creation. It is not necessary to write a constructor for a class. It is because java compiler
creates a default constructor if your class doesn't have any.
EXAMPLE
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
15
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
o The static variable can be used to refer to the common property of all objects
(which is not unique for each object), for example, the company name of
employees, college name of students, etc.
o The static variable gets memory only once in the class area at the time of class
loading.
16
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
4. String college="ITS";
5. }
Suppose there are 500 students in my college, now all instance data members will get
memory each time when the object is created. All students have its unique rollno and
name, so instance data member is good in such case. Here, "college" refers to the
common property of all objects. If we make it static, this field will get the memory only
once.
17
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
1. variable
2. method
3. class
The final keyword can be applied with the variables, a final variable that have no value it
is called blank final variable or uninitialized final variable. It can be initialized in the
constructor only. The blank final variable can be static also which will be initialized in the
static block only. We will have detailed learning of these. Let's first learn the basics of
final keyword.
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
18
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
Inheritance in Java
Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system).
The idea behind inheritance in Java is that you can create new classes that are built
upon existing classes. When you inherit from an existing class, you can reuse methods
and fields of the parent class. Moreover, you can add new methods and fields in your
current class also.
The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality.
19
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
In the terminology of Java, a class which is inherited is called a parent or superclass, and
the new class is called child or subclass.
PROGRAM
Polymorphism in Java
Polymorphism in Java is a concept by which we can perform a single action in
different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The
word "poly" means many and "morphs" means forms. So polymorphism means many
forms.
There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and
method overriding.
If you overload a static method in Java, it is the example of compile time polymorphism.
Here, we will focus on runtime polymorphism in java.
Encapsulation in Java
Encapsulation in Java is a process of wrapping code and data together into a single
unit, for example, a capsule which is mixed of several medicines.We can create a fully
encapsulated class in Java by making all the data members of the class private. Now we
can use setter and getter methods to set and get the data in it.
20
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
It provides you the control over the data. Suppose you want to set the value of id
which should be greater than 100 only, you can write the logic inside the setter method.
You can write the logic not to store the negative numbers in the setter methods.
It is a way to achieve data hiding in Java because other class will not be able to access
the data through the private data members.
The encapsulate class is easy to test. So, it is better for unit testing.
The standard IDE's are providing the facility to generate the getters and setters. So, it
is easy and fast to create an encapsulated class in Java.
File: Student.java
1. //A Java class which is a fully encapsulated class.
2. //It has a private data member and getter and setter methods.
3. package com.javatpoint;
4. public class Student{
5. //private data member
6. private String name;
7. //getter method for name
8. public String getName(){
9. return name;
10. }
11. //setter method for name
12. public void setName(String name){
13. this.name=name
14. }
15. }
21
CGC , Chandigarh Engineering College
Jhanjeri, Mohali-140307
THANK YOU
22