SlideShare uma empresa Scribd logo
1 de 49
Java Programming Constructs


Pre-Assessment Questions
         1.   Which access specifier is used with the data member of a class that is
              accessible at the class level only in which it is defined?
              a.   public
              b.   private
              c.   protected
              d.   default

         •    The _______ keyword is used to define class variables and methods
              that belong to a class and not to any particular instance of the class.  
              a.   static
              b.   native
              c.   synchronized
              d.   abstract



 ©NIIT                      Java Fundamentals                  Lesson 3A / Slide 1 of 49
Java Programming Constructs


Pre-Assessment Questions (Contd.)
         1.    Consider the statements:
               Statement A: A final method cannot be overridden.
               Statement B: A final class cannot be inherited.
               Identify the correct option.
              a.   Statement A is true and statement B is false.
              b.   Statement A is false and statement B is true.
              c.   Both, statements, A and B, are true.
              d.   Both, statements, A and B, are false.




 ©NIIT                    Java Fundamentals               Lesson 3A / Slide 2 of 49
Java Programming Constructs


Pre-Assessment Questions (Contd.)
    1.   When do you get an error message as :
         Exception in thread "main" Java.lang.NoSuchMethodError:main

         •   When the class has been incorrectly declared in a program.
         •   When you try to execute a program that does not have a main()
             method.
         •   When you have given incorrect arguments to the main() method.
         •   When you save the file with a different name as the name of the
             class in which the code is written




 ©NIIT                    Java Fundamentals               Lesson 3A / Slide 3 of 49
Java Programming Constructs


Pre-Assessment Questions (Contd.)
         1.   Consider the statements:
              Statement A: An abstract class can be instantiated.
              Statement B: An abstract class cannot be inherited.
              Identify the correct option.

              •    Statement A true and statement B false.
              •    Statement A false and statement B true.
              •    Both, statements, A and B, true.
              •    Both, statements, A and B, false.




 ©NIIT                Java Fundamentals                Lesson 3A / Slide 4 of 49
Java Programming Constructs


Solutions to Pre-Assessment
Questions
    •    b. private
    •    a. static
    •    c. Both, statements, A and B are true
    •    b. When you try to execute a program that does not have a main() method
    •    d. Both, statements, A and B are false




 ©NIIT                    Java Fundamentals              Lesson 3A / Slide 5 of 49
Java Programming Constructs


Objectives
    In this lesson, you will learn to:


         •   Use conditional statements
         •   Use looping constructs
         •   Enhance methods of a class
         •   Pass arguments to methods
         •   Create nested classes

               




 ©NIIT                       Java Fundamentals   Lesson 3A / Slide 6 of 49
Java Programming Constructs


Using Conditional Statements
    •    Conditional statements allow selective execution of a set of statements
         depending on the value of expressions associated with them.
    •    Conditional statements are also known as decision-making statements.
    •    You can control the flow of a program using conditional statements.
    •    Two types of conditional statements in Java are:
           • The if-else statement
           • The switch-case construct




 ©NIIT                       Java Fundamentals                Lesson 3A / Slide 7 of 49
Java Programming Constructs


 Using Conditional Statements
(Contd.)
    •    Using the if-else Statement
          • The if-else statement:
                • Enables you to execute selectively.
                • Is followed by a boolean expression.
                • Executes the set of statements depending upon the result of the
                  boolean expression.




 ©NIIT                       Java Fundamentals               Lesson 3A / Slide 8 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
     •  Using the if-else Statement (Contd.)
          • Syntax of the if-else statement is:
               if(boolean expression)
                     {
                            statement(s)
                     }
                     else
                     {
                                      statement(s)
                     }
In the preceding syntax, if the boolean expression of the if construct evaluates to true,
the Java compiler executes the statements following the if construct else executes the
statements following the else construct.
 ©NIIT                       Java Fundamentals                 Lesson 3A / Slide 9 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
    •    Relational Operators:
          • Used to compare the values of two variables or operands and find the
             relationship between the two.
          • The relational operators are therefore called comparison operators also.




 ©NIIT                       Java Fundamentals              Lesson 3A / Slide 10 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
•    The following table lists the various relational operators (Contd.):

                   Operator               Use                        Operation


              >                    op1 > op2             Returns true if the value of the
                                                         op1 operand is greater than the
                                                         value of the op2 operand.

              <                    op1<op2               Returns true if the value of the
                                                         op1 operand is less than the
                                                         value of the op2 operand.

              >=                   op1>=op2              Returns true if the value of the
                                                         op1 operand is greater than or
                                                         equal to value of op2 operand.

    ©NIIT                      Java Fundamentals                Lesson 3A / Slide 11 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
         Operator          Use                        Operation


   <=               op1<=op2             Returns true if value of op1 operand
                                         is less than or equal to value of op2
                                         operand.
   ==               op1 ==op2            Returns true if value of op1 operand
                                         is equal to value of op2 operand.

   !=               op1 != op2           Returns true if value of op1 operand
                                         is not equal to value of op2
                                         operand.



 ©NIIT               Java Fundamentals              Lesson 3A / Slide 12 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
    •    Conditional Operators :
         • Used to combine multiple conditions in one boolean expression.
         • Are of two types :
             • Unary
             • Binary

         •   Various conditional operators are:
             • AND(&&)
             • OR(||)
             • NOT(!)


 ©NIIT                    Java Fundamentals             Lesson 3A / Slide 13 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
    •    The following table lists the various conditional operators and their
         operations:
                   Operator                       Use                 Operation


         AND (&&)                      op1 && op2             Returns true if both op1
                                                              operand and op2 operand
                                                              are true.
         OR (||)                       op1 || op2             Returns true if either op1
                                                              operand or op2operand is
                                                              true.
         NOT (!)                       ! op1                  Returns true if op1
                                                              operand is not true.

 ©NIIT                        Java Fundamentals              Lesson 3A / Slide 14 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
         •   Using the multiple if-else Statement:
               • You can replace a single if construct with multiple if-else
                  statements to write a compound if statement.
               • The multiple if-else construct allows you to check multiple boolean
                  expressions in a single compound if statement.




 ©NIIT                       Java Fundamentals               Lesson 3A / Slide 15 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
    •    Using the multiple if-else Statement (Contd.):
          • The syntax of the multiple if-else statements is:
                if (Boolean_expression_1)
                {
                  statements
                }
                else if (Boolean_expression_2)
                {
                  statements
                }
                else (Boolean_expression_3)
                {
                  statements}
 ©NIIT                       Java Fundamentals                  Lesson 3A / Slide 16 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
    •    The switch-case Construct:
         • Successively tests the value of an expression or a variable against a list
              of case labels with integer or character constants.
         • When a match is found in one of the case labels, the statements
              associated with that case label get executed.
         • The switch keyword in the switch-case construct contains the variable
              or the expression whose value is evaluated to select a case label.
         • The case keyword is followed by a constant and a colon.
         • The data type of case constant should match the switch variable.




 ©NIIT                     Java Fundamentals               Lesson 3A / Slide 17 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
    •    The switch-case Construct (Contd.)
         • The syntax of the switch-case construct is:
              switch(expression or variable name)
                   {
                   case Expr1:
                            statements;
                            break;
                   case Expr2:
                            statements;
                            break;
                   default:
                            statements; }

 ©NIIT                    Java Fundamentals              Lesson 3A / Slide 18 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
         •   The switch-case construct (Contd.)
             • Statements associated with the default keyword are executed if
                  the value of the switch variable does not match any of the case
                  constants.
             • The break statement used in the case label causes the program
                  flow to exit from the body of the switch-case construct.




 ©NIIT                    Java Fundamentals               Lesson 3A / Slide 19 of 49
Java Programming Constructs


Using Conditional Statements
(Contd.)
         •   The break Statement:
             • Causes the program flow to exit from the body of the switch
                  construct.
             • Control goes to the first statement following the end of the switch-
                  case construct.
             • If not used inside a case construct, the control passes to the next
                  case statement and the remaining statements in the switch-case
                  construct are executed.




 ©NIIT                    Java Fundamentals              Lesson 3A / Slide 20 of 49
Java Programming Constructs


Demonstration-Scholarship Criteria of
Certified Careers Institute
    •    Problem Statement
          • The Certified Careers institute is granting scholarship to those
              students who passed in each subject and scored more than
              175 marks out of 200 marks. Steve is a Java developer in the
              institute. He is assigned the task for developing the Java
              application that enables the user to select the eligible
              students. Before creating the application for all the students
              he is testing the application for a specific student David. David
              has passed each subject and scored 180 marks. Help Steve to
              code the application for David.




 ©NIIT                   Java Fundamentals             Lesson 3A / Slide 21 of 49
Java Programming Constructs


Demonstration-Scholarship Criteria of
Certified Careers Institute(Contd.)
    •    Solution
          • To solve the problem, Steve can use the if-else statement,
               conditional operators, and relational operators to code the
               selection procedure. A student is applicable for scholarship
               only if the student passes in each subject and total marks
               scored are more than 175 out of 200. To solve the given
               problem, perform the following tasks :
                1. Code the application.
                2. Compile and execute the application.




 ©NIIT                   Java Fundamentals            Lesson 3A / Slide 22 of 49
Java Programming Constructs


Using Looping Statements
    •    A looping statement causes a section of program to be executed a certain
         number of times.
    •    The repetition continues while the condition set in the looping statement
         remains true.
    •    When the condition becomes false, the loop ends and the control is passed to
         the statement following the loop.




 ©NIIT                     Java Fundamentals              Lesson 3A / Slide 23 of 49
Java Programming Constructs


Using Looping Statements (Contd.)
    •    The for Loop:
         • Is a looping statement iterating for a fixed number of times.
         • Consists of the for keyword followed by parentheses containing three
              expressions, each separated by a semicolon.
         • The three expressions in the for loop are:
              • Initialization expression
              • Test expression
              • IterationExpr expression
         • Is used when the number of iterations is known in advance.
         • For example, it can be used to determine the square of each of the first
              ten natural numbers.




 ©NIIT                     Java Fundamentals              Lesson 3A / Slide 24 of 49
Java Programming Constructs


Using Looping Statements(Contd.)
    •    The for Loop (Contd.)
         • The syntax of the for loop is:
              for(InitializationExpr; TestExpr; IterationExpr)
                    {
                               statement_1
                               statement_2
                               …
                    }
         • In the preceding syntax, the initialization expression is executed only
              once, when the control is passed to the for loop for the first time. The
              initialization expression gives the loop variable an initial value.
         • The test expression is executed each time the control passes to the
              beginning of the loop. If true, the body of the loop is executed,
              otherwise not.
         • The IterationExpr expression is always executed when the control
              returns to the beginning of the loop in each loop iteration.
 ©NIIT                      Java Fundamentals                Lesson 3A / Slide 25 of 49
Java Programming Constructs


Using Looping Statements(Contd.)
    •    The for Loop (Contd.)
         • The following figure shows the use of for loop:




 ©NIIT                    Java Fundamentals             Lesson 3A / Slide 26 of 49
Java Programming Constructs


Using Looping Statements(Contd.)
    •    The while Loop:
         • Executes a statement or a block of statements as long as the evaluating
              condition remains true.
         • The evaluating condition has to be a boolean expression and must
              return a boolean value that can be true or false.
         • The syntax of the while loop is:
              while(Bool_Expr)
                    {
                           statements; //executed as long as Bool_Expr is
                    true
                    }
              In the preceding syntax, the statements in the while loop are executed
              as long as the Bool_Expr condition is true. When the condition returns
              false, the statement immediately following the while block is executed.


 ©NIIT                     Java Fundamentals               Lesson 3A / Slide 27 of 49
Java Programming Constructs


Using Looping Statements(Contd.)
    •    The do-while Loop:
         • Used when the body of the loop needs to be executed at least once.
         • The do-while construct places the test condition at the end of the loop.
         • The do keyword marks the beginning of the loop and braces form the
              body of the loop.
         • The while statement provides the test condition.
         • The test condition checks whether the loop will execute again or not.
         • The syntax of the do-while loop is:
                   do
                  {
                             statements;
                  }
                  while(Bool_Expr);



 ©NIIT                     Java Fundamentals             Lesson 3A / Slide 28 of 49
Java Programming Constructs


Using Looping Statements(Contd.)
    •    The continue statement:
         • In the while and do-while loops:
              • The continue statement returns the control to the conditional
                  expression that controls the loop.
              • The control skips any statement following the continue statement
                  in the loop body.
         • In the for loop:
              • control goes to the re-initialization expression first and then to the
                  conditional expression.




 ©NIIT                     Java Fundamentals                Lesson 3A / Slide 29 of 49
Java Programming Constructs


Enhancing Methods of a Class
    •    Methods are used to access the variables that are defined in a class.
    •    A method is an interface to a class.
    •    Parameterized methods need some extra information for its execution.
    •    The extra information is passed to the method by using arguments.
    •    Arguments are also known as parameters of the methods.




 ©NIIT                    Java Fundamentals               Lesson 3A / Slide 30 of 49
Java Programming Constructs


Enhancing Methods of a Class
(Contd.)
    •    Defining Parameterized Methods:
          • Parameterized methods use parameters to process data and generate an
             output.
          • The output of a parameterized method is not static and depends on the
             value of the parameters passed.
          • Parameters in a parameterized method allow the method to be
             generalized.




 ©NIIT                      Java Fundamentals             Lesson 3A / Slide 31 of 49
Java Programming Constructs


Enhancing Methods of a Class
(Contd.)
•      Defining a Method that Returns a Value:
       • You can define a method that can return a value instead of just computing
            results and displaying them.
       • The return type of a method is specified to the Java compiler in the method
            declaration statement.
       • The return type of a method can be of any primitive data type or abstract
            data type.
       • The value is returned from a method using the return keyword followed by
            the value to be returned.
       • The methods that do not return any value have return type void.



    ©NIIT                    Java Fundamentals             Lesson 3A / Slide 32 of 49
Java Programming Constructs


Enhancing Methods of a Class
(Contd.)
    •    Defining a Method that Returns a Value
         • The syntax of any method that returns a value is:
              <return_type> method_name(parameter_list)
                   {
                             statements;
                             return value;
                   }
              In the preceding syntax, the return type specifies the type of data
              returned by the method and the return statement specifies the value
              returned.




 ©NIIT                     Java Fundamentals              Lesson 3A / Slide 33 of 49
Java Programming Constructs


Enhancing Methods of a Class
(Contd.)
    •    Overloading Methods
         • Method overloading is defined as the function that enables you to define
              two or more methods with the same name but with different signatures
              within the class.
         • The methods that share the same name, but have different signatures
              are called overloaded methods.
         • The signature of a method consists of:
              • The name of the method.
              • The number of arguments it takes.
              • The data type of the arguments.
              • The order of the arguments.


 ©NIIT                     Java Fundamentals             Lesson 3A / Slide 34 of 49
Java Programming Constructs


Enhancing Methods of a Class
(Contd.)
    •    Overloading Methods (Contd.)
         • When an overloaded method is invoked, the Java compiler uses
              the type of arguments or the number of arguments to determine which
              copy of overloaded method to invoke.
         • Overloading methods must differ in the type or the number of
              parameters.
         • The return types of overloaded methods can be different.
         • Overloaded methods are used when you need several methods that
              perform closely related tasks.
         • Method overloading is a way to implement polymorphism in Java.



 ©NIIT                    Java Fundamentals              Lesson 3A / Slide 35 of 49
Java Programming Constructs


Enhancing Methods of a Class
(Contd.)
    •    Overloading Methods (Contd.)
         • The following code snippet shows an overloaded multiply() method:
              public void multiply(int a, int b)     //Multiply two integers
              public void multiply(float a, float b)      //Multiply two
              floats
              public void multiply(double a, double b)      //Multiply two
              doubles




 ©NIIT                   Java Fundamentals           Lesson 3A / Slide 36 of 49
Java Programming Constructs


Passing Arguments to a Method
  •   Arguments can be passed to a method using two ways:
      • Call-by-value: Copies the value of the actual parameters to the formal
           parameters. Therefore, the changes made to the formal parameters have no
           effect on the actual parameters.
      • Call-by-reference: Passes the reference and not the value of the actual
           parameters to the formal parameters in a method. Therefore, the changes
           made to the formal parameters affect the actual parameters.
  •   You can also pass arguments to the main() method at the run-time of a program.




  ©NIIT                    Java Fundamentals             Lesson 3A / Slide 37 of 49
Java Programming Constructs


Passing Arguments to a Method
(Contd.)
    •    Passing Arguments by Value:
         • In Java, when arguments of primitive data type, such as int and float
              are passed to a method then they are passed by value. The following
              figure shows the concept of passing arguments by value:




         •   When arguments are passed by value, a copy of the actual argument is
             passed to the formal arguments of the called method, which is
             maintained at a separate memory location. Therefore, when the called
             method changes the value of the argument, the change is not reflected
             in the actual argument.

 ©NIIT                     Java Fundamentals              Lesson 3A / Slide 38 of 49
Java Programming Constructs


Passing Arguments to a Method
(Contd.)
    •    Passing Arguments by Reference:
         • Passes a reference to an argument to the parameter of a method.
         • In Java, the objects of abstract data type are passed by reference.
         • When arguments are passed to the method by reference then any
              change made to the formal argument by the called method is also
              reflected in the actual argument. The following figure shows the concept
              of passing arguments by reference:




         •    The argument passed by reference to the formal parameter has the
              same memory location as that of the actual parameter.

 ©NIIT                     Java Fundamentals               Lesson 3A / Slide 39 of 49
Java Programming Constructs

Passing Arguments to a Method 
(Contd.)
•      Passing Arguments at the Command Line:
            • Command-line arguments are used to provide information that a
                 program needs at startup.
            • The main() method in Java is the first method to be executed, therefore
                 the command-line arguments are passed to the main() method.
            • The main() method stores the command-line arguments in the String
                 array object.
            • In Java, the syntax for command-line argument is:
                 public static void main(String args[])
                 {    //statements
                 } //End of main() method
                 In the preceding syntax, each of the elements in the array named
                 args[] is a reference to the command-line arguments each of which is a
                 String object.

    ©NIIT                     Java Fundamentals              Lesson 3A / Slide 40 of 49
Java Programming Constructs


Creating Nested Classes
•      Nested Classes:
       • A nested class is a class defined as a member of another class.
       • The scope of nested class is bounded by the scope of its enclosing class.
       • The nested class has access to the members of its enclosing class including
            private members.
       • The enclosing class does not have any access to the members of the nested
            class.
       • The nested classes are of two types:
            • Static: A nested class declared as static is called the static nested class.
            • Inner: A non-static nested class is called the inner class.




    ©NIIT                      Java Fundamentals               Lesson 3A / Slide 41 of 49
Java Programming Constructs


Creating Nested Classes (Contd.)
•      Nested Classes (Contd.):
       • Static Nested Class
            • A static nested class cannot access the members of its enclosing class
                 directly because it is declared as static.
            • Therefore, it has to access the members of its enclosing class through
                 an object of enclosing class.
            • The following code snippet shows the declaration of a static nested
                 class:
                 class EnclosingClass{
                      //statements
                      static AstaticNestedClass
                      {
                                //statements
                      }
                 }

    ©NIIT                    Java Fundamentals              Lesson 3A / Slide 42 of 49
Java Programming Constructs


Creating Nested Classes (Contd.)
•      Nested Classes (Contd.)
       • Inner Class:
            • An inner class is a non-static nested class, whose instance exists inside
                 the instance of its enclosing class.
            • An inner class can directly access the variables and methods of its
                 enclosing class.
            • The following figure shows a nested class:




    ©NIIT                     Java Fundamentals               Lesson 3A / Slide 43 of 49
Java Programming Constructs


Demonstration-Selection Criteria of
Fun Seasons Corp.
    •    Problem Statement:
         • The Fun Seasons Corp. departmental store is conducting
             interviews for the post of salesman. Various applicants are
             applying for the required post by filling an application form. The
             departmental store performs the selection process of the
             applicants and the selected applicants are chosen as candidates.
             John David, the programmer hired by the departmental store is
             assigned the task of developing the Java application. He needs
             to create an application that accepts the applicant ID, in the
             “A#####” format, and educational status, such as Graduate
             and PostGraduate, as command-line arguments. Help John David
             to write a code for the selection procedure.



 ©NIIT                    Java Fundamentals            Lesson 3A / Slide 44 of 49
Java Programming Constructs


Demonstration-Selection Criteria of
Fun Seasons Corp.(Contd.)
    •    Solution
         • To solve the problem, John David needs to use nested class.
             John David can create an Applicant class and a Candidate class
             as nested class of the Applicant class. The Candidate class is the
             inner class and can access the members of the Applicant class.
             The Candidate class is having the selection procedure of an
             applicant and displays the status of an applicant. The selection
             procedure is based on the age of the applicant. If the age of an
             applicant is below 25 years, the applicant is a candidate. To
             solve the given problem, perform the following tasks:
             1. Create the code for the application.
             2. Compile and execute the application.



 ©NIIT                    Java Fundamentals            Lesson 3A / Slide 45 of 49
Java Programming Constructs


Summary
    In this lesson, you learned that:
    • Conditional statements are used to allow selective execution of statements.
         The conditional statements in Java are:
         • if-else
         • switch-case
    • Looping statements are used when you want a section of a program to be
         repeated a certain number of times. Java offers the following looping
         statements:
         • for
         • while
         • do-while




 ©NIIT                    Java Fundamentals              Lesson 3A / Slide 46 of 49
Java Programming Constructs


Summary (Contd.)
    •    The for loop checks the validity of a condition first and then executes the
         body of the loop.
    •    The while loop checks for a condition before executing the body of the loop.
    •    In the do-while loop, first the body of the loop is executed and then the
         conditional expression is evaluated.
    •    The break and continue statements are used to control the program flow
         within a loop.
    •    Relational operator is used to compare the values of two operands, such as
         >=, ==, !=, <= >, and <.
    •    The three conditional operators are &&, ||, and !.
    •    AND (&&) operator returns true if both the operands are true. OR (||)
         returns true if either of the operands are true. NOT (!) operator is a unary
         operator, which represents the false condition of an operand.




 ©NIIT                     Java Fundamentals                Lesson 3A / Slide 47 of 49
Java Programming Constructs


Summary (Contd.)
    •    You can generalize a method by passing parameters and arguments to the
         method.
    •    The data type of the data returned by a method and the variable that
         receives the data must be compatible with the return type specified by the
         method.
    •    Method overloading means having more than one method with the same
         name. The Java compiler resolves the method to be invoked using the
         signature of the method.
    •    The signature of a method consists of:
         • The method name.
         • The number of arguments passed to the method.
         • The data types of the arguments passed.
         • The order of the arguments passed.


 ©NIIT                     Java Fundamentals               Lesson 3A / Slide 48 of 49
Java Programming Constructs


Summary (Contd.)
    •    Methods can take arguments of different data types. Variables of the
         primitive data type are passed by value, while variables of the abstract data
         type are passed by reference.
    •    Nested class is a class that is defined as a member of another class. Nested
         classes are of two types:
         • Static nested class
         • Non-static nested class




 ©NIIT                      Java Fundamentals               Lesson 3A / Slide 49 of 49

Mais conteúdo relacionado

Mais procurados

OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Designİbrahim Kürce
 
8 most expected java interview questions
8 most expected java interview questions8 most expected java interview questions
8 most expected java interview questionsPoonam Kherde
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview QuestionsEhtisham Ali
 
Java Concurrency Starter Kit
Java Concurrency Starter KitJava Concurrency Starter Kit
Java Concurrency Starter KitMark Papis
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1Sherihan Anver
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007
Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007
Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007Arun Kumar
 
Evolution of Patterns
Evolution of PatternsEvolution of Patterns
Evolution of PatternsChris Eargle
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreadingKuntal Bhowmick
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interviewKuntal Bhowmick
 
Strategy and Template Pattern
Strategy and Template PatternStrategy and Template Pattern
Strategy and Template PatternJonathan Simon
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Raffi Khatchadourian
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2Sherihan Anver
 
Javainterview
JavainterviewJavainterview
JavainterviewAmarjit03
 

Mais procurados (20)

OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
 
Java
JavaJava
Java
 
Java interview question
Java interview questionJava interview question
Java interview question
 
8 most expected java interview questions
8 most expected java interview questions8 most expected java interview questions
8 most expected java interview questions
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview Questions
 
Java Concurrency Starter Kit
Java Concurrency Starter KitJava Concurrency Starter Kit
Java Concurrency Starter Kit
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007
Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007
Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007
 
Volatile keyword
Volatile keywordVolatile keyword
Volatile keyword
 
Evolution of Patterns
Evolution of PatternsEvolution of Patterns
Evolution of Patterns
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 
oop Lecture19
oop Lecture19oop Lecture19
oop Lecture19
 
Strategy and Template Pattern
Strategy and Template PatternStrategy and Template Pattern
Strategy and Template Pattern
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
Javainterview
JavainterviewJavainterview
Javainterview
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Destaque

Semi Detailed Lesson Plan in Programming Languages
Semi Detailed Lesson Plan in Programming LanguagesSemi Detailed Lesson Plan in Programming Languages
Semi Detailed Lesson Plan in Programming LanguagesManila Central University
 
Empowerment technologies
Empowerment technologiesEmpowerment technologies
Empowerment technologiesDeped
 
Dll empowerment technologies
Dll empowerment technologiesDll empowerment technologies
Dll empowerment technologieswacasr12
 
Empowerment Technology
Empowerment TechnologyEmpowerment Technology
Empowerment TechnologyReygie Fabro
 
Empowerment through technology
Empowerment through technologyEmpowerment through technology
Empowerment through technologySteven Parker
 

Destaque (9)

Infographics
InfographicsInfographics
Infographics
 
Semi Detailed Lesson Plan in Programming Languages
Semi Detailed Lesson Plan in Programming LanguagesSemi Detailed Lesson Plan in Programming Languages
Semi Detailed Lesson Plan in Programming Languages
 
Empowerment technologies
Empowerment technologiesEmpowerment technologies
Empowerment technologies
 
Dll empowerment technologies
Dll empowerment technologiesDll empowerment technologies
Dll empowerment technologies
 
Empowerment Technology
Empowerment TechnologyEmpowerment Technology
Empowerment Technology
 
Lesson 1 Empowerment Technology
Lesson 1 Empowerment TechnologyLesson 1 Empowerment Technology
Lesson 1 Empowerment Technology
 
Empowerment through technology
Empowerment through technologyEmpowerment through technology
Empowerment through technology
 
Sample of Semi Detailed Lesson Plan
Sample of Semi Detailed Lesson PlanSample of Semi Detailed Lesson Plan
Sample of Semi Detailed Lesson Plan
 
EMPOWERMENT POWERPOINT
EMPOWERMENT POWERPOINT EMPOWERMENT POWERPOINT
EMPOWERMENT POWERPOINT
 

Semelhante a Dacj 1-3 a

Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Syed Farjad Zia Zaidi
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part IEugene Lazutkin
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.pptAyushDut
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Kernel Training
 
Cso gaddis java_chapter4
Cso gaddis java_chapter4Cso gaddis java_chapter4
Cso gaddis java_chapter4mlrbrown
 
Day 2 Compose Camp.pptx
Day 2 Compose Camp.pptxDay 2 Compose Camp.pptx
Day 2 Compose Camp.pptxShayantaniKar
 
JAVA 8 Parallel Stream
JAVA 8 Parallel StreamJAVA 8 Parallel Stream
JAVA 8 Parallel StreamTengwen Wang
 
Java session05
Java session05Java session05
Java session05Niit Care
 
Java class 1
Java class 1Java class 1
Java class 1Edureka!
 
Visual COBOL Development for Unix and Java
Visual COBOL Development for Unix and JavaVisual COBOL Development for Unix and Java
Visual COBOL Development for Unix and JavaMicro Focus
 
Introduction To Python.pptx
Introduction To Python.pptxIntroduction To Python.pptx
Introduction To Python.pptxAnum Zehra
 
C programming session 02
C programming session 02C programming session 02
C programming session 02AjayBahoriya
 

Semelhante a Dacj 1-3 a (20)

Embedded c
Embedded cEmbedded c
Embedded c
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Decisions
DecisionsDecisions
Decisions
 
Decisions
DecisionsDecisions
Decisions
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.ppt
 
Decisions
DecisionsDecisions
Decisions
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Cso gaddis java_chapter4
Cso gaddis java_chapter4Cso gaddis java_chapter4
Cso gaddis java_chapter4
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
Decision control
Decision controlDecision control
Decision control
 
Day 2 Compose Camp.pptx
Day 2 Compose Camp.pptxDay 2 Compose Camp.pptx
Day 2 Compose Camp.pptx
 
Dacj 1-1 b
Dacj 1-1 bDacj 1-1 b
Dacj 1-1 b
 
JAVA 8 Parallel Stream
JAVA 8 Parallel StreamJAVA 8 Parallel Stream
JAVA 8 Parallel Stream
 
Java session05
Java session05Java session05
Java session05
 
Java class 1
Java class 1Java class 1
Java class 1
 
Visual COBOL Development for Unix and Java
Visual COBOL Development for Unix and JavaVisual COBOL Development for Unix and Java
Visual COBOL Development for Unix and Java
 
Unit II.pptx
Unit II.pptxUnit II.pptx
Unit II.pptx
 
Introduction To Python.pptx
Introduction To Python.pptxIntroduction To Python.pptx
Introduction To Python.pptx
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 

Mais de Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-2 c
Dacj 1-2 cDacj 1-2 c
Dacj 1-2 c
 

Último

So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 

Último (20)

So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 

Dacj 1-3 a

  • 1. Java Programming Constructs Pre-Assessment Questions 1. Which access specifier is used with the data member of a class that is accessible at the class level only in which it is defined? a. public b. private c. protected d. default • The _______ keyword is used to define class variables and methods that belong to a class and not to any particular instance of the class.   a. static b. native c. synchronized d. abstract ©NIIT Java Fundamentals Lesson 3A / Slide 1 of 49
  • 2. Java Programming Constructs Pre-Assessment Questions (Contd.) 1. Consider the statements: Statement A: A final method cannot be overridden. Statement B: A final class cannot be inherited. Identify the correct option. a. Statement A is true and statement B is false. b. Statement A is false and statement B is true. c. Both, statements, A and B, are true. d. Both, statements, A and B, are false. ©NIIT Java Fundamentals Lesson 3A / Slide 2 of 49
  • 3. Java Programming Constructs Pre-Assessment Questions (Contd.) 1. When do you get an error message as : Exception in thread "main" Java.lang.NoSuchMethodError:main • When the class has been incorrectly declared in a program. • When you try to execute a program that does not have a main() method. • When you have given incorrect arguments to the main() method. • When you save the file with a different name as the name of the class in which the code is written ©NIIT Java Fundamentals Lesson 3A / Slide 3 of 49
  • 4. Java Programming Constructs Pre-Assessment Questions (Contd.) 1. Consider the statements: Statement A: An abstract class can be instantiated. Statement B: An abstract class cannot be inherited. Identify the correct option. • Statement A true and statement B false. • Statement A false and statement B true. • Both, statements, A and B, true. • Both, statements, A and B, false. ©NIIT Java Fundamentals Lesson 3A / Slide 4 of 49
  • 5. Java Programming Constructs Solutions to Pre-Assessment Questions • b. private • a. static • c. Both, statements, A and B are true • b. When you try to execute a program that does not have a main() method • d. Both, statements, A and B are false ©NIIT Java Fundamentals Lesson 3A / Slide 5 of 49
  • 6. Java Programming Constructs Objectives In this lesson, you will learn to: • Use conditional statements • Use looping constructs • Enhance methods of a class • Pass arguments to methods • Create nested classes    ©NIIT Java Fundamentals Lesson 3A / Slide 6 of 49
  • 7. Java Programming Constructs Using Conditional Statements • Conditional statements allow selective execution of a set of statements depending on the value of expressions associated with them. • Conditional statements are also known as decision-making statements. • You can control the flow of a program using conditional statements. • Two types of conditional statements in Java are: • The if-else statement • The switch-case construct ©NIIT Java Fundamentals Lesson 3A / Slide 7 of 49
  • 8. Java Programming Constructs Using Conditional Statements (Contd.) • Using the if-else Statement • The if-else statement: • Enables you to execute selectively. • Is followed by a boolean expression. • Executes the set of statements depending upon the result of the boolean expression. ©NIIT Java Fundamentals Lesson 3A / Slide 8 of 49
  • 9. Java Programming Constructs Using Conditional Statements (Contd.) • Using the if-else Statement (Contd.) • Syntax of the if-else statement is: if(boolean expression) { statement(s) } else { statement(s) } In the preceding syntax, if the boolean expression of the if construct evaluates to true, the Java compiler executes the statements following the if construct else executes the statements following the else construct. ©NIIT Java Fundamentals Lesson 3A / Slide 9 of 49
  • 10. Java Programming Constructs Using Conditional Statements (Contd.) • Relational Operators: • Used to compare the values of two variables or operands and find the relationship between the two. • The relational operators are therefore called comparison operators also. ©NIIT Java Fundamentals Lesson 3A / Slide 10 of 49
  • 11. Java Programming Constructs Using Conditional Statements (Contd.) • The following table lists the various relational operators (Contd.): Operator Use Operation > op1 > op2 Returns true if the value of the op1 operand is greater than the value of the op2 operand. < op1<op2 Returns true if the value of the op1 operand is less than the value of the op2 operand. >= op1>=op2 Returns true if the value of the op1 operand is greater than or equal to value of op2 operand. ©NIIT Java Fundamentals Lesson 3A / Slide 11 of 49
  • 12. Java Programming Constructs Using Conditional Statements (Contd.) Operator Use Operation <= op1<=op2 Returns true if value of op1 operand is less than or equal to value of op2 operand. == op1 ==op2 Returns true if value of op1 operand is equal to value of op2 operand. != op1 != op2 Returns true if value of op1 operand is not equal to value of op2 operand. ©NIIT Java Fundamentals Lesson 3A / Slide 12 of 49
  • 13. Java Programming Constructs Using Conditional Statements (Contd.) • Conditional Operators : • Used to combine multiple conditions in one boolean expression. • Are of two types : • Unary • Binary • Various conditional operators are: • AND(&&) • OR(||) • NOT(!) ©NIIT Java Fundamentals Lesson 3A / Slide 13 of 49
  • 14. Java Programming Constructs Using Conditional Statements (Contd.) • The following table lists the various conditional operators and their operations: Operator Use Operation AND (&&) op1 && op2 Returns true if both op1 operand and op2 operand are true. OR (||) op1 || op2 Returns true if either op1 operand or op2operand is true. NOT (!) ! op1 Returns true if op1 operand is not true. ©NIIT Java Fundamentals Lesson 3A / Slide 14 of 49
  • 15. Java Programming Constructs Using Conditional Statements (Contd.) • Using the multiple if-else Statement: • You can replace a single if construct with multiple if-else statements to write a compound if statement. • The multiple if-else construct allows you to check multiple boolean expressions in a single compound if statement. ©NIIT Java Fundamentals Lesson 3A / Slide 15 of 49
  • 16. Java Programming Constructs Using Conditional Statements (Contd.) • Using the multiple if-else Statement (Contd.): • The syntax of the multiple if-else statements is: if (Boolean_expression_1) { statements } else if (Boolean_expression_2) { statements } else (Boolean_expression_3) { statements} ©NIIT Java Fundamentals Lesson 3A / Slide 16 of 49
  • 17. Java Programming Constructs Using Conditional Statements (Contd.) • The switch-case Construct: • Successively tests the value of an expression or a variable against a list of case labels with integer or character constants. • When a match is found in one of the case labels, the statements associated with that case label get executed. • The switch keyword in the switch-case construct contains the variable or the expression whose value is evaluated to select a case label. • The case keyword is followed by a constant and a colon. • The data type of case constant should match the switch variable. ©NIIT Java Fundamentals Lesson 3A / Slide 17 of 49
  • 18. Java Programming Constructs Using Conditional Statements (Contd.) • The switch-case Construct (Contd.) • The syntax of the switch-case construct is: switch(expression or variable name) { case Expr1: statements; break; case Expr2: statements; break; default: statements; } ©NIIT Java Fundamentals Lesson 3A / Slide 18 of 49
  • 19. Java Programming Constructs Using Conditional Statements (Contd.) • The switch-case construct (Contd.) • Statements associated with the default keyword are executed if the value of the switch variable does not match any of the case constants. • The break statement used in the case label causes the program flow to exit from the body of the switch-case construct. ©NIIT Java Fundamentals Lesson 3A / Slide 19 of 49
  • 20. Java Programming Constructs Using Conditional Statements (Contd.) • The break Statement: • Causes the program flow to exit from the body of the switch construct. • Control goes to the first statement following the end of the switch- case construct. • If not used inside a case construct, the control passes to the next case statement and the remaining statements in the switch-case construct are executed. ©NIIT Java Fundamentals Lesson 3A / Slide 20 of 49
  • 21. Java Programming Constructs Demonstration-Scholarship Criteria of Certified Careers Institute • Problem Statement • The Certified Careers institute is granting scholarship to those students who passed in each subject and scored more than 175 marks out of 200 marks. Steve is a Java developer in the institute. He is assigned the task for developing the Java application that enables the user to select the eligible students. Before creating the application for all the students he is testing the application for a specific student David. David has passed each subject and scored 180 marks. Help Steve to code the application for David. ©NIIT Java Fundamentals Lesson 3A / Slide 21 of 49
  • 22. Java Programming Constructs Demonstration-Scholarship Criteria of Certified Careers Institute(Contd.) • Solution • To solve the problem, Steve can use the if-else statement, conditional operators, and relational operators to code the selection procedure. A student is applicable for scholarship only if the student passes in each subject and total marks scored are more than 175 out of 200. To solve the given problem, perform the following tasks : 1. Code the application. 2. Compile and execute the application. ©NIIT Java Fundamentals Lesson 3A / Slide 22 of 49
  • 23. Java Programming Constructs Using Looping Statements • A looping statement causes a section of program to be executed a certain number of times. • The repetition continues while the condition set in the looping statement remains true. • When the condition becomes false, the loop ends and the control is passed to the statement following the loop. ©NIIT Java Fundamentals Lesson 3A / Slide 23 of 49
  • 24. Java Programming Constructs Using Looping Statements (Contd.) • The for Loop: • Is a looping statement iterating for a fixed number of times. • Consists of the for keyword followed by parentheses containing three expressions, each separated by a semicolon. • The three expressions in the for loop are: • Initialization expression • Test expression • IterationExpr expression • Is used when the number of iterations is known in advance. • For example, it can be used to determine the square of each of the first ten natural numbers. ©NIIT Java Fundamentals Lesson 3A / Slide 24 of 49
  • 25. Java Programming Constructs Using Looping Statements(Contd.) • The for Loop (Contd.) • The syntax of the for loop is: for(InitializationExpr; TestExpr; IterationExpr) { statement_1 statement_2 … } • In the preceding syntax, the initialization expression is executed only once, when the control is passed to the for loop for the first time. The initialization expression gives the loop variable an initial value. • The test expression is executed each time the control passes to the beginning of the loop. If true, the body of the loop is executed, otherwise not. • The IterationExpr expression is always executed when the control returns to the beginning of the loop in each loop iteration. ©NIIT Java Fundamentals Lesson 3A / Slide 25 of 49
  • 26. Java Programming Constructs Using Looping Statements(Contd.) • The for Loop (Contd.) • The following figure shows the use of for loop: ©NIIT Java Fundamentals Lesson 3A / Slide 26 of 49
  • 27. Java Programming Constructs Using Looping Statements(Contd.) • The while Loop: • Executes a statement or a block of statements as long as the evaluating condition remains true. • The evaluating condition has to be a boolean expression and must return a boolean value that can be true or false. • The syntax of the while loop is: while(Bool_Expr) { statements; //executed as long as Bool_Expr is true } In the preceding syntax, the statements in the while loop are executed as long as the Bool_Expr condition is true. When the condition returns false, the statement immediately following the while block is executed. ©NIIT Java Fundamentals Lesson 3A / Slide 27 of 49
  • 28. Java Programming Constructs Using Looping Statements(Contd.) • The do-while Loop: • Used when the body of the loop needs to be executed at least once. • The do-while construct places the test condition at the end of the loop. • The do keyword marks the beginning of the loop and braces form the body of the loop. • The while statement provides the test condition. • The test condition checks whether the loop will execute again or not. • The syntax of the do-while loop is: do { statements; } while(Bool_Expr); ©NIIT Java Fundamentals Lesson 3A / Slide 28 of 49
  • 29. Java Programming Constructs Using Looping Statements(Contd.) • The continue statement: • In the while and do-while loops: • The continue statement returns the control to the conditional expression that controls the loop. • The control skips any statement following the continue statement in the loop body. • In the for loop: • control goes to the re-initialization expression first and then to the conditional expression. ©NIIT Java Fundamentals Lesson 3A / Slide 29 of 49
  • 30. Java Programming Constructs Enhancing Methods of a Class • Methods are used to access the variables that are defined in a class. • A method is an interface to a class. • Parameterized methods need some extra information for its execution. • The extra information is passed to the method by using arguments. • Arguments are also known as parameters of the methods. ©NIIT Java Fundamentals Lesson 3A / Slide 30 of 49
  • 31. Java Programming Constructs Enhancing Methods of a Class (Contd.) • Defining Parameterized Methods: • Parameterized methods use parameters to process data and generate an output. • The output of a parameterized method is not static and depends on the value of the parameters passed. • Parameters in a parameterized method allow the method to be generalized. ©NIIT Java Fundamentals Lesson 3A / Slide 31 of 49
  • 32. Java Programming Constructs Enhancing Methods of a Class (Contd.) • Defining a Method that Returns a Value: • You can define a method that can return a value instead of just computing results and displaying them. • The return type of a method is specified to the Java compiler in the method declaration statement. • The return type of a method can be of any primitive data type or abstract data type. • The value is returned from a method using the return keyword followed by the value to be returned. • The methods that do not return any value have return type void. ©NIIT Java Fundamentals Lesson 3A / Slide 32 of 49
  • 33. Java Programming Constructs Enhancing Methods of a Class (Contd.) • Defining a Method that Returns a Value • The syntax of any method that returns a value is: <return_type> method_name(parameter_list) { statements; return value; } In the preceding syntax, the return type specifies the type of data returned by the method and the return statement specifies the value returned. ©NIIT Java Fundamentals Lesson 3A / Slide 33 of 49
  • 34. Java Programming Constructs Enhancing Methods of a Class (Contd.) • Overloading Methods • Method overloading is defined as the function that enables you to define two or more methods with the same name but with different signatures within the class. • The methods that share the same name, but have different signatures are called overloaded methods. • The signature of a method consists of: • The name of the method. • The number of arguments it takes. • The data type of the arguments. • The order of the arguments. ©NIIT Java Fundamentals Lesson 3A / Slide 34 of 49
  • 35. Java Programming Constructs Enhancing Methods of a Class (Contd.) • Overloading Methods (Contd.) • When an overloaded method is invoked, the Java compiler uses the type of arguments or the number of arguments to determine which copy of overloaded method to invoke. • Overloading methods must differ in the type or the number of parameters. • The return types of overloaded methods can be different. • Overloaded methods are used when you need several methods that perform closely related tasks. • Method overloading is a way to implement polymorphism in Java. ©NIIT Java Fundamentals Lesson 3A / Slide 35 of 49
  • 36. Java Programming Constructs Enhancing Methods of a Class (Contd.) • Overloading Methods (Contd.) • The following code snippet shows an overloaded multiply() method: public void multiply(int a, int b) //Multiply two integers public void multiply(float a, float b) //Multiply two floats public void multiply(double a, double b) //Multiply two doubles ©NIIT Java Fundamentals Lesson 3A / Slide 36 of 49
  • 37. Java Programming Constructs Passing Arguments to a Method • Arguments can be passed to a method using two ways: • Call-by-value: Copies the value of the actual parameters to the formal parameters. Therefore, the changes made to the formal parameters have no effect on the actual parameters. • Call-by-reference: Passes the reference and not the value of the actual parameters to the formal parameters in a method. Therefore, the changes made to the formal parameters affect the actual parameters. • You can also pass arguments to the main() method at the run-time of a program. ©NIIT Java Fundamentals Lesson 3A / Slide 37 of 49
  • 38. Java Programming Constructs Passing Arguments to a Method (Contd.) • Passing Arguments by Value: • In Java, when arguments of primitive data type, such as int and float are passed to a method then they are passed by value. The following figure shows the concept of passing arguments by value: • When arguments are passed by value, a copy of the actual argument is passed to the formal arguments of the called method, which is maintained at a separate memory location. Therefore, when the called method changes the value of the argument, the change is not reflected in the actual argument. ©NIIT Java Fundamentals Lesson 3A / Slide 38 of 49
  • 39. Java Programming Constructs Passing Arguments to a Method (Contd.) • Passing Arguments by Reference: • Passes a reference to an argument to the parameter of a method. • In Java, the objects of abstract data type are passed by reference. • When arguments are passed to the method by reference then any change made to the formal argument by the called method is also reflected in the actual argument. The following figure shows the concept of passing arguments by reference: • The argument passed by reference to the formal parameter has the same memory location as that of the actual parameter. ©NIIT Java Fundamentals Lesson 3A / Slide 39 of 49
  • 40. Java Programming Constructs Passing Arguments to a Method  (Contd.) • Passing Arguments at the Command Line: • Command-line arguments are used to provide information that a program needs at startup. • The main() method in Java is the first method to be executed, therefore the command-line arguments are passed to the main() method. • The main() method stores the command-line arguments in the String array object. • In Java, the syntax for command-line argument is: public static void main(String args[]) { //statements } //End of main() method In the preceding syntax, each of the elements in the array named args[] is a reference to the command-line arguments each of which is a String object. ©NIIT Java Fundamentals Lesson 3A / Slide 40 of 49
  • 41. Java Programming Constructs Creating Nested Classes • Nested Classes: • A nested class is a class defined as a member of another class. • The scope of nested class is bounded by the scope of its enclosing class. • The nested class has access to the members of its enclosing class including private members. • The enclosing class does not have any access to the members of the nested class. • The nested classes are of two types: • Static: A nested class declared as static is called the static nested class. • Inner: A non-static nested class is called the inner class. ©NIIT Java Fundamentals Lesson 3A / Slide 41 of 49
  • 42. Java Programming Constructs Creating Nested Classes (Contd.) • Nested Classes (Contd.): • Static Nested Class • A static nested class cannot access the members of its enclosing class directly because it is declared as static. • Therefore, it has to access the members of its enclosing class through an object of enclosing class. • The following code snippet shows the declaration of a static nested class: class EnclosingClass{ //statements static AstaticNestedClass { //statements } } ©NIIT Java Fundamentals Lesson 3A / Slide 42 of 49
  • 43. Java Programming Constructs Creating Nested Classes (Contd.) • Nested Classes (Contd.) • Inner Class: • An inner class is a non-static nested class, whose instance exists inside the instance of its enclosing class. • An inner class can directly access the variables and methods of its enclosing class. • The following figure shows a nested class: ©NIIT Java Fundamentals Lesson 3A / Slide 43 of 49
  • 44. Java Programming Constructs Demonstration-Selection Criteria of Fun Seasons Corp. • Problem Statement: • The Fun Seasons Corp. departmental store is conducting interviews for the post of salesman. Various applicants are applying for the required post by filling an application form. The departmental store performs the selection process of the applicants and the selected applicants are chosen as candidates. John David, the programmer hired by the departmental store is assigned the task of developing the Java application. He needs to create an application that accepts the applicant ID, in the “A#####” format, and educational status, such as Graduate and PostGraduate, as command-line arguments. Help John David to write a code for the selection procedure. ©NIIT Java Fundamentals Lesson 3A / Slide 44 of 49
  • 45. Java Programming Constructs Demonstration-Selection Criteria of Fun Seasons Corp.(Contd.) • Solution • To solve the problem, John David needs to use nested class. John David can create an Applicant class and a Candidate class as nested class of the Applicant class. The Candidate class is the inner class and can access the members of the Applicant class. The Candidate class is having the selection procedure of an applicant and displays the status of an applicant. The selection procedure is based on the age of the applicant. If the age of an applicant is below 25 years, the applicant is a candidate. To solve the given problem, perform the following tasks: 1. Create the code for the application. 2. Compile and execute the application. ©NIIT Java Fundamentals Lesson 3A / Slide 45 of 49
  • 46. Java Programming Constructs Summary In this lesson, you learned that: • Conditional statements are used to allow selective execution of statements. The conditional statements in Java are: • if-else • switch-case • Looping statements are used when you want a section of a program to be repeated a certain number of times. Java offers the following looping statements: • for • while • do-while ©NIIT Java Fundamentals Lesson 3A / Slide 46 of 49
  • 47. Java Programming Constructs Summary (Contd.) • The for loop checks the validity of a condition first and then executes the body of the loop. • The while loop checks for a condition before executing the body of the loop. • In the do-while loop, first the body of the loop is executed and then the conditional expression is evaluated. • The break and continue statements are used to control the program flow within a loop. • Relational operator is used to compare the values of two operands, such as >=, ==, !=, <= >, and <. • The three conditional operators are &&, ||, and !. • AND (&&) operator returns true if both the operands are true. OR (||) returns true if either of the operands are true. NOT (!) operator is a unary operator, which represents the false condition of an operand. ©NIIT Java Fundamentals Lesson 3A / Slide 47 of 49
  • 48. Java Programming Constructs Summary (Contd.) • You can generalize a method by passing parameters and arguments to the method. • The data type of the data returned by a method and the variable that receives the data must be compatible with the return type specified by the method. • Method overloading means having more than one method with the same name. The Java compiler resolves the method to be invoked using the signature of the method. • The signature of a method consists of: • The method name. • The number of arguments passed to the method. • The data types of the arguments passed. • The order of the arguments passed. ©NIIT Java Fundamentals Lesson 3A / Slide 48 of 49
  • 49. Java Programming Constructs Summary (Contd.) • Methods can take arguments of different data types. Variables of the primitive data type are passed by value, while variables of the abstract data type are passed by reference. • Nested class is a class that is defined as a member of another class. Nested classes are of two types: • Static nested class • Non-static nested class ©NIIT Java Fundamentals Lesson 3A / Slide 49 of 49