Sunteți pe pagina 1din 67

Unit 1: Introduction

• Feature of Java, JVM, JRE, class path, classes, fields, access control,
objects creation, initialization, methods, this, overloading methods,
main method, native methods, class design, lexical elements, types
and literals, variables, array variables, naming, operators, expressions,
member access, precedence, associativity, statements & blocks, if-
else, switch, while and do-while, for, labels, break, continue, return,
goto.
Features of Java
• Simple
• Secure
• Portable
• Object-oriented
• Robust
• Multithreaded
• Architecture-neutral
• Interpreted
• High performance
• Distributed
• Dynamic
Difference between JDK, JRE, and JVM
• 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.
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.
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.

• The JVM performs the following main tasks:


• Loads code
• Verifies code
• Executes code
• Provides runtime environment
Naming Conventions
Class
• It should start with the uppercase letter.
• It should be a noun such as Color, Button, System, Thread, etc.
• Use appropriate words, instead of acronyms.
• Example: -
• public class Employee
•{
• //code snippet
•}
Interface
• It should start with the uppercase letter.
• It should be an adjective such as Runnable, Remote, ActionListener.
• Use appropriate words, instead of acronyms.
• Example: -
• interface Printable
•{
• //code snippet
•}
Method
• It should start with lowercase letter.
• It should be a verb such as main(), print(), println().
• If the name contains multiple words, start it with a lowercase letter followed by an uppercase
letter such as actionPerformed().
• Example:-
• class Employee
• {
• //method
• void draw()
• {
• //code snippet
• }
• }
Variable
• It should start with a lowercase letter such as id, name.
• It should not start with the special characters like & (ampersand), $ (dollar), _ (underscore).
• If the name contains multiple words, start it with the lowercase letter followed by an uppercase
letter such as firstName, lastName.
• Avoid using one-character variables such as x, y, z.
• Example :-
• class Employee
• {
• //variable
• int id;
• //code snippet
• }
Package
• It should be a lowercase letter such as java, lang.
• If the name contains multiple words, it should be separated by dots
(.) such as java.util, java.lang.
• Example :-
• package java.io; //package
• class Employee
•{
• //code snippet
•}
Constant
• It should be in uppercase letters such as RED, YELLOW.
• If the name contains multiple words, it should be separated by an underscore(_)
such as MAX_PRIORITY.
• It may contain digits but not as the first letter.
• Example :-
• class Employee
• {
• //constant
• static final int MIN_AGE = 18;
• //code snippet
• }
An overview of the software development process.
Through the Java VM, the same application is
capable of running on multiple platforms.
Java Variables
• A variable is a container which holds the value while the java program
is executed. A variable is assigned with a datatype.

• Variable is a name of memory location.

• There are three types of variables in java: local, instance and static.
Types of Variables
• There are three types of variables in java:
• local variable
• instance variable
• static variable
Example to understand the types of variables in java
• class A{
• int data=50;//instance variable
• static int m=100;//static variable
• void method(){
• int n=90;//local variable
•}
•}
Data Types in Java
• Data types specify the different sizes and values that can be stored in
the variable. There are two types of data types in Java:

• Primitive data types: The primitive data types include boolean, char,
byte, short, int, long, float and double.

• Non-primitive data types: The non-primitive data types include


Classes, Interfaces, and Arrays.
Java If-else Statement
• if statement
• if-else statement
• if-else-if ladder
• nested if statement
Java if Statement
• The Java if statement tests the condition. It executes the if block if
condition is true.

Syntax:
if(condition){
//code to be executed
}
//Java Program to demonstate the use of if statement.

• public class IfExample {


• public static void main(String[] args) {
• //defining an 'age' variable
• int age=20;
• //checking the age
• if(age>18){
• System.out.print("Age is greater than 18");
• }
• }
• }
//A Java Program to demonstrate the use of if-else statement.
• //It is a program of odd and even number.
• public class IfElseExample {
• public static void main(String[] args) {
• //defining a variable
• int number=13;
• //Check if the number is divisible by 2 or not
• if(number%2==0){
• System.out.println("even number");
• }else{
• System.out.println("odd number");
• }
• }
• }
//Java Program to demonstrate the use of If else-if ladder.
• public class PositiveNegativeExample {
• public static void main(String[] args) {
• int number=-13;
• if(number>0){
• System.out.println("POSITIVE");
• }else if(number<0){
• System.out.println("NEGATIVE");
• }else{
• System.out.println("ZERO");
• }
• }
• }
/Java Program to demonstrate the use of Nested If Statement.
• public class JavaNestedIfExample2 {
• public static void main(String[] args) {
• //Creating two variables for age and weight
• int age=25;
• int weight=48;
• //applying condition on age and weight
• if(age>=18){
• if(weight>50){
• System.out.println("You are eligible to donate blood");
• } else{
• System.out.println("You are not eligible to donate blood");
• }
• } else{
• System.out.println("Age must be greater than 18");
• }
• } }
Switch Case
• public class SwitchExample {
• public static void main(String[] args) {
• //Declaring a variable for switch expression
• int number=20;
• //Switch expression
• switch(number){
• //Case statements
• case 10: System.out.println("10");
• break;
• case 20: System.out.println("20");
• break;
• case 30: System.out.println("30");
• break;
• //Default case statement
• default:System.out.println("Not in 10, 20 or 30");
• }
• }
• }
Java While Loop
• 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.
• public class WhileExample {
• public static void main(String[] args) {
• int i=1;
• while(i<=10){
• System.out.println(i);
• i++;
• }
•}
•}
Java 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.
• public class DoWhileExample {
• public static void main(String[] args) {
• int i=1;
• do{
• System.out.println(i);
• i++;
• }while(i<=10);
•}
•}
Java Simple For Loop
//Java Program to demonstrate the example of for loop
• //which prints table of 1
• public class ForExample {
• public static void main(String[] args) {
• //Code of Java for loop
• for(int i=1;i<=10;i++){
• System.out.println(i);
• }
•}
•}
Java Break Statement
Java Continue Statement Example
• /Java Program to demonstrate the use of continue statement
• //inside the for loop.
• public class ContinueExample {
• public static void main(String[] args) {
• //for loop
• for(int i=1;i<=10;i++){
• if(i==5){
• //using continue statement
• continue;//it will skip the rest statement
• }
• System.out.println(i);
• }
• }
• }
Access Modifiers in java
• The access modifiers in java specifies accessibility (scope) of a data
member, method, constructor or class.

• There are 4 types of java access modifiers:

• private
• default
• protected
• public
private access modifier
• The private access modifier is accessible only within class.
• Role of Private Constructor
• If you make any class constructor private, you cannot create the instance
of that class from outside the class. For example:

• Simple example of private access modifier

• In this example, we have created two classes A and Simple. A class contains
private data member and private method. We are accessing these private
members from outside the class, so there is compile time error.
default access modifier
• If you don't use any modifier, it is treated as default by default. The
default modifier is accessible only within package.

• Example of default access modifier


• In this example, we have created two packages pack and mypack. We
are accessing the A class from outside its package, since A class is not
public, so it cannot be accessed from outside the package.
protected access modifier
• The protected access modifier is accessible within package and outside the
package but through inheritance only.
• The protected access modifier can be applied on the data member, method
and constructor. It can't be applied on the class.

• Example of protected access modifier


• In this example, we have created the two packages pack and mypack. The A
class of pack package is public, so can be accessed from outside the
package. But msg method of this package is declared as protected, so it can
be accessed from outside the class only through inheritance.
public access modifier
• The public access modifier is accessible everywhere. It has the widest
scope among all other modifiers.
Understanding all java access modifiers
Method Overloading in Java
• If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.

• Different ways to overload the method

• There are two ways to overload the method in java


• By changing number of arguments
• By changing the data type
1) Method Overloading: changing no. of arguments
2) Method Overloading: changing data type of arguments
Q) Why Method Overloading is not possible by changing the
return type of method only?
• In java, method overloading is not possible by changing the return type of the method only
because of ambiguity.
Method Overriding in Java
• If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in Java.

• In other words, If a subclass provides the specific implementation of


the method that has been declared by one of its parent class, it is
known as method overriding.
• Usage of Java Method Overriding

• Method overriding is used to provide the specific implementation of a


method which is already provided by its superclass.
• Method overriding is used for runtime polymorphism

• Rules for Java Method Overriding

• The method must have the same name as in the parent class
• The method must have the same parameter as in the parent class.
• There must be an IS-A relationship (inheritance).
Understanding the problem without method overriding
Constructor in Java
• Constructor is a special type of method that is used to initialize the object.
• Constructor is invoked at the time of object creation. It constructs the
values i.e. provides data for the object that is why it is known as constructor.

• Rules for creating constructor


• There are basically two rules defined for the constructor.
• Constructor name must be same as its class name
• Constructor must have no explicit return type
Types of constructors
• There are two types of constructors:

• default constructor (no-arg constructor)


• parameterized constructor
Default Constructor
• A constructor that have no parameter is known as default constructor.

• class Bike{

• Bike(){System.out.println("Bike is created");}

• public static void main(String args[]){
• Bike b=new Bike();
•}
•}
• Rule: If there is no constructor in a class, compiler automatically
creates a default constructor.
• What is the purpose of default constructor?

• Default constructor provides the default values to the object like 0,


null etc. depending on the type.
DEFAULT CONSTRUCTOR THAT DISPLAYS THE DEFAULT VALUES
• class Student{
• int id;
• String name;

• void display(){System.out.println(id+" "+name);}

• public static void main(String args[]){
• Student s1=new Student();
• Student s2=new Student();
• s1.display();
• s2.display();
• }
• }

• 0 null
• 0 null
• PARAMETERIZED CONSTRUCTOR

• A constructor that have parameters is known as parameterized


constructor

• Why use parameterized constructor?

• Parameterized constructor is used to provide different values to the


distinct objects.
• class Student{
• int id;
• String name;

• Student(int i,String n){
• id = i;
• name = n;
• }
• void display(){System.out.println(id+" "+name);}

• public static void main(String args[]){
• Student s1 = new Student(111,"Karan");
• Student s2 = new Student(222,"Aryan");
• s1.display();
• s2.display();
• }
• }
What is the difference between constructor and method ?
Constructor Method

Constructor is used to initialize the state Method is used to expose behaviour of an


of an object. object.

Constructor must not have return type. Method must have return type.

Constructor is invoked implicitly. Method is invoked explicitly.

The java compiler provides a default Method is not provided by compiler in any
constructor if you don't have any case.
constructor.

Constructor name must be same as the Method name may or may not be same as
class name. class name.

S-ar putea să vă placă și