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

Week 2 Lecture 4 - Programming Basics in Java Part 1

The document covers fundamental concepts in Object-Oriented Programming, focusing on lexical issues, tokens, identifiers, and data types in Java. It explains the significance of reserved words and modifiers, as well as the distinction between primitive and non-primitive data types. Key rules for naming identifiers and examples of various data types are also provided.

Uploaded by

uw-24-cs-bs-100
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)
2 views

Week 2 Lecture 4 - Programming Basics in Java Part 1

The document covers fundamental concepts in Object-Oriented Programming, focusing on lexical issues, tokens, identifiers, and data types in Java. It explains the significance of reserved words and modifiers, as well as the distinction between primitive and non-primitive data types. Key rules for naming identifiers and examples of various data types are also provided.

Uploaded by

uw-24-cs-bs-100
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/ 21

CS-112

Object Oriented Programming


DEPARTMENT OF COMPUTER SCIENCE
COURSE INSTRUCTOR : MS. HABIBA ARSHAD
Topics To be Covered Today

❑ Lexical Issues
❑ Reserve Words
❑ Data Types
Tokens/ Lexical Issues
Tokens are the smallest meaningful symbols in the language.
Token type Description/Purpose Examples
Keywords are the reserved names of a language and cannot be used as int, double, for, while
names of variables, functions etc. Words with special meaning
to the compiler
Identifiers refers to the names of variable, arrays, functions, classes, std, x, myFunction, sum
Interfaces etc.
Literals refer to fixed values that we cannot change in a program. "Hello, world!", 24.3, 0, ’c’
Operators are special symbols which operate on variable & constants, and +, -, &&, %, <
form an expression.
Punctuation Are the special characters used to separate statements. {}(),;
/Separators Punctuation defining the structure of a program
Whitespace Spaces of various sorts; ignored by the compiler Spaces, tabs, newlines,
comments
Identifiers
An identifier is a word used in a program to name
a variable, method (function), class, interface,
package etc.
Identifiers
To name an identifier you must follow some rules:
▪An identifier (a name) can contain only letters, digits 0 through 9, and the underscore
character (_).
▪The first character in an identifier cannot be a digit.
▪In particular, no name can contain a space or any other special character other than $
and underscore _.
▪There is no limit to the length of an identifier.
▪Java is case sensitive. That is, uppercase and lowercase letters are considered to be
different characters. For example, Java considers mystuff, myStuff, and MyStuff to be
three different identifiers, and you could have three different variables with these
three names.
▪Keywords cannot be used as identifiers.
Contin....
Reserve Words
oReserved words or keywords are words that have a specific meaning
to the compiler and cannot be used for other purposes in the
program.
oFor example:
When the compiler sees the word class, it understands that the word
after class is the name for the class.
Reserve Words
There are 50 reserved keywords in Java.
The keyword const and goto are reserved but not used.
All keywords are in lower case.
true, false, and null are literals not keywords .

We can’t use these reserved names as Identifiers (Variable or Class or


Interface names).
Keywords
Modifiers
oJava uses certain reserved words called modifiers that specify the
properties of the data, methods, and classes and how they can be
used.
oExamples of modifiers are public and static. Other modifiers are
private, final, abstract, and protected. A public method, or class can
be accessed by other programs/ classes. A private method cannot
be accessed by other programs. The code is only accessible within
the declared class.
Data Types
Data type describes the size and type of values that can be stored in a variable.

In any program, you need to store particular type of data in the computer’s
memory. The compiler should know the amount of memory that has to be
allocated to that particular data. It directs the compiler to allocate specific
amount of memory to a particular type of data.

Data types in Java can be broadly classified in two categories:


• Primitive data types/ Simple data types
• Non-Primitive Data Types/ Derived Data Types or Reference
Primitive Data Types: for storing
simple values and are predefined in
java
Boolean Data Type
Byte Data Type
Char

char alpha = 'J';


char a = 65, b = 66, c = 67;
System.out.println(alpha); // prints J
System.out.println(a); // Displays A
System.out.println(b); // Displays B
System.out.println(c); // Displays C
Short data type
Int Data type
long data type
Floating Data types
Double Data type
The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you
should end the value with a “d”:
Non-Primitive Data Types:
Used for storing complex objects. Created by the programmer and is
not defined by Java.
They are also called “reference variables” or “object references”
since they reference a memory location which stores the data.

You might also like