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

3-Intro To Java Constructs

Uploaded by

srishreyan10b
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

3-Intro To Java Constructs

Uploaded by

srishreyan10b
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

BASIC

PROGRAMMING
CONSTRUCTS
• Lexical Issues
• Data Types
• Variable
Topics • Operators
• Control & Looping Constructs
• Enhance For Loop
JAVA - Atomic Elements

Whitespac
Identifiers Literals Comments
e

Operators Separators Keywords


• Whitespace
• No Indentation rules
• Space between keywords
• Space, Tab or Newline
Lexical • Identifiers
Issues • Naming concept for Class / Methods /
Variables
• Sequence of Alpha- Numeric
• Special Symbol: $ and _ (Underscore)
• Case Sensitive
• Should not start with Number
1. Total
2. Frame2
3. TOTAL
4.
Find Valid $rs
5. 5test
and Invalid 6. var_1_int
Identifiers 7. Ticket-7
8. Course_1007
9. goto
• Literals
• Constant values in under data types (integer,
float, Character, String)
• Ex: ID = 12
• Price = 1500.45

Lexical • Section = ‘A’


• Dept = “Computer Science and Engg”
Issues • Comments
• Documentation purpose – lines are not executed
• Single line (\\)
• Multiple lines (\*………….*\)
• Separators
• ; (Semicolon)  statement end
• ( ) (parenthesis)  list of parameters
• { } (Braces)  code block (class / method)
Lexical • [ ] (Brackets)  Array index
Issues • , (comma)  identifier separation in variable
declaration statement
• . (period)  Access methods from
packages/sub packages / classes objects
• :: (colon)  Method access from its class
• Keywords
• 61 keywords
• Ex: new, int, char, private, class,
public, this, void, try, if, while, for
etc
Lexical • Used to build sentences

Issues • Reserved words; can’t use as


identifiers
• ‘const’ and ‘goto’ are reserved but
not used
• Additional Reserved Words: true,
false (Boolean values) & null
Data Types
• Each variable and expression has data
type
• Type defines the operations performed
over the value
• Data Types
Primitive • Integer (int, short, long, byte) – Signed
whole number
Data Types • Floating Point (float, double) – number with
fractional portions
• Character (char) – Symbols in a character set
• Boolean (boolean) – true / false value
• Width is defined as Bits
• How many Data Types?
• byte (8 bits)
• Range: -128 to 127
• File Streaming purpose
• short (16 bits)
• Rarely used
• Range: –32,768 to 32,767
Integer • int (32 bits)
• Range: –2,147,483,648 to 2,147,483,647
• Commonly used data type under Integer
• long (64 bits)
• Range: larger than int
• Used where value is more than int
• Ex: Number vehicles crossed the signal per
month
Output

Total Fee : Rs.1280000000


Output

Average = 74.71428571428571
Compilation Error
Character
• Unicode to represent all the
characters in natural languages
• 16 bits
• Range: 0 to 65,536
A
C
67
Output

Result is Pass: false


Revaluation is Pass: true
Changes in the Reval :true
Variables
• Storage location in the computer memory
• Has value, data type and identifier
• Variable Declaration
• Syntax
type identifier1, identifier2,…..identifier6;
Variables type identifier1 = value1, identifier2 = value2;
• Example:
Int a, b;
Float f = 3.14;
• Dynamic Initialization
• double r = pi * r * r;
• Scope : Object visibility to
other parts of program
Variables
• Scope determines lifetime of
the object
• Global & Local Scope
Status : true
Operators
Operator Meaning Example Result
+ Addition 5+5 10
- Subtraction 10 - 5 5
* Multiplication 2*8 16
/ Division 20 / 4 5
% Modulus 22 % 4 2

Arithmetic Operators
Operato Meaning Example Result Expansio
r n
+= Addition n = 10 11 n=n+1
Augmented Assignment n += 1
(compound)
Assignment Operators
-= Subtraction n = 10 9 n=n-1
• Arithmetic Assignment n -= 1
operators (+, -, *, /,
%) are combined *= Multiplicati n = 10 20 n=n*1
with assignment on n *= 2
(=) operator Assignment
/= Division n = 10 2 n=n/1
Assignment n /= 5

%= Modulus n = 10 1 n=n%1
Assignment n %= 3
Operator Meaning Example Result Expansion
++var Pre increment n = 10 11 n=n+1
n = ++n n=n

var++ Post increment n = 10 10 n=n


n = n++ n=n+1

--var Pre decrement n = 10 9 n=n-1


n = --n n=n

var-- Post decrement n = 10 10 n=n


n = n-- n=n-1

Increment and Decrement Operator


Bitwise Operator
Operator Meaning Example Result
~ Bitwise Unary NOT A=1 0
~A
& Bitwise AND A = 0, B = 0 0
A&B
| Bitwise OR A = 1, B = 0 1
A|B
^ Bitwise Exclusive OR A = 1, B = 0 1
A^B
>> Shift Right
>>> Shift Right Zero fill
<< Shift Left
Operator Meaning
&= Bitwise AND Assignment

Bitwise |= Bitwise OR Assignment


^= Bitwise Exclusive OR
Operator Assignment
>>= Shift Right Assignment
>>>= Shift Right zero fill
Assignment
<<= Shift Left Assignment
Relational Operator
Operator Meaning Example Result
A = true, B = false
== Equal A == B false
!= Not Equal A != B true
X = 2, Y = 4
> Greater than X>Y false
< Less than X<Y true
<= Less than or equal X <= Y true
>= Greater than or equal X >= Y false

True & False are non-numeric values; not equal to zero or one or non-zero like in C & C+
+
Boolean Logical Operator
Operator Meaning Example Result

A = true, B = false
& Logical AND A&B False
| Logical OR A|B True
^ Logical XOR A^B True
|| Short Circuit OR A || B True
&& Short circuit AND A && B False
! Logical Unary NOT !A False

X = true; Y = true
!X&!Y | X^Y|!Y ----> ?
Syntax
• variable = value;
Assignment • variable = expression;
Operator
Example
• w = 2;
• w = 3 + 6 – 1;
Ternary (three) operator
Syntax
Expression1 ? Expression2 : Expression3;
Expression1 is true  Expression2 will be
executed  Else, Expression3 will be
executed
The ?
Operator

Final Mark:49
++ (postfix), --(postfix), ++(prefix), --(prefix),
~, !, +(unary), -(unary)
Int a = 10, b = 2, c = 20;
*, /, %
Int t = a + b * c
+, -, >>, >>>, <<
>, >=, <, <=
Ans: 240 ? 50 ?
==, !=
&, |, ^, &&, ||, ?:, Ans: 50

Operator Precedence
Parenthesis
++ (postfix), --(postfix), +
+(prefix), --(prefix), Int a = 10, b = 2, c =
~, !, +(unary), -(unary) 20;
*, /, % Int t = (a + b) * c
+, -, >>, >>>, <<
>, >=, <, <= Ans: 240 ? 50 ?
==, != Ans: 240 HOW ???
&, |, ^, &&, ||, ?:,

Operator Precedence
DIY
• Write a Java program to calculate the area of a rectangle
• Create a program that converts temperature from Celsius to Fahrenheit
and vice versa
• Write a program that checks whether a given number is positive or
negative using the ternary operator
DIY - 2
• Implement a program that determines if a person is eligible to vote based
on their age.
• Develop a program that compares two numbers and prints whether the
first number is greater, smaller, or equal to the second number.
• Create a program to find the maximum of three numbers using the ternary
operator.

You might also like