0% found this document useful (0 votes)
19 views31 pages

Lecture 2A Data Types and Variables

Uploaded by

f20230634
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views31 pages

Lecture 2A Data Types and Variables

Uploaded by

f20230634
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lecture 2A

Data Types and Variables

Course: Object Oriented Programming (ECOM F213)


13-08-25

Prof. Anita Agrawal


BITS [Link] Goa campus
Quick recap..
• Software objects are conceptually similar to real-world objects:
they too consist of state and related behavior.

8/21/2025
• An object stores its state in fields (variables in some
programming languages) and exposes its behavior through

Anita Agrawal
methods (functions in some programming languages).

• Methods operate on an object's internal state and serve as the


primary mechanism for object-to-object communication.

2
• In this session……..

8/21/2025
• Variable naming rules and conventions
• Basic data types

Anita Agrawal
• Default values and
• Literals.

3
Types of variables
• 4 types:

8/21/2025
• Instance variables

Anita Agrawal
• Class variables

• Local variables

• Parameters
4
Types of variables
• Instance variables ( non-static fields):
• Objects store their individual states in non-static fields

8/21/2025
• Their values are unique to each instance of a class (to
each object, in other words)

Anita Agrawal
• Example: the currentSpeed of one bicycle is
independent from the currentSpeed of another

5
Types of variables
• Class variables (static fields):
• This tells the compiler that there is exactly one copy of

8/21/2025
this variable in existence, regardless of how many times
the class has been instantiated.

Anita Agrawal
• Example: The same number of legs will apply to all
instances.

static int legs = 4;

6
Types of Variables
• Local Variables:

• Used by methods.

8/21/2025
• A method will often store its temporary state in local
variables.

Anita Agrawal
• Only visible to the methods in which they are
declared; they are not accessible from the rest of the
class.
• Example:
void calculate()
{
int count = 0; Local
variable
…. 7
}
Types of Variables
• Parameter Variables:
• Parameters are always classified as "variables" not

8/21/2025
"fields".

• This applies to other parameter-accepting constructs

Anita Agrawal
as well (such as constructors and exception handlers)

• Example:
void cost_of_the_car (int rupees)
Here, the rupees variable is the parameter to this
method.
8
Java Identifiers and syntax for variable
• The syntax for the declaration of a variable is:

8/21/2025
Data type identifier;

“data type” ( one of the primitive types)

Anita Agrawal
“identifier” is a legal Java identifier

9
Legal Java Identifiers
For class, method, variable or label…..
• Alphanumeric characters, A-Z, a-z, digits 0-9, ‘$‘and ‘_‘
(underscore).

8/21/2025
• Reserved Words can’t be used.
• Ex: double abstract = 100; (invalid)

Anita Agrawal
• The first letter should not be a digit.
• Ex: “1prog” (invalid)

• Are case-sensitive.

• No limit on the length of the identifier but recommended


length of 4 – 15 letters only.
10
Primitive Data type
• The Java programming language is statically-typed.

8/21/2025
• A variable's data type determines the values it may
contain, plus the operations that may be performed on it.

Anita Agrawal
• Primitive types are special data types built into the
language; they are not objects created from a class.

• A primitive type is predefined by the language and is


named by a reserved keyword.

11
• There are eight primitives in Java.

• All types have strictly defined range

8/21/2025
Anita Agrawal
12
Data
Types
Floating
integer boolean character
Point

8/21/2025
byte float

Anita Agrawal
short double

int

long

Primitive Types 13
Primitive Data Types in Java: Integers

8/21/2025
Anita Agrawal
• All of the above are stored as signed two’s complement
numbers

• Java does not support unsigned positive only integers


14
Primitive data types in Java: floating-
point numbers
Also known as real numbers

8/21/2025
• Two kinds of floating point types to store: –

Anita Agrawal
• float type (Single precision)

• double Type (Double precision)

15
Primitive Data Types in Java: floating
point numbers
• IEEE 754 single-precision binary floating-point format:
• Sign bit: 1 bit

8/21/2025
• Exponent width: 8 bits
• Significand precision: 23 bits (explicitly stored)

Anita Agrawal
16
Primitive Data Types in Java: floating
point numbers
• IEEE 754 double-precision binary floating-point format:
• Sign bit: 1 bit

8/21/2025
• Exponent width: 11 bits
• Significand precision: 52 bits (explicitly stored)

Anita Agrawal
17
Java – C: A comparison
• Java is a strongly typed language.

8/21/2025
• C is a weakly typed language

Example: (In C…)

Anita Agrawal
int main()
{
int i= 3.0;
printf("Value of i is %d", i);
return 0;
}
18
Output: Value of i is 3
• Example: (In Java)
class example
{
public static void main(String args[])
{

8/21/2025
int i= 3.0;
[Link]("value of i: "+i);
}

Anita Agrawal
}
Type mismatch throws an error.
error: incompatible types:
Possible lossy conversion from double to
int
int i=3.0;

19
Char type
• Java uses Unicode to represent characters.

8/21/2025
• Unicode defines fully international character set: English,
Latin, Greek, and many more

Anita Agrawal
• The minimum value of char data type is '\u0000' (0). The
maximum value of char data type is '\uffff'.

• It supports ASCII values also 0-255

20
Boolean Type
• Takes two values- true and false

8/21/2025
• Returned by relational operators and used by conditional
expressions

Anita Agrawal
21
String type
• In addition to the 8 primitive data types, the Java
programming language provides special support for

8/21/2025
character strings via the [Link] class.

• It is technically not a primitive data type.

Anita Agrawal
• String objects are immutable.

22
Literals

Anita Agrawal 8/21/2025


23
Literals
• A literal is the source code representation of a fixed
value

8/21/2025
• They are represented directly in your code without
requiring computation.

Anita Agrawal
• it's possible to assign a literal to a variable of a primitive
type.

24
Literals-Integer representation
• An integer literal is of type long if it ends with the letter L
or l; otherwise it is of type int

8/21/2025
• Integer literals can be expressed by the following number
systems:

Anita Agrawal
• Decimal
• Octal
• Hexadecimal
• Binary

25
Syntax to be followed for other number
systems
• int decval: 1238 //number in decimal

8/21/2025
• int Octval: 01237 //number in octal

Anita Agrawal
• int Hexval: 0x12ABC //number in hexadec

• int binval: 0b10110 //number in binary

26
Literals: Floating point
• Floating-point literals are written with a decimal point.
• Can be represented as
–Standard Notation: 3.1234, 56.778

8/21/2025
–Scientific Notation: 6.022E23, 1234E-13, 23e+100

• Floating-point literals are by default double

Anita Agrawal
• For example, 5.0 is considered a double value, not a float value.

• To store a literal as float, we have to append F or f to the constant

• float f = 2.335; // Error


• float f = 2.335f;// Correct way

27
Boolean literals
• Used to represent logical values: true and false

8/21/2025
• True and False do not convert into numerical representation

• True≠1 and False≠0 (Unlike C/C++)

Anita Agrawal
• True and False can be only assigned to boolean variable

28
Character Literals
• Represented by enclosing in single quotes
• Example : char c = ‘A’;

8/21/2025
• The bit values can be converted into integers and
manipulated with integer operators, by enclosing in ‘ ‘.

Anita Agrawal
• Example : char c = ‘A’;
c++; // c now contains ‘B’
• Example:
char letter = '\u0065';

29
Escape Sequences in Java

8/21/2025
Anita Agrawal
There are only 8 escape sequences in Java. You cannot define 30
your own character escape sequences
String Literals
• Strings are implemented as objects rather than an array
of characters

8/21/2025
• Sequence of characters enclosed in a pair of double
quotes.
• Example: “Hello World!”

Anita Agrawal
31

You might also like