SlideShare uma empresa Scribd logo
1 de 31
Java Programming
Manish Kumar
(manish87it@gmail.com)
Lecture- 3
Contents
 Variables
 Data Type
 Operators
 Keywords
 OOP’s Concept
Variables
 A variable is a container that holds the value when the java program executed or we can say that a variable is
a name of memory location.
 The value of a variable can be changed.
Syntax –
<data type> <variable name> = <value of variable>
 In java, there are three types of variable:
 Local Variable
 Instance Variable
 Static Variable
Local Variable – A variable declared inside methods, constructors, or blocks is called local variable. Few
important points of local variable are as follows:
 We can not use access modifier for local variable.
Variables
 We can not use access modifier for local variable.
 The scope of local variable is within the method, constructor or block in which it is declared.
 There is no default value for local variables, so local variable should be initialized at the declaration
time.
 A local variable can not be defined with static keyword.
public class Demo {
public void sum () {
int x = 10, y=20, z=0;
z = x + y;
System.out.println(“Addition of x and y =” +z);
}
public static void main (String args []) {
Demo d = new Demo ();
d.sum ();
}
}
Output - Addition of x and y = 30
public class Demo {
public void sum () {
int x+5;
z = x + y;
System.out.println(“Addition of x and y =” +z);
}
public static void main (String args []) {
Demo d = new Demo ();
d.sum ();
} }
Output - variable number might not have been initialized x = x + 5;
^
1 error
Variables
Instance Variable – A variable declared outside the body of methods, constructors, or blocks but inside the
class is called instance variable. Access Modifiers can be used with instance variable.
class Test {
int x=10;//instance variable
void show () {
int y=20;//local variable
} }
Static Variable - A variable declared with static keyword is called static variable. Static variable can be shared
among all the instances of the class. Static variable cannot be local.
Ex. static int x = 10;
Variables
Naming Convention
 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.
Ex.
class Employee {
int id;
//code snippet
}
Data Type
 Data type means that define the data or variables.
 Based on the data type of a variable, the operating system allocates memory for a variable and decides what
can be stored in the memory.
 There are two types of data type in java as follows:
 Reference Data Types
 Primitive Data Types
Reference Data Type - The reference data types include Classes, Interfaces and Arrays. The default value of
any reference data type in null. Reference variables are created by using constructors of the classes. Reference
variables are used to access objects. Ex.
Demo d = new Demo ();
Here Demo is a class and d is a reference variable.
Data Type
Data Type Default Value Default size Description
boolean false 1 bit for simple flags that track true/false conditions. Default value is false.
char 'u0000' 2 byte used to store any character
byte 0 1 byte used to save space in large arrays, mainly in place of integers, since a byte is four
times smaller than an integer. Default value is 0
short 0 2 byte used to save memory as byte data type. A short is 2 times smaller than an integer.
Default value is 0
int 0 4 byte used as the default data type for integral values unless there is a concern about
memory. The default value is 0
long 0L 8 byte used when a wider range than int is needed. Default value is 0L
float 0.0f 4 byte mainly used to save memory in large arrays of floating-point numbers. Default value
is 0.0f
double 0.0d 8 byte used as the default data type for decimal values, generally the default choice.
Default value is 0.0d
Primitive Data Types:
Operators
Operator is a symbol that is used to operate on variables to perform some operations. There are following types
of operator in java:
Unary Operator
It operates on only one variable. Unary operators are performed following operation:
• Increment Operator (++)
• Decrement Operator (--)
class Test {
public static void main (String args []) {
int x = 20;
System.out.pritln(x++);
System.out.pritln(++x);
System.out.pritln(x--);
System.out.pritln(--x);
} }
Output – 20
22
22
20
Operators
Arithmetic Operator
This operator is used to operate on two variables. (+, -, *, /, %). % - Return remainder value.
class Demo {
public static void main (String args []) {
int x = 20, y = 10;
int z = x + y;
System.out.pritln(z);
}
}
Output – 30
Operators
Shift Operator
This operator is used to shift all the bits in a value to the left or right side of a specified number of times. Shift
Operator are two types: Left shift (<<) and Right Shift Operator (>>).
class LeftShift {
public static void main (String args []) {
System.out.println (10<<2); //10*2^2=10*4=40
System.out.println (10<<3); //10*2^3=10*8=80
}
}
Output – 40
80
class RightShift {
public static void main (String args []) {
System.out.println (10>>2); //10 / 2^2=10 / 4=2
System.out.println (20>>3); //20 / 2^3=20 / 8=2
System.out.println (80>>4); //80 / 2^4=80 / 16=5
}}
Output – 2
2
5
Operators
Relational Operator
Operator Description Example (a=10, b=20)
equal to (==) Checks that the value of two variables are equal or not. Return true if
equal otherwise false.
(a == b) is false
not equal to (!=) Checks that the value of two variables are equal or not. Return true if
equal otherwise false.
(a != b) is true
greater than (>) Checks that the value of left variable is greater than the right one. If
yes then true.
(a>b) is not true
less than (<) Checks that the value of left variable is smaller than the right one. If
yes then true.
(a<b) is true
Greater than or equal
to (>=)
Checks that the value of left variable is greater than or equal to the
right one. If yes then true.
(a >= b) is not true
less than or equal to
(<=)
Checks that the value of left variable is smaller than or equal to the
right one. If yes then true.
(a <= b) is true
Operators
Logical Operator
Operator Description
Example (a is true & b is
false)
&&
Called logical AND operator. If the value of both variables is
non-zero, then condition is true.
(a && b) is false
||
Called logical OR operator. If the value of any one variable is
non-zero, then condition is true.
(a || b) is true
!
Called logical NOT operator. Use to reverse the variable’s
logical state. If a condition is false then logical NOT operator
will make true.
! (a && b) is true
Operators
Assignment Operator
Operator Example
=
z = x + y, Here the value of “x + y” assign into z.
+=
x += y means that x = x + y
-=
x -= y means that x = x - y
*=
x *= y means that x = x * y
/=
x /= y means that x = x / y
%=
x %= y means that x = x % y, etc.
Operators
Ternary Operator
It is also known as Conditional Operator. This operator consists of three variables and is used to evaluate
boolean expressions.
Syntax – <Data Type> <Variable Name> = <Expression>? <Value if True>:<Value if False>
class Test {
public static void main (String args []) {
int a=2;
int b=5;
int min=(a<b)? a: b;
System.out.println(min);
}
}
Output - 2
Keywords
Keywords are also known as reserved words. These are predefined words by java so it can not be used as a
variable or object name. There are many keywords are used in java. Here we discussed few keywords:
this keyword - In java, this is a reference variable that refers to the current class object. In other words, we can
say that this keyword is used to differentiate local and instance variable when both the variable name is same.
Uses of this keyword
• this keyword is used to call current class method.
• this () is used to invoke current class constructor.
• this can be passed as an argument in method and constructor call.
Keywords
abstract - It is used to declare an abstract class. Abstract class can have abstract and non-abstract method.
catch – In java catch keyword is used catch the exception generated by try statements.
class – Java class keyword is used to declare a class.
extends – This keyword indicate that a class is derived from another class or interface.
final – Java final keyword is used to make a constant.
import – This keyword is used to makes classes and interfaces accessible to current class code.
abstract, boolean, break, byte, case, class, char, catch, default, continue, do,
double, else, enum, extends, final, finally, if, for, implements, import,
instanceof, int, interface, long, native, null, new, package, public, protected,
private, return, short, static, strictfp, super, switch, synchronized, this, throw,
throws, try, transient, void, while, volatile
OOPs Concept
 OOPs stands for Object-Oriented Programming System.
 As a language that has the Object-Oriented feature, Java supports the following fundamental concepts:
 Object
 Class
 Abstraction
 Inheritance
 Encapsulation
 Polymorphism
 The mail goal of Object-Oriented Programming is to implement real-world entities, such as classes, object,
etc.
 Java, Python, C#, etc. are the example of object-oriented programming.
OOPs Concept
Object
 Everything in the world that has state and behavior is an object such as human has states – name, color and
behavior – performing some action.
 An object can also be defined as “an instance of a class”.
 There are two ways to create an object :
1st Method
Test t = new Test();
Here Test is a class, t is reference variable that is assigned by object (new Test()) and Test() is a constructor. In
this method declaration of reference variable and initialization both happened together.
2nd Method
Test t;
t = new Test();
In this method first declare reference variable after that initialize it.
OOPs Concept
Class
 A class is a keyword that are used to create a class in java.
 Class can also be defined as a collection of similar type of objects.
 Class is a blueprint/template that describes the state and behavior of an object.
Ex.
class Test {
// Statements;
}
Abstraction
Showing the functionality and hiding the internal details is called Abstraction. To achieve abstraction in java
through Abstract class and Interfaces.
E.g. Send Email – We don’t know the internal details after sending mail.
OOPs Concept
Inheritance
 To inherit the common properties from base class to subclass is called inheritance.
 The class which inherits the properties of other class is known as sub class (derived class, child class) and the
class whose properties are inherited is called base class (super class, parent class).
 The extends is used to inherit the properties of the base class.
Syntax –
class Test {
// properties;
}
class Test1 extends Test {
………………
………………
}
OOPs Concept
Types of Inheritance
There are five types of inheritance but in java only three are supported. Here we discussed all types of
inheritance as follows:
1. Single Inheritance - In this type of inheritance, one parent and one child class exist.
OOPs Concept
2. Multilevel Inheritance - In this type of inheritance a generation concept involves such as grandparent,
parent and child class.
OOPs Concept
3. Hierarchical Inheritance - In this type of inheritance, one parent and multiple child exist.
OOPs Concept
4. Multiple Inheritance - In this type of inheritance, more than one parent exists.
OOPs Concept
5. Hybrid Inheritance - It is a combination of all above types of inheritance.
Note: - Multiple and Hybrid inheritance are not supported in java.
Advantages of Inheritance –
 It provides code reusability
 It is used to achieve runtime polymorphism
 Memory Management
OOPs Concept
Encapsulation
 Wrapping or binding of data and code together in a single unit is called encapsulation.
 The variables of a class will be hidden from other classes, and can be accessed only through the methods of
their current class. Therefore, it is also known as data hiding.
 A java class is the example of encapsulation. Java Bean is the fully implementation of encapsulation because
all the data members are private in bean class.
OOPs Concept
Polymorphism
 Polymorphism is the ability of an object to take many forms.
 In other words, we can say that if one task can be implemented in many ways.
 For example, the role of a boy can play differently such as student, friend, brother, son, etc.
 We achieve polymorphism through method overloading and overriding.
OOPs Concept
Association
 In association, an object can be associated with one or many objects. There can be four types of association
between the objects.
 One to one
 One to many
 Many to one
 Many to many
Let's understand the relationship with real-time examples. For example, one country can have one prime
minister (one to one), and a prime minister can have many ministers (one to many). Also, many MP's can have
one prime minister (many to one), and many ministers can have many departments (many to many).
 Association can be unidirectional or bidirectional.
OOPs Concept
Aggregation
 Aggregation represents the relationship where one object contains other objects as a part of its state.
 It represents the weak relationship between objects.
 To achieve association, we use aggregation.
Object-Oriented Vs. Object Based Programming
 Object-based programming language follows all the features of OOPs except Inheritance.
 JavaScript and VBScript are examples of object-based programming languages.
Lecture - 3 Variables-data type_operators_oops concept

Mais conteúdo relacionado

Mais procurados

Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
CBSE VS SOA Presentation
CBSE VS SOA PresentationCBSE VS SOA Presentation
CBSE VS SOA PresentationMaulik Parikh
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variablesPushpendra Tyagi
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android Aakash Ugale
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programmingElizabeth Thomas
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & PackagesArindam Ghosh
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVAAnkita Totala
 
Variable and constants in Vb.NET
Variable and constants in Vb.NETVariable and constants in Vb.NET
Variable and constants in Vb.NETJaya Kumari
 

Mais procurados (20)

Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
CBSE VS SOA Presentation
CBSE VS SOA PresentationCBSE VS SOA Presentation
CBSE VS SOA Presentation
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
Java basic introduction
Java basic introductionJava basic introduction
Java basic introduction
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Introduction of java
Introduction  of javaIntroduction  of java
Introduction of java
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
JAVA PPT Part-1 BY ADI.pdf
JAVA PPT Part-1 BY ADI.pdfJAVA PPT Part-1 BY ADI.pdf
JAVA PPT Part-1 BY ADI.pdf
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Variable and constants in Vb.NET
Variable and constants in Vb.NETVariable and constants in Vb.NET
Variable and constants in Vb.NET
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 

Semelhante a Lecture - 3 Variables-data type_operators_oops concept

Semelhante a Lecture - 3 Variables-data type_operators_oops concept (20)

Basic_Java_02.pptx
Basic_Java_02.pptxBasic_Java_02.pptx
Basic_Java_02.pptx
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
 
Java q ref 2018
Java q ref 2018Java q ref 2018
Java q ref 2018
 
Java session4
Java session4Java session4
Java session4
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Java basic concept
Java basic conceptJava basic concept
Java basic concept
 
java
java java
java
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming Needs
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Javatut1
Javatut1 Javatut1
Javatut1
 
Java tut1
Java tut1Java tut1
Java tut1
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 

Mais de manish kumar

Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packagesmanish kumar
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritancemanish kumar
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statementmanish kumar
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywordsmanish kumar
 
Lecture - 1 introduction to java
Lecture - 1 introduction to javaLecture - 1 introduction to java
Lecture - 1 introduction to javamanish kumar
 

Mais de manish kumar (7)

Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Lecture - 1 introduction to java
Lecture - 1 introduction to javaLecture - 1 introduction to java
Lecture - 1 introduction to java
 

Último

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 

Último (20)

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 

Lecture - 3 Variables-data type_operators_oops concept

  • 2. Contents  Variables  Data Type  Operators  Keywords  OOP’s Concept
  • 3. Variables  A variable is a container that holds the value when the java program executed or we can say that a variable is a name of memory location.  The value of a variable can be changed. Syntax – <data type> <variable name> = <value of variable>  In java, there are three types of variable:  Local Variable  Instance Variable  Static Variable Local Variable – A variable declared inside methods, constructors, or blocks is called local variable. Few important points of local variable are as follows:  We can not use access modifier for local variable.
  • 4. Variables  We can not use access modifier for local variable.  The scope of local variable is within the method, constructor or block in which it is declared.  There is no default value for local variables, so local variable should be initialized at the declaration time.  A local variable can not be defined with static keyword. public class Demo { public void sum () { int x = 10, y=20, z=0; z = x + y; System.out.println(“Addition of x and y =” +z); } public static void main (String args []) { Demo d = new Demo (); d.sum (); } } Output - Addition of x and y = 30 public class Demo { public void sum () { int x+5; z = x + y; System.out.println(“Addition of x and y =” +z); } public static void main (String args []) { Demo d = new Demo (); d.sum (); } } Output - variable number might not have been initialized x = x + 5; ^ 1 error
  • 5. Variables Instance Variable – A variable declared outside the body of methods, constructors, or blocks but inside the class is called instance variable. Access Modifiers can be used with instance variable. class Test { int x=10;//instance variable void show () { int y=20;//local variable } } Static Variable - A variable declared with static keyword is called static variable. Static variable can be shared among all the instances of the class. Static variable cannot be local. Ex. static int x = 10;
  • 6. Variables Naming Convention  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. Ex. class Employee { int id; //code snippet }
  • 7. Data Type  Data type means that define the data or variables.  Based on the data type of a variable, the operating system allocates memory for a variable and decides what can be stored in the memory.  There are two types of data type in java as follows:  Reference Data Types  Primitive Data Types Reference Data Type - The reference data types include Classes, Interfaces and Arrays. The default value of any reference data type in null. Reference variables are created by using constructors of the classes. Reference variables are used to access objects. Ex. Demo d = new Demo (); Here Demo is a class and d is a reference variable.
  • 8. Data Type Data Type Default Value Default size Description boolean false 1 bit for simple flags that track true/false conditions. Default value is false. char 'u0000' 2 byte used to store any character byte 0 1 byte used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer. Default value is 0 short 0 2 byte used to save memory as byte data type. A short is 2 times smaller than an integer. Default value is 0 int 0 4 byte used as the default data type for integral values unless there is a concern about memory. The default value is 0 long 0L 8 byte used when a wider range than int is needed. Default value is 0L float 0.0f 4 byte mainly used to save memory in large arrays of floating-point numbers. Default value is 0.0f double 0.0d 8 byte used as the default data type for decimal values, generally the default choice. Default value is 0.0d Primitive Data Types:
  • 9. Operators Operator is a symbol that is used to operate on variables to perform some operations. There are following types of operator in java: Unary Operator It operates on only one variable. Unary operators are performed following operation: • Increment Operator (++) • Decrement Operator (--) class Test { public static void main (String args []) { int x = 20; System.out.pritln(x++); System.out.pritln(++x); System.out.pritln(x--); System.out.pritln(--x); } } Output – 20 22 22 20
  • 10. Operators Arithmetic Operator This operator is used to operate on two variables. (+, -, *, /, %). % - Return remainder value. class Demo { public static void main (String args []) { int x = 20, y = 10; int z = x + y; System.out.pritln(z); } } Output – 30
  • 11. Operators Shift Operator This operator is used to shift all the bits in a value to the left or right side of a specified number of times. Shift Operator are two types: Left shift (<<) and Right Shift Operator (>>). class LeftShift { public static void main (String args []) { System.out.println (10<<2); //10*2^2=10*4=40 System.out.println (10<<3); //10*2^3=10*8=80 } } Output – 40 80 class RightShift { public static void main (String args []) { System.out.println (10>>2); //10 / 2^2=10 / 4=2 System.out.println (20>>3); //20 / 2^3=20 / 8=2 System.out.println (80>>4); //80 / 2^4=80 / 16=5 }} Output – 2 2 5
  • 12. Operators Relational Operator Operator Description Example (a=10, b=20) equal to (==) Checks that the value of two variables are equal or not. Return true if equal otherwise false. (a == b) is false not equal to (!=) Checks that the value of two variables are equal or not. Return true if equal otherwise false. (a != b) is true greater than (>) Checks that the value of left variable is greater than the right one. If yes then true. (a>b) is not true less than (<) Checks that the value of left variable is smaller than the right one. If yes then true. (a<b) is true Greater than or equal to (>=) Checks that the value of left variable is greater than or equal to the right one. If yes then true. (a >= b) is not true less than or equal to (<=) Checks that the value of left variable is smaller than or equal to the right one. If yes then true. (a <= b) is true
  • 13. Operators Logical Operator Operator Description Example (a is true & b is false) && Called logical AND operator. If the value of both variables is non-zero, then condition is true. (a && b) is false || Called logical OR operator. If the value of any one variable is non-zero, then condition is true. (a || b) is true ! Called logical NOT operator. Use to reverse the variable’s logical state. If a condition is false then logical NOT operator will make true. ! (a && b) is true
  • 14. Operators Assignment Operator Operator Example = z = x + y, Here the value of “x + y” assign into z. += x += y means that x = x + y -= x -= y means that x = x - y *= x *= y means that x = x * y /= x /= y means that x = x / y %= x %= y means that x = x % y, etc.
  • 15. Operators Ternary Operator It is also known as Conditional Operator. This operator consists of three variables and is used to evaluate boolean expressions. Syntax – <Data Type> <Variable Name> = <Expression>? <Value if True>:<Value if False> class Test { public static void main (String args []) { int a=2; int b=5; int min=(a<b)? a: b; System.out.println(min); } } Output - 2
  • 16. Keywords Keywords are also known as reserved words. These are predefined words by java so it can not be used as a variable or object name. There are many keywords are used in java. Here we discussed few keywords: this keyword - In java, this is a reference variable that refers to the current class object. In other words, we can say that this keyword is used to differentiate local and instance variable when both the variable name is same. Uses of this keyword • this keyword is used to call current class method. • this () is used to invoke current class constructor. • this can be passed as an argument in method and constructor call.
  • 17. Keywords abstract - It is used to declare an abstract class. Abstract class can have abstract and non-abstract method. catch – In java catch keyword is used catch the exception generated by try statements. class – Java class keyword is used to declare a class. extends – This keyword indicate that a class is derived from another class or interface. final – Java final keyword is used to make a constant. import – This keyword is used to makes classes and interfaces accessible to current class code. abstract, boolean, break, byte, case, class, char, catch, default, continue, do, double, else, enum, extends, final, finally, if, for, implements, import, instanceof, int, interface, long, native, null, new, package, public, protected, private, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, try, transient, void, while, volatile
  • 18. OOPs Concept  OOPs stands for Object-Oriented Programming System.  As a language that has the Object-Oriented feature, Java supports the following fundamental concepts:  Object  Class  Abstraction  Inheritance  Encapsulation  Polymorphism  The mail goal of Object-Oriented Programming is to implement real-world entities, such as classes, object, etc.  Java, Python, C#, etc. are the example of object-oriented programming.
  • 19. OOPs Concept Object  Everything in the world that has state and behavior is an object such as human has states – name, color and behavior – performing some action.  An object can also be defined as “an instance of a class”.  There are two ways to create an object : 1st Method Test t = new Test(); Here Test is a class, t is reference variable that is assigned by object (new Test()) and Test() is a constructor. In this method declaration of reference variable and initialization both happened together. 2nd Method Test t; t = new Test(); In this method first declare reference variable after that initialize it.
  • 20. OOPs Concept Class  A class is a keyword that are used to create a class in java.  Class can also be defined as a collection of similar type of objects.  Class is a blueprint/template that describes the state and behavior of an object. Ex. class Test { // Statements; } Abstraction Showing the functionality and hiding the internal details is called Abstraction. To achieve abstraction in java through Abstract class and Interfaces. E.g. Send Email – We don’t know the internal details after sending mail.
  • 21. OOPs Concept Inheritance  To inherit the common properties from base class to subclass is called inheritance.  The class which inherits the properties of other class is known as sub class (derived class, child class) and the class whose properties are inherited is called base class (super class, parent class).  The extends is used to inherit the properties of the base class. Syntax – class Test { // properties; } class Test1 extends Test { ……………… ……………… }
  • 22. OOPs Concept Types of Inheritance There are five types of inheritance but in java only three are supported. Here we discussed all types of inheritance as follows: 1. Single Inheritance - In this type of inheritance, one parent and one child class exist.
  • 23. OOPs Concept 2. Multilevel Inheritance - In this type of inheritance a generation concept involves such as grandparent, parent and child class.
  • 24. OOPs Concept 3. Hierarchical Inheritance - In this type of inheritance, one parent and multiple child exist.
  • 25. OOPs Concept 4. Multiple Inheritance - In this type of inheritance, more than one parent exists.
  • 26. OOPs Concept 5. Hybrid Inheritance - It is a combination of all above types of inheritance. Note: - Multiple and Hybrid inheritance are not supported in java. Advantages of Inheritance –  It provides code reusability  It is used to achieve runtime polymorphism  Memory Management
  • 27. OOPs Concept Encapsulation  Wrapping or binding of data and code together in a single unit is called encapsulation.  The variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.  A java class is the example of encapsulation. Java Bean is the fully implementation of encapsulation because all the data members are private in bean class.
  • 28. OOPs Concept Polymorphism  Polymorphism is the ability of an object to take many forms.  In other words, we can say that if one task can be implemented in many ways.  For example, the role of a boy can play differently such as student, friend, brother, son, etc.  We achieve polymorphism through method overloading and overriding.
  • 29. OOPs Concept Association  In association, an object can be associated with one or many objects. There can be four types of association between the objects.  One to one  One to many  Many to one  Many to many Let's understand the relationship with real-time examples. For example, one country can have one prime minister (one to one), and a prime minister can have many ministers (one to many). Also, many MP's can have one prime minister (many to one), and many ministers can have many departments (many to many).  Association can be unidirectional or bidirectional.
  • 30. OOPs Concept Aggregation  Aggregation represents the relationship where one object contains other objects as a part of its state.  It represents the weak relationship between objects.  To achieve association, we use aggregation. Object-Oriented Vs. Object Based Programming  Object-based programming language follows all the features of OOPs except Inheritance.  JavaScript and VBScript are examples of object-based programming languages.