0% found this document useful (0 votes)
4 views

3- Object Oriented Programming Java(Lecture-3) 500 4

The document provides an overview of Object Oriented Programming in Java, focusing on Java variables and data types. It explains the different types of variables, such as String, int, float, char, and boolean, along with rules for naming identifiers. Additionally, it categorizes data types into primitive and non-primitive, detailing the eight primitive data types and their characteristics.

Uploaded by

x9fx4vj55q
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)
4 views

3- Object Oriented Programming Java(Lecture-3) 500 4

The document provides an overview of Object Oriented Programming in Java, focusing on Java variables and data types. It explains the different types of variables, such as String, int, float, char, and boolean, along with rules for naming identifiers. Additionally, it categorizes data types into primitive and non-primitive, detailing the eight primitive data types and their characteristics.

Uploaded by

x9fx4vj55q
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
You are on page 1/ 11

Object Oriented Programming (Java)

Java Programming basics


Text Book

Title: Java How to Program, Early Objects


Author(s): Paul Deitel, Harvey Deitel
Publisher: Pearson Education
Year: 2015
ISBN: 0133807800,9780133807806

Object Oriented Programming using Java by Simon Kendal

2019 - Modern Programming (Java) 1-2


Learning Outcomes

 Students will be able to understand Java Variables

 Will be able to use different Java Data Types


Java Variables
• Variables are containers for storing data values.

• In Java, there are different types of variables, for example:

String - stores text, such as "Hello". String values are surrounded by double quotes
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single
quotes
boolean - stores values with two states: true or false

4
Identifiers
• All Java variables must be identified with unique names.
• These unique names are called identifiers.
• Identifiers can be short names (like x and y) or more descriptive names
(age, sum, totalVolume).
• Note: It is recommended to use descriptive names in order to create
understandable and maintainable code.

5
// Good
int minutesPerHour = 60;

// OK, but not so easy to understand what m actually is


int m = 60;

6
The general rules for naming variables are:
• Names can contain letters, digits, underscores, and dollar signs
• Names must begin with a letter
• Names should start with a lowercase letter, and cannot contain
whitespace
• Names can also begin with $ and
• Names are case-sensitive ("myVar" and "myvar" are different variables)
• Reserved words (like Java keywords, such as int or boolean) cannot be
used as names

7
Java Data Types
• As explained before, a variable in Java must be a specified data type:

• Data types are divided into two groups:

Primitive data types - includes byte, short, int, long, float, double,
boolean and char
Non-primitive data types - such as String, Arrays and Classes (you will
learn more about these in a later chapter)

8
Primitive Data Types
• A primitive data type specifies the size and type of variable values, and it
has no additional methods.
• There are eight primitive data types in Java:
S.No Data Type Size Description
1. byte 1 byte Stores whole numbers from -128 to 127
2. short 2 bytes Stores whole numbers from -32,768 to 32,767
3. int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
4. long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
5. float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
6. double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
7. boolean 1 bit Stores true or false values
8. char 2 bytes Stores a single character/letter or ASCII values
9
Numbers

• Primitive number types are divided into two groups:

• Integer types stores whole numbers, positive or negative (such as 123 or -


456), without decimals.
• Valid types are byte, short, int and long.
• Which type you should use, depends on the numeric value.

• Floating point types represents numbers with a fractional part,


containing one or more decimals.
• There are two types: float and double.

10
Thank You…!

You might also like