Core Java Part IV Computer Science YouTube Lecture Handouts

Get top class preparation for GATE Computer-Science right from your home: fully solved questions with step-by-step explanation- practice your way to success, Get detailed illustrated notes covering entire syllabus: point-by-point for high retention.

Topics to be covered

  • Constructors
  • Basic operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Assignment Operators
  • Decision Making Statements
    • if
    • if … else
    • Nested if
    • Switch statement
  • Looping Statements
    • for
    • while
    • do. . while

Constructors

A constructor is a special type of method/function that is executed whenever a class is instantiate. Means, it is not required to call the constructors using a class object. Whenever we create an object of a class, it get invoked.

  • A constructor must have the same name as class name.
  • It can contain arguments as its parameters.
  • It can be public, private or protected. An access specifiers specifies its accessibility in different classes such as child class, main class etc.
  • It cannot be static.
  • It cannot be declared as ‘final’ .

Code/Output

A Constructor

Operators Used in Java

Operators are symbols used for operations. There are many types of operators in JAVA

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
Operators Used in Java
Sr. No.Arithmetic OperatorOperationExample
1+ (Add)To add two numbersa + b
2- (Subtract)To subtract two numbersa-b
3⚹ (Multiply)To multiply two numbersa ⚹ b
4/ (Division)To divide two a number by othera/b
5% (Modulus)It will return remainder after divisiona % b
6++ (Increment)Increase the value by 1a = a + 1 or a ++
7-- (Decrement)Decrease the value by 2a = a-1 or a--

Arithmetic Operators

Arithmetic operator are used for evaluating mathematical as well as algebraic statements.

Arithmetic Operators

Relational Operators

Relational operators are used to evaluate the relationship between two operands.

Relational Operators
Sr. No.Relation OperatorDescriptionExample
1== (equal to)It checks whether the value of both operator are equal or not. If yes. return “true”If (a == b)
2! = (not equal to)It checks whether the value of both operator are equal or not. If no. return “true”If (a! = b)
3> (greater than)It checks whether the value of left operator is greater than the value of right operand. If yes. Returns “true”If (a > b)
4>= (greater than and equal to)It checks whether the value of left operator is greater than and equal to the value of right operand. If yes. Returns “true”If (a >= b)
5< Less than)It checks whether the value of left operator is less than the value of right operand. If yes. Returns “true”If (a < b)
6<= (Less than and equal to)It checks whether the value of left operator is less than and equal to the right operand. If yes. Returns “true”If (a <= b)

Logical Operators

Logical operators are used to connect two or more expression. Value of compound expression is depended only on the expression written and logical operator used.

There are three types of Logical Operators in Java

Logical Operators

Assignment Operators

They are used to assign values to variable.

Assignment Operators

Decision Making Statements

In java, there are decision making structures in which statements are executed after satisfying a particular condition.

Decision Making Statements

Decision Making Statements

Decision Making Statements

Code/Output (If … Else) Statement

Code/Output (If … Else) Statement

Code/Output (Switch () ) Statement

Code/Output (Switch () ) Statement

Looping Statements

Looping is used to execute a statement or set of statements repeatedly until an expression is returning true.

There are three types of Loops in JAVA

  • while Loop
  • do . . While Loop
  • for Loop

A Loop has three steps to perform a particular task by specific number of time:

  • Initialization
  • Condition check
  • Increment/Decrement of Counter Variable
Looping Statements
A Loop Has Three Steps to Perform
Types of Loops

Looping Statement- (While)

while is a looping control statement that repeat set of instruction contain by its block until its expression is returning ‘true’ .

SYNTAX

int i = 1; //initialization of counter variable

while (i < 10) //containing expression (condition)

{

Statement (s) ;

i ++ ; //increment of counter variable

}

Looping Statement- (While)

Looping Statement- (Do. . While)

It is used for same purpose as while loop but its functionality is somehow different.

In while, it first check the condition and then it execute the statement but do. . while works in reverse order. It first execute the statement hold by do {} block then it checks the condition. It means even if the condition returns false, the statement will execute at least once.

SYNTAX

int i = 1; //initialization of counter variable

do //containing statement to be executed

{

Statement (s) ;

i ++ ; //increment of counter variable

}

while (i < 10) ; //defining condition

Looping Statement- (Do. . While)

Looping Statement- (For)

In for loop, initialization, condition, increment/decrement are the part of one expression.

SYNTAX

//initialization, condition, increment in one expression

for (i = 1; i <= 10; i ++)

{

Statement (s)

}

Looping Statement- (For)

MCQs

Q-1. Which loop control statement executes its block at least once, even if its expression returns false?

1. for loop

2. while loop

3. do … while loop

4. switch ()

Answer: 3

Q-2. Which loop control statement executes its block at least once, even if its expression returns false?

1. for loop

2. while loop

3. do … while loop

4. switch ()

Answer: 4

Developed by: