SlideShare a Scribd company logo
Programming in Java
Lecture 2: Java Basics: Keywords,
Constants, Variables and Data Types
Contents
• Introduction
• Object Oriented Programming
• Identifiers
• Keywords
• Primitive Data Types
• Constants
• Variables
Introduction
• Java is an Object Oriented Programming language.
• Its main features are:
• Platform Independence
• Security
• What is the difference between Platform Independence
and Portability?
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
• 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.
• 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
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.
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.
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.
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
Primitive Data Types
• Classification of primitive data types
• Java defines eight primitive types of data:
• byte
• short
• int
• long
• char
• float
• double
• boolean
Primitive Data Types
Boolean Type Numeric Type
Integral Types Floating
point Types
Character Type Integer Types
boolean char byte short int long float double
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.
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.
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
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.
Type Width (bits) Approximate Range
Float 32 1.4e–045 to 3.4e+038
Double 64 4.9e–324 to 1.8e+308
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;
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.
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).
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
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)
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;
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
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.
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);
}
}
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.

More Related Content

PPT
Basic elements of java
PPTX
02 data types in java
PPTX
Java Datatypes
PDF
Java data types, variables and jvm
PPTX
Java Data Types and Variables
PPTX
Data Types, Variables, and Operators
PPT
Data types
PPT
Introduction to objects and inputoutput
Basic elements of java
02 data types in java
Java Datatypes
Java data types, variables and jvm
Java Data Types and Variables
Data Types, Variables, and Operators
Data types
Introduction to objects and inputoutput

What's hot (20)

PPT
M C6java3
PDF
Java basic data types
PDF
Chapter 01 Introduction to Java by Tushar B Kute
PPTX
Java basics and java variables
PPT
Primitive data types in java
PDF
Java essentials for hadoop
PPT
M C6java2
PPTX
Datatype introduction- JAVA
PDF
ITFT-Constants, variables and data types in java
PPT
Java: Primitive Data Types
PPT
Java Notes
PPTX
Classes objects in java
PPTX
Introduction to Java
ODP
JAVA Data Types - Part 1
PDF
ITFT - Java
PPTX
Android webinar class_java_review
PPTX
Learning core java
PDF
PDF
C++ Object oriented concepts & programming
PPT
M C6java7
M C6java3
Java basic data types
Chapter 01 Introduction to Java by Tushar B Kute
Java basics and java variables
Primitive data types in java
Java essentials for hadoop
M C6java2
Datatype introduction- JAVA
ITFT-Constants, variables and data types in java
Java: Primitive Data Types
Java Notes
Classes objects in java
Introduction to Java
JAVA Data Types - Part 1
ITFT - Java
Android webinar class_java_review
Learning core java
C++ Object oriented concepts & programming
M C6java7
Ad

Viewers also liked (11)

PPTX
Decision making in JAVA
PPT
Java
PPTX
Presentation
PPTX
OOPS Basics With Example
PPTX
Java notes(OOP) jkuat IT esection
PPT
Java features
PPT
Network programming in Java
PPTX
Python: Multiple Inheritance
PPTX
Java 101 intro to programming with java
PPS
Java Exception handling
PPT
Type Casting in C++
Decision making in JAVA
Java
Presentation
OOPS Basics With Example
Java notes(OOP) jkuat IT esection
Java features
Network programming in Java
Python: Multiple Inheritance
Java 101 intro to programming with java
Java Exception handling
Type Casting in C++
Ad

Similar to L2 datatypes and variables (20)

DOC
java handout.doc
PDF
L2 datatypes and variables
PPT
demo1 java of demo 1 java with demo 1 java.ppt
PPTX
Chapter i(introduction to java)
PPTX
Unit-1_GHD.pptxguguigihihihihihihoihihhi
PPTX
Introduction to Java Programming
PPTX
JAVA LESSON-01.pptx
PPTX
Introduction to Java Basics Programming Java Basics-I.pptx
PPTX
OOP-java-variables.pptx
PPSX
Java session3
PPT
Introduction to-programming
PPTX
Introduction to java Programming Language
PPTX
UNIT – 2 Features of java- (Shilpa R).pptx
PPTX
Unit-1 Data Types and Operators.pptx to computers
PPTX
Java data types
PPTX
java Basic Programming Needs
PPTX
Java fundamentals
PPTX
Core java
PPTX
Core java
PPT
Java introduction
java handout.doc
L2 datatypes and variables
demo1 java of demo 1 java with demo 1 java.ppt
Chapter i(introduction to java)
Unit-1_GHD.pptxguguigihihihihihihoihihhi
Introduction to Java Programming
JAVA LESSON-01.pptx
Introduction to Java Basics Programming Java Basics-I.pptx
OOP-java-variables.pptx
Java session3
Introduction to-programming
Introduction to java Programming Language
UNIT – 2 Features of java- (Shilpa R).pptx
Unit-1 Data Types and Operators.pptx to computers
Java data types
java Basic Programming Needs
Java fundamentals
Core java
Core java
Java introduction

More from teach4uin (20)

PPTX
Controls
PPT
validation
PPT
validation
PPT
Master pages
PPTX
.Net framework
PPT
Scripting languages
PPTX
Css1
PPTX
Code model
PPT
Asp db
PPTX
State management
PPT
security configuration
PPT
static dynamic html tags
PPT
static dynamic html tags
PPTX
New microsoft office power point presentation
PPT
.Net overview
PPT
Stdlib functions lesson
PPT
enums
PPT
memory
PPT
array
PPT
storage clas
Controls
validation
validation
Master pages
.Net framework
Scripting languages
Css1
Code model
Asp db
State management
security configuration
static dynamic html tags
static dynamic html tags
New microsoft office power point presentation
.Net overview
Stdlib functions lesson
enums
memory
array
storage clas

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Cloud computing and distributed systems.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Understanding_Digital_Forensics_Presentation.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Cloud computing and distributed systems.
Advanced methodologies resolving dimensionality complications for autism neur...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation_ Review paper, used for researhc scholars
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Big Data Technologies - Introduction.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
Spectroscopy.pptx food analysis technology
Digital-Transformation-Roadmap-for-Companies.pptx
Electronic commerce courselecture one. Pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

L2 datatypes and variables

  • 1. Programming in Java Lecture 2: Java Basics: Keywords, Constants, Variables and Data Types
  • 2. Contents • Introduction • Object Oriented Programming • Identifiers • Keywords • Primitive Data Types • Constants • Variables
  • 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?
  • 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
  • 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.
  • 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
  • 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.
  • 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.
  • 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.
  • 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
  • 11. Primitive Data Types • Classification of primitive data types • Java defines eight primitive types of data: • byte • short • int • long • char • float • double • boolean
  • 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
  • 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.
  • 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.
  • 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
  • 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.
  • 17. Type Width (bits) Approximate Range Float 32 1.4e–045 to 3.4e+038 Double 64 4.9e–324 to 1.8e+308
  • 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;
  • 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.
  • 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).
  • 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
  • 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)
  • 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;
  • 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
  • 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.
  • 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); } }
  • 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.

Editor's Notes

  • #2: 이 TP에서는 자바의 생성 배경과 그 동안 자바가 어떻게 발전해 왔는지에 대해 설명한다.
  • #4: 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. If the source code strictly follows POSIX standard less likely one end up modifying it. Platform independence focuses on ability of software to run on VIRTUAL hardware that inturn interfaces with the PHYSICAL hardware. Examples of cross-platform or platform independent languages are Python, JavaScript, Java etc. Read more at http://www.geekinterview.com/question_details/28891#otcDwPsDLqwPt0Q5.99 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. If the source code strictly follows POSIX standard less likely one end up modifying it. Platform independence focuses on ability of software to run on VIRTUAL hardware that inturn interfaces with the PHYSICAL hardware. Examples of cross-platform or platform independent languages are Python, JavaScript, Java etc. Read more at http://www.geekinterview.com/question_details/28891#otcDwPsDLqwPt0Q5.99 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. If the source code strictly follows POSIX standard less likely one end up modifying it. Platform independence focuses on ability of software to run on VIRTUAL hardware that inturn interfaces with the PHYSICAL hardware. Examples of cross-platform or platform independent languages are Python, JavaScript, Java etc. Read more at http://www.geekinterview.com/question_details/28891#otcDwPsDLqwPt0Q5.99