0% found this document useful (0 votes)
22 views34 pages

Lecture 3.

The document provides an overview of Object-Oriented Programming with Java, focusing on the fundamental concepts of tokens, operators, variables, and data types. It explains Java tokens, including keywords, identifiers, constants, special symbols, and comments, as well as various types of operators such as arithmetic, logical, and bitwise. Additionally, it covers variable types (local, instance, and class/static) and differentiates between primitive and non-primitive data types in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views34 pages

Lecture 3.

The document provides an overview of Object-Oriented Programming with Java, focusing on the fundamental concepts of tokens, operators, variables, and data types. It explains Java tokens, including keywords, identifiers, constants, special symbols, and comments, as well as various types of operators such as arithmetic, logical, and bitwise. Additionally, it covers variable types (local, instance, and class/static) and differentiates between primitive and non-primitive data types in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Object Oriented Programming with Java

(Subject Code: BCS-403)

Unit 1
Lecture 3

Department of Computer Science ,ABES Engineering College


Lecture 3
• Tokens of Java:
• Operators
• Variables
• Data types
• Comments

Department of Computer Science ,AB


ES Engineering College
Java Tokens
In Java, Tokens are the smallest elements of a program
that is meaningful to the compiler. They are also known
as the fundamental building blocks of the program.
Tokens can be classified as follows:
• Keywords
• Identifiers
• Constants
• Special Symbols
• Operators
• Comments
• Separators
1. Keyword:
Keywords are pre-defined or reserved words in a
programming language. Each keyword is meant
to perform a specific function in a program.
Since keywords are referred names for a
compiler, they can’t be used as variable names
because by doing so, we are trying to assign a
new meaning to the keyword which is not
allowed.
abstract assert boolean
break byte case
catch char class
const continue default
2. Identifiers:
Identifiers are used as the general terminology for
naming of variables, functions and arrays. These are
user-defined names consisting of an arbitrarily long
sequence of letters and digits with either a letter or
the underscore (_) as a first character. Identifier
names must differ in spelling and case from any
keywords.
Examples of valid identifiers:
MyVariable
MYVARIABLE
myvariable
x
i
x1
i1
3. Constants/Literals:
Constants are also like normal variables. But the
only difference is, their values cannot be
modified by the program once they are defined.
final data_type variable_name;
4. Special Symbols:
The following special symbols are used in Java having
some special meaning and thus, cannot be used for
some other purpose.

[] () {}, ; * =
5. Operators:
Java provides many types of operators which can
be used according to the need. They are
classified based on the functionality they
provide. Some of the types are-
• Arithmetic Operators
• Unary Operators
• Assignment Operator
• Relational Operators
• Logical Operators
• Ternary Operator
• Bitwise Operators
• Shift Operators
6. Comments:
In Java, Comments are the part of the program which
are ignored by the compiler while compiling the
Program. They are useful as they can be used to
describe the operation or methods in the program.
The Comments are classified as follows:
• Single Line Comments
• Multiline Comments
// This is a Single Line Comment
/*
This is a Multiline Comment
*/
7. Separators:
Separators are used to separate different parts of the
codes. It tells the compiler about completion of a
statement in the program. The most commonly and
frequently used separator in java is semicolon (;).
Operators in java
Operator in java is a symbol that is used to perform
operations. For example: +, -, *, / etc.
There are many types of operators in java which are given
below:
• Unary Operator,
• Arithmetic Operator,
• shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
Java Unary Operator Example: ++ and --

class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}
Output:
10
12
12
10
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}}
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
Java AND Operator Example:
Logical && and Bitwise &

• The logical && operator doesn't check second


condition if first condition is false. It checks
second condition only if first one is true.
• The bitwise & operator always checks both
conditions whether first condition is true or
false.
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}}
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a++<c);//false && true = false
System.out.println(a);//
10 because second condition is not checked
System.out.println(a<b&a++<c);//false & true = false
System.out.println(a);//
11 because second condition is checked
}}
Java OR Operator Example: Logical || and
Bitwise |

• The logical || operator doesn't check second


condition if first condition is true. It checks
second condition only if first one is false.
• The bitwise | operator always checks both
conditions whether first condition is true or
false.
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a>b||a<c);//true || true = true
System.out.println(a>b|a<c);//true | true = true
//|| vs |
System.out.println(a>b||a++<c);//true || true = true
System.out.println(a);//
10 because second condition is not checked
System.out.println(a>b|a++<c);//true | true = true
System.out.println(a);//
11 because second condition is checked
}}
Java Ternary Operator
class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
Output:
2
Java Assignment Operator
class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;//10+3 a=a+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}
Java Shift Operator Example: Left Shift

class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//
15*2^4=15*16=240
}}
Java Shift Operator Example: Right Shift

class OperatorExample{
public static void main(String args[]){
System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
}}
Java Variable Types

Variable is name of reserved area allocated in


memory.
A variable is a container which holds
the value while the Java program is
executed. A variable is assigned with
a data type.
It is a combination of "vary + able"
which means its value can be
changed. Eg: int data=10
There are three kinds of variables in Java:
• Local variables
• Instance variables
• Class/static variables

class A{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class
Local variables :
• Local variables are declared in methods, constructors, or
blocks.
• Local variables are created when the method,
constructor or block is entered and the variable will be
destroyed once it exits the method, constructor or block.
• Access modifiers cannot be used for local variables.
• Local variables are visible only within the declared
method, constructor or block.
• Local variables are implemented at stack level internally.
• There is no default value for local variables so local
variables should be declared and an initial value should
be assigned before the first use.
Instance variables :
• Instance variables are declared in a class, but
outside a method, constructor or any block.
• When a space is allocated for an object in the
heap a slot for each instance variable value is
created.
• Instance variables are created when an object is
created with the use of the key word 'new' and
destroyed when the object is destroyed.
• Instance variables hold values that must be
referenced by more than one method, constructor
or block, or essential parts of an object’s state
that must be present through out the class.
Class/static variables :
• Class variables also known as static variables
are declared with the static keyword in a class,
but outside a method, constructor or a block.
• There would only be one copy of each class
variable per class, regardless of how many
objects are created from it.
• Static variables are stored in static memory. It
is rare to use static variables other than
declared final and used as either public or
private constants.
Data Types in Java
• Data types specify the different sizes and values that
can be stored in the variable. There are two types of
data types in Java:

1. Primitive data types: The primitive data types include


boolean, char, byte, short, int, long, float and double.

2. Non-primitive data types: The non-primitive data


types include Classes, Interfaces, and Arrays.
Data Types in Java
Java Primitive Data Types
• In Java language, primitive data types are the
building blocks of data manipulation. These are the
most basic data types available in Java language
There are 8 types of primitive data types:
Data Type Description
Boolean Used for simple flags that track true/false conditions.
Eg: Boolean one = false

Byte Used to save memory in large arrays where the memory savings is most required. It can also be
used in place of "int" data type.
Eg: byte a = 10, byte b = -20

Short Used to save memory just like byte data type.


Eg: short s = 10000, short r = -5000

Int Used as a default data type for integral values unless if there is no problem about memory.
Eg: int a = 100000, int b = -200000

Long Used when you need a range of values more than those provided by int.
Eg: long a = 100000L, long b = -200000L

Float Use a float (instead of double) if you need to save memory in large arrays of floating point numbers.
Value should always end with ‘f’.
Eg: float f1 = 234.5f

Double Its value range is unlimited. The double data type is generally used for decimal values just like
float. Eg: double d1 = 12.3

Char The char data type is a single 16-bit Unicode character. The char data type is used to store
characters.
char letterA = 'A‘
Non-Primitive Data Type or Reference Data Types

● The Reference Data Types will contain a memory address of variable


values because the reference types won’t store the variable value
directly in memory.

● They are strings, objects, arrays, etc.


Strings
● Strings are defined as an array of characters.
● The difference between a character array and a string in Java is, that the
string is designed to hold a sequence of characters in a single variable
whereas, a character array is a collection of separate char type entities.
Class
● A class is a user-defined blueprint or prototype from which objects are
created. It represents the set of properties or methods that are common
to all objects of one type.
Object
● It is a basic unit of Object-Oriented Programming and represents real-life
entities.
Non-Primitive Data Type or Reference Data
Types

Interface
● Like a class, an interface can have methods
and variables, but the methods declared in an
interface are by default abstract (only method
signature, no body).

Array
● An array is a group of like-typed variables that
are referred to by a common name. Arrays in
Java work differently than they do in C/C++.

You might also like