SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
Programming in Java
Lecture 2: Java Basics: Keywords,
Constants, Variables and Data Types
By
Ravi Kant Sahu
Asst. Professor, LPU
Contents
• Introduction
• Object Oriented Programming
• Identifiers
• Keywords
• Primitive Data Types
• Constants
• Variables
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Introduction
• Java is an Object Oriented Programming language.
• Its main features are:
• Platform Independence
• Security
• What is the difference between Platform Independence
and Portability?
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Object Oriented Programming
• Object-oriented programming is a method of implementation
in which programs are organized as cooperative collections of
objects, each of which represents an instance of some class,
and whose classes are all members of one or more hierarchy of
classes united via inheritance relationships.
Basic Concepts:
• Object
• Classification
• Data Encapsulation
• Inheritance
• Polymorphism
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• Object: An object is a discrete(distinct) entity that has well-
defined attributes and behavior.
• Classification: Objects with common attributes, behavior and
relationships with other objects are grouped into a logical unit
called classes. This process is called Classification.
• Data Encapsulation: Encapsulation is the mechanism that
binds together code and the data it manipulates, and keeps both
safe from outside interference and misuse.
• In a class, One can not use the methods and data without any
object of that class.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• Inheritance: Inheritance is the process by which one object
acquires the properties of another object.
• Polymorphism: Polymorphism refers to a programming
language's ability to process objects differently depending on
their data type or class.
•A method or function behaves differently in different
conditions.
•Example: method overloading, overriding
• 7 + 5
• 10.38 + 1.62
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Data Types, Variables and Constants
• Java is a strongly typed language.
• It means:
• Every variable has a type
• Every expression has a type, and every type is strictly
defined
•All assignments, whether explicit or via parameter
passing in method calls, are checked for type
compatibility.
• Any type mismatches are errors.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Identifiers
• A name in a program is called an identifier.
• Identifiers can be used to denote classes, methods,
variables, and labels.
• An identifier may be any descriptive sequence of uppercase
and lowercase letters, numbers, or the underscore and dollar-
sign characters.
• Example: number, Number, sum_$, bingo, $$_100
Note: Identifiers must not begin with a number.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Keywords
• Keywords are reserved identifiers that are predefined in the
language.
• Cannot be used as names for a variable, class, or method.
• All the keywords are in lowercase.
• There are 50 keywords currently defined in the Java language.
•The keywords const and goto are reserved but not used.
• true, false, and null are also reserved.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Java Keywords
abstract char else goto long return throw
assert class enum if native short throws
boolean const extends implements new static this
break continue final import package strictfp transient
byte default finally instanceof private super void
case do float int protected switch try
catch double for interface public synchronized while and
volatile
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Primitive Data Types
• Classification of primitive data types
• Java defines eight primitive types of data:
• byte
• short
• int
• long
• char
• float
• double
• boolean
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Primitive Data Types
Boolean Type Numeric Type
Integral Types Floating
point Types
Character Type Integer Types
boolean char byte short int long float double
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
These primitive types can be put in four groups:
• Integers: includes byte, short, int, and long, which are for
whole-valued signed numbers.
• Floating-point numbers: includes float and double, which
represent numbers with fractional precision.
• Characters: includes char, which represents symbols in a
character set, like letters and numbers.
• Boolean: includes boolean, which is a special type for
representing true/false values.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Integers
• Java defines four integer types: byte, short, int, and
long.
•All of these are signed, positive and negative
values.
•Java does not support unsigned, positive-only
integers.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Type Width
(bits)
Range
Byte 8 –128 to 127
Short 16 –32,768 to 32,767
Int 32 –2,147,483,648 to 2,147,483,647
Long 64 –9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Floating-Points
• Floating-point numbers, also known as real numbers,
are used when evaluating expressions that require
fractional precision.
• There are two kinds of floating-point types, float and
double, which represent single- and double-precision
numbers, respectively.
•By default floating point constants are double type in
java.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Type Width (bits) Approximate Range
Float 32 1.4e–045 to 3.4e+038
Double 64 4.9e–324 to 1.8e+308
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Float
• Float specifies a single-precision value that uses 32 bits
of storage.
• Single precision is faster on some processors and takes
half as much space as double precision.
• Variables of type float are useful when we need a
fractional component with small precision.
• float a = 12.50f;
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Double
• Double provides high precision, and uses 64 bits to store
a value.
• Double precision is actually faster than single precision
on some modern processors.
• All transcendental math functions, such as sin( ), cos( ),
and sqrt( ), return double values.
• Double is useful when we need to maintain accuracy
over many iterative calculations.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Characters
• Data type used to store characters is char.
• Unlike C/C++ (8 bits), Java char is a 16-bit type.
• The range of a char is 0 to 65,536.
• There are no negative chars.
• char variables behave like integers (as shown in the
example).
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
class CharTest{
public static void main(String args[])
{
char c1;
c1 = ‘A’;
System.out.println("c1 is currently " + c1);
c1++;
System.out.println("c1 is now " + c1);
}
}
Output: c1 is currently A
c1 is now B
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Boolean
• Boolean can have only one of two possible values, true
or false.
•This is the return type of all relational operators.
• e.g. a < b (either true or false)
•Boolean is also the type required by the conditional
expressions that govern the control statements such as if
and for.
• e.g. if ( x == 5)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Constants
• The values of the constant can't be changed once its
declared.
•Constants are declared using the final keyword.
•Even though Java does not have a constant type, we can
achieve the same effect by declaring and initializing variables
that are static, public, and final.
•Example:
final int NUMBER_OF_HOURS_IN_A_DAY = 24;
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Variables
• The variable is the basic unit of storage in a Java
program.
• A variable is defined by the combination of an
identifier, a type, and an optional initializer.
• All variables have a scope, which defines their
visibility.
e.g. int a = 10;
type identifier value
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Initialization
• Static Initialization:
variables can be initialized using constants
e.g. char c = ‘M’;
or, char c;
c = ‘M’;
• Dynamic Initialization:
Java allows variables to be initialized dynamically,
using any expression valid at the time the variable is
declared.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
class Initialize
{
public static void main(String args[])
{
// a and b are statically initialized
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is “ + c);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Questions
Portability Vs Platform Independence
• Portability focuses on adaptation of software in various
OS, by recompiling the source to make the binary
compatible with the target OS and not necessarily
modifying the source.
• Platform independence focuses on ability of software to
run on VIRTUAL hardware that in turn interfaces with
the PHYSICAL hardware.
• Examples of cross-platform or platform independent
languages are Python, JavaScript, Java etc.

Weitere ähnliche Inhalte

Was ist angesagt?

3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and VariablesNilesh Dalvi
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm Madishetty Prathibha
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and InterfaceHaris Bin Zahid
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteTushar B Kute
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variablesPushpendra Tyagi
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVAAnkita Totala
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSkillwise Group
 
Java essentials for hadoop
Java essentials for hadoopJava essentials for hadoop
Java essentials for hadoopSeo Gyansha
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Sakthi Durai
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 

Was ist angesagt? (20)

Packages
PackagesPackages
Packages
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Internationalization
InternationalizationInternationalization
Internationalization
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
 
List classes
List classesList classes
List classes
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
Java Notes
Java NotesJava Notes
Java Notes
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 
ITFT - Java
ITFT - JavaITFT - Java
ITFT - Java
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
Java essentials for hadoop
Java essentials for hadoopJava essentials for hadoop
Java essentials for hadoop
 
Oops Concept Java
Oops Concept JavaOops Concept Java
Oops Concept Java
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 

Andere mochten auch

Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java ProgrammingRavi Kant Sahu
 
Keywords of java
Keywords of javaKeywords of java
Keywords of javaJani Harsh
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flowMohammed Sikander
 
Top 10 things a fresh programmer should know - Dao Ngoc Khanh
Top 10 things a fresh programmer should know - Dao Ngoc KhanhTop 10 things a fresh programmer should know - Dao Ngoc Khanh
Top 10 things a fresh programmer should know - Dao Ngoc KhanhDevDay.org
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi_Kant_Sahu
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi Kant Sahu
 
Becoming a Better Developer #WCA2
Becoming a Better Developer #WCA2Becoming a Better Developer #WCA2
Becoming a Better Developer #WCA2Brian Richards
 
Learn Java language fundamentals with Unit nexus
Learn Java language fundamentals with Unit nexusLearn Java language fundamentals with Unit nexus
Learn Java language fundamentals with Unit nexusUnit Nexus Pvt. Ltd.
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 

Andere mochten auch (16)

Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Keywords of java
Keywords of javaKeywords of java
Keywords of java
 
Exception handling
Exception handlingException handling
Exception handling
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
 
Top 10 things a fresh programmer should know - Dao Ngoc Khanh
Top 10 things a fresh programmer should know - Dao Ngoc KhanhTop 10 things a fresh programmer should know - Dao Ngoc Khanh
Top 10 things a fresh programmer should know - Dao Ngoc Khanh
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
Event handling
Event handlingEvent handling
Event handling
 
Packages
PackagesPackages
Packages
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Sp rao abap
Sp rao abapSp rao abap
Sp rao abap
 
Operators in java
Operators in javaOperators in java
Operators in java
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Applets
AppletsApplets
Applets
 
Becoming a Better Developer #WCA2
Becoming a Better Developer #WCA2Becoming a Better Developer #WCA2
Becoming a Better Developer #WCA2
 
Learn Java language fundamentals with Unit nexus
Learn Java language fundamentals with Unit nexusLearn Java language fundamentals with Unit nexus
Learn Java language fundamentals with Unit nexus
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 

Ähnlich wie L2 datatypes and variables

L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variablesteach4uin
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itlokeshpappaka10
 
Enumerations in java.pptx
Enumerations in java.pptxEnumerations in java.pptx
Enumerations in java.pptxSrizan Pokrel
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java Ravi_Kant_Sahu
 
Learning core java
Learning core javaLearning core java
Learning core javaAbhay Bharti
 
cs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxcs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxmshanajoel6
 
Lecture2_Datatypes_Variables.ppt
Lecture2_Datatypes_Variables.pptLecture2_Datatypes_Variables.ppt
Lecture2_Datatypes_Variables.pptAshutoshTrivedi30
 
intro_java (1).pptx
intro_java (1).pptxintro_java (1).pptx
intro_java (1).pptxSmitNikumbh
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentalsAnsgarMary
 
demo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.pptdemo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.pptFerdieBalang
 

Ähnlich wie L2 datatypes and variables (20)

L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
 
Chapter 4.pptx
Chapter 4.pptxChapter 4.pptx
Chapter 4.pptx
 
Enumerations in java.pptx
Enumerations in java.pptxEnumerations in java.pptx
Enumerations in java.pptx
 
L1 basics
L1 basicsL1 basics
L1 basics
 
Java Course in Chandigarh
Java Course in ChandigarhJava Course in Chandigarh
Java Course in Chandigarh
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 
Learning core java
Learning core javaLearning core java
Learning core java
 
cs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptxcs213Lecture_1 java programming oopsss.pptx
cs213Lecture_1 java programming oopsss.pptx
 
Lecture2_Datatypes_Variables.ppt
Lecture2_Datatypes_Variables.pptLecture2_Datatypes_Variables.ppt
Lecture2_Datatypes_Variables.ppt
 
Java Basics
Java BasicsJava Basics
Java Basics
 
intro_java (1).pptx
intro_java (1).pptxintro_java (1).pptx
intro_java (1).pptx
 
Java
JavaJava
Java
 
Java Jive 002.pptx
Java Jive 002.pptxJava Jive 002.pptx
Java Jive 002.pptx
 
Core Java
Core JavaCore Java
Core Java
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
 
Java basic
Java basicJava basic
Java basic
 
demo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.pptdemo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.ppt
 
Java introduction
Java introductionJava introduction
Java introduction
 

Mehr von Ravi_Kant_Sahu

Mehr von Ravi_Kant_Sahu (6)

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 
Event handling
Event handlingEvent handling
Event handling
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Jdbc
JdbcJdbc
Jdbc
 
Swing api
Swing apiSwing api
Swing api
 

Kürzlich hochgeladen

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 

Kürzlich hochgeladen (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

L2 datatypes and variables

  • 1. Programming in Java Lecture 2: Java Basics: Keywords, Constants, Variables and Data Types By Ravi Kant Sahu Asst. Professor, LPU
  • 2. Contents • Introduction • Object Oriented Programming • Identifiers • Keywords • Primitive Data Types • Constants • Variables Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. Introduction • Java is an Object Oriented Programming language. • Its main features are: • Platform Independence • Security • What is the difference between Platform Independence and Portability? Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. Object Oriented Programming • Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of one or more hierarchy of classes united via inheritance relationships. Basic Concepts: • Object • Classification • Data Encapsulation • Inheritance • Polymorphism Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. • Object: An object is a discrete(distinct) entity that has well- defined attributes and behavior. • Classification: Objects with common attributes, behavior and relationships with other objects are grouped into a logical unit called classes. This process is called Classification. • Data Encapsulation: Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. • In a class, One can not use the methods and data without any object of that class. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. • Inheritance: Inheritance is the process by which one object acquires the properties of another object. • Polymorphism: Polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. •A method or function behaves differently in different conditions. •Example: method overloading, overriding • 7 + 5 • 10.38 + 1.62 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. Data Types, Variables and Constants • Java is a strongly typed language. • It means: • Every variable has a type • Every expression has a type, and every type is strictly defined •All assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility. • Any type mismatches are errors. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 8. Identifiers • A name in a program is called an identifier. • Identifiers can be used to denote classes, methods, variables, and labels. • An identifier may be any descriptive sequence of uppercase and lowercase letters, numbers, or the underscore and dollar- sign characters. • Example: number, Number, sum_$, bingo, $$_100 Note: Identifiers must not begin with a number. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. Keywords • Keywords are reserved identifiers that are predefined in the language. • Cannot be used as names for a variable, class, or method. • All the keywords are in lowercase. • There are 50 keywords currently defined in the Java language. •The keywords const and goto are reserved but not used. • true, false, and null are also reserved. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. Java Keywords abstract char else goto long return throw assert class enum if native short throws boolean const extends implements new static this break continue final import package strictfp transient byte default finally instanceof private super void case do float int protected switch try catch double for interface public synchronized while and volatile Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11. Primitive Data Types • Classification of primitive data types • Java defines eight primitive types of data: • byte • short • int • long • char • float • double • boolean Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. Primitive Data Types Boolean Type Numeric Type Integral Types Floating point Types Character Type Integer Types boolean char byte short int long float double Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. These primitive types can be put in four groups: • Integers: includes byte, short, int, and long, which are for whole-valued signed numbers. • Floating-point numbers: includes float and double, which represent numbers with fractional precision. • Characters: includes char, which represents symbols in a character set, like letters and numbers. • Boolean: includes boolean, which is a special type for representing true/false values. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 14. Integers • Java defines four integer types: byte, short, int, and long. •All of these are signed, positive and negative values. •Java does not support unsigned, positive-only integers. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 15. Type Width (bits) Range Byte 8 –128 to 127 Short 16 –32,768 to 32,767 Int 32 –2,147,483,648 to 2,147,483,647 Long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 16. Floating-Points • Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. • There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively. •By default floating point constants are double type in java. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 17. Type Width (bits) Approximate Range Float 32 1.4e–045 to 3.4e+038 Double 64 4.9e–324 to 1.8e+308 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 18. Float • Float specifies a single-precision value that uses 32 bits of storage. • Single precision is faster on some processors and takes half as much space as double precision. • Variables of type float are useful when we need a fractional component with small precision. • float a = 12.50f; Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 19. Double • Double provides high precision, and uses 64 bits to store a value. • Double precision is actually faster than single precision on some modern processors. • All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. • Double is useful when we need to maintain accuracy over many iterative calculations. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 20. Characters • Data type used to store characters is char. • Unlike C/C++ (8 bits), Java char is a 16-bit type. • The range of a char is 0 to 65,536. • There are no negative chars. • char variables behave like integers (as shown in the example). Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 21. class CharTest{ public static void main(String args[]) { char c1; c1 = ‘A’; System.out.println("c1 is currently " + c1); c1++; System.out.println("c1 is now " + c1); } } Output: c1 is currently A c1 is now B Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 22. Boolean • Boolean can have only one of two possible values, true or false. •This is the return type of all relational operators. • e.g. a < b (either true or false) •Boolean is also the type required by the conditional expressions that govern the control statements such as if and for. • e.g. if ( x == 5) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 23. Constants • The values of the constant can't be changed once its declared. •Constants are declared using the final keyword. •Even though Java does not have a constant type, we can achieve the same effect by declaring and initializing variables that are static, public, and final. •Example: final int NUMBER_OF_HOURS_IN_A_DAY = 24; Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 24. Variables • The variable is the basic unit of storage in a Java program. • A variable is defined by the combination of an identifier, a type, and an optional initializer. • All variables have a scope, which defines their visibility. e.g. int a = 10; type identifier value Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 25. Initialization • Static Initialization: variables can be initialized using constants e.g. char c = ‘M’; or, char c; c = ‘M’; • Dynamic Initialization: Java allows variables to be initialized dynamically, using any expression valid at the time the variable is declared. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 26. class Initialize { public static void main(String args[]) { // a and b are statically initialized double a = 3.0, b = 4.0; // c is dynamically initialized double c = Math.sqrt(a * a + b * b); System.out.println("Hypotenuse is “ + c); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 28. Portability Vs Platform Independence • Portability focuses on adaptation of software in various OS, by recompiling the source to make the binary compatible with the target OS and not necessarily modifying the source. • Platform independence focuses on ability of software to run on VIRTUAL hardware that in turn interfaces with the PHYSICAL hardware. • Examples of cross-platform or platform independent languages are Python, JavaScript, Java etc.