28
Programming in Java Lecture 2: Java Basics: Keywords, Constants, Variables and Data Types By Ravi Kant Sahu Asst. Professor, LPU

L2 datatypes and variables

Embed Size (px)

Citation preview

Page 1: L2 datatypes and variables

Programming in Java

Lecture 2: Java Basics: Keywords, Constants, Variables and Data Types

ByRavi Kant Sahu

Asst. Professor, LPU

Page 2: L2 datatypes and variables

Contents• Introduction• Object Oriented Programming • Identifiers• Keywords• Primitive Data Types• Constants • Variables

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 3: L2 datatypes and 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?

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 4: L2 datatypes and variables

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)

Page 5: L2 datatypes and variables

• 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)

Page 6: L2 datatypes and variables

• 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)

Page 7: L2 datatypes and variables

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)

Page 8: L2 datatypes and variables

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, $$_100Note: Identifiers must not begin with a number.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 9: L2 datatypes and variables

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)

Page 10: L2 datatypes and variables

Java Keywordsabstract 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)

Page 11: L2 datatypes and variables

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)

Page 12: L2 datatypes and variables

Primitive Data Types

Boolean Type Numeric Type

Integral Types Floating point Types

Character Type Integer Types

boolean char byte short int long float doubleRavi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 13: L2 datatypes and variables

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)

Page 14: L2 datatypes and variables

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)

Page 15: L2 datatypes and variables

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)

Page 16: L2 datatypes and variables

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 anddouble, 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)

Page 17: L2 datatypes and variables

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)

Page 18: L2 datatypes and variables

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)

Page 19: L2 datatypes and variables

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)

Page 20: L2 datatypes and variables

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)

Page 21: L2 datatypes and variables

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 Ac1 is now B

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 22: L2 datatypes and variables

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)

Page 23: L2 datatypes and variables

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)

Page 24: L2 datatypes and variables

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 valueRavi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 25: L2 datatypes and variables

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)

Page 26: L2 datatypes and variables

class Initialize {public static void main(String args[])

{// a and b are statically initializeddouble a = 3.0, b = 4.0;// c is dynamically initializeddouble c = Math.sqrt(a * a + b * b);System.out.println("Hypotenuse is “ + c);

}}

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 27: L2 datatypes and variables

Questions

Page 28: L2 datatypes and variables

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.