+ All Categories
Home > Documents > Java Essentials PPT Lesson01

Java Essentials PPT Lesson01

Date post: 09-Sep-2015
Category:
Upload: ravi
View: 34 times
Download: 1 times
Share this document with a friend
Description:
Java Basics
Popular Tags:
41
Copyright © 2012-2014, Simplilearn, All rights reserved Java for Hadoop – Lesson 1 Essentials of Java for Hadoop
Transcript
  • Copyright 2012-2014, Simplilearn, All rights reserved

    Java for Hadoop Lesson 1

    Essentials of Java for Hadoop

  • Copyright 2012-2014, Simplilearn, All rights reserved. 2

    Lesson Objectives

    In this lesson, you will be able to:

    Understand what Java is

    Understand the object-oriented principles of Java

    Discuss various language constructs

    Learn programming in Java using these constructs

    Discuss classes and objects

  • Copyright 2012-2014, Simplilearn, All rights reserved. 3

    Java - Definition

    Java:

    Is an object-oriented programming language.

    Executes programs as bytecode, therefore it is portable and can function on different hardware and software platform.

    Program after compilation generates a bytecode which is interpreted by the Java Virtual Machine (JVM). JVM also secures the underlying hardware.

  • Copyright 2012-2014, Simplilearn, All rights reserved. 4

    Java Virtual Machine (JVM)

    Functions of Java Virtual Machine (JVM) bytecode verifier:

    Checks the branching instructions and ensures that they lead to valid branches

    Verifies the data types of the variables

    Ensures that the variables are always initialized and their references refer to valid locations

    Controls access to the public and private members

    Interprets the bytecode

  • Copyright 2012-2014, Simplilearn, All rights reserved. 5

    Working of Java

    Java components:

    Java is not only a programming language, but it also has a collection of components which can be used to develop web applications, mobile applications, etc.

    All these components interact together.

    To run a simple Java program, the programmer needs the javac compiler and java interpreter.

  • Copyright 2012-2014, Simplilearn, All rights reserved. 6

    Running a Basic Java Program

    Steps for setting up the environment on Windows:

    Click Control Panel -> System -> Advanced

    Click Environment Variables

    Under System Variables, click PATH

    Click Edit to edit the PATH variable

    In the editable text box of PATH, specify the location of the installed JDK

  • Copyright 2012-2014, Simplilearn, All rights reserved. 7

    Running a Basic Java Program (contd.)

    Steps involved in executing a Java program:

    Java programs can be written in a text editor such as notepad and saved with a .java extension.

    Every Java program should have a class which further has a main method.

    Java programs can be compiled using javac compiler.

    Once compiled, the class file is generated and interpreted through java interpreter.

    public class HelloWorld {

    public static void main (String args []) {

    System.out.println (" Hello World program");

    }

    }

    Class name

    Method name

    Output parameter Print line

  • Copyright 2012-2014, Simplilearn, All rights reserved. 8

    Running a Basic Java Program in NetBeans IDE

    When the user executes the Java programs from IDEs such as NetBeans, Eclipse, and so on, they are executed through an appropriate GUI component as shown below:

  • Copyright 2012-2014, Simplilearn, All rights reserved. 9

    BASIC JAVA SYNTAX

  • Copyright 2012-2014, Simplilearn, All rights reserved. 10

    Data Types in Java

    A Data type is required to store data used in a Java program. Java supports various basic data types as shown in the table below.

    Data type Memory size Minimum value Maximum value

    byte 1 byte -128 127

    short 2 bytes 32,768 32,767

    char 2 bytes 0 65,536

    int 4 bytes 2,147,483,648 2,147,483, 647

    float 4 bytes approximately 3.40282347E+38F

    (67 significant decimal digits)

    Java implements IEEE 754 standard

    approximately +3.40282347E+38F

    (67 significant decimal digits)

    Java implements IEEE 754 standard

    long 8 bytes 9,223,372,036,854,775,808

    9,223,372,036,854,775,807

    double 8 bytes -1.79769313486231570E+308

    (15 significant decimal digits)

    +1.79769313486231570E+308

    (15 significant decimal digits)

    boolean JVM dependent It can only assume the value to be true/false

  • Copyright 2012-2014, Simplilearn, All rights reserved. 11

    Variables in Java

    A variable in a Java program holds a value of a basic data type. Memory has to be allocated to the variables by declaring them with data types. Java also supports more complicated types of data known as classes. Java defines four types of variables:

    Instance variables

    Class variables

    Local variables

    Parameters

  • Copyright 2012-2014, Simplilearn, All rights reserved. 12

    Naming Conventions of Variables

    Following are the naming conventions for using variables in a Java program:

    Variables are case sensitive and should never start with $ or _.

    Characters that follow the first character in the variable name can be letters, digits, underscore, or even $.

    Variable names cannot be keywords or reserved words.

    The variable name should always begin with a lower case word.

    If the variable name has more than one word, then the first word should start with a lower case letter, subsequent words should start with an upper case letter.

    Subsequent words can be separated by an underscore.

  • Copyright 2012-2014, Simplilearn, All rights reserved. 13

    Type Casting

    Type casting is a process of converting one data type into another type.

    Boolean types cannot be converted.

    Types of Type Casting

    Implicit Type Casting

    Explicit Type Casting

  • Copyright 2012-2014, Simplilearn, All rights reserved. 14

    Operators

    An operator implies certain operation to be performed on operands.

    Types of Operators

    Mathematical Operators

    Unary Operators

    Relational Operators

    Logical or Conditional Operators

    Bitwise Operators

  • Copyright 2012-2014, Simplilearn, All rights reserved. 15

    Mathematical Operators

    Mathematical operators imply those who can perform mathematical operations.

    Mathematical operators supported in Java are:

    Java also provides various mathematical functions through java.lang.Math.

    Symbol Interpretation

    + Addition

    - Subtraction

    * Multiplication

    / Division

    % Modulus

    = Assignment operator

  • Copyright 2012-2014, Simplilearn, All rights reserved. 16

    Unary Operators

    Unary operators are those which have only one operand for the operation.

    Unary operators supported in Java:

    Operator Description

    + When prefixed it indicates that the integer is a

    positive value

    - When prefixed it indicates that it is a negative

    value

    ++ When prefixed or suffixed it increments the value

    by 1

    -- When prefixed or suffixed decrements the value by

    1

    ! When prefixed returns the binary complement

    equivalent of the operand

  • Copyright 2012-2014, Simplilearn, All rights reserved. 17

    Relational Operators

    Relational operators are used to compare two different values of any type.

    Relational operators supported in Java:

    Operator Description

    == Equal to

    != Not equal to

    >= Greater than or equal to

    Greater than

  • Copyright 2012-2014, Simplilearn, All rights reserved. 18

    Logical or Conditional Operators

    Logical operators allow checking multiple conditions simultaneously.

    Logical operators supported in Java are:

    Operator Description

    && Logical and

    || Logical OR

    ?; Ternary operator

  • Copyright 2012-2014, Simplilearn, All rights reserved. 19

    Bitwise Operators

    Bitwise operators are used to perform bit level operations on the operands. These operators interpret the bit equivalent of the variable on which it is applied and perform the specified operation on it.

    Bitwise operations supported in Java are:

    Operator Description

    & Bitwise AND

    | Bitwise OR

    ~ Bitwise complement

    > Signed right shift

  • Copyright 2012-2014, Simplilearn, All rights reserved. 20

    Static Versus Non-Static Variables

    The difference between final and static variables is as follows:

    Final Variables

    Initialized only once and not overwritten.

    Variables whose values remain

    unchanged.

    Prefixed with the keyword

    final.

    Can be initialized later in the program.

    Static Variables

    Prefixed with the keyword

    static.

    Common to all the members of

    the class.

  • Copyright 2012-2014, Simplilearn, All rights reserved. 21

    Static Versus Non-Static Variables (contd.)

    The code demonstrates the usage of static variable in the class ObjectCounter.

    public class ObjectCounter {

    private static int count=0;

    ObjectCounter(){

    count=count+1;

    }

    public static void main(String args[])

    {

    int i;

    for(i=0;i

  • Copyright 2012-2014, Simplilearn, All rights reserved. 22

    Statements and Blocks of Code

    A Java program comprises instructions for accomplishing a task. Each instruction of the code is termed as a statement.

    Statements can be classified into:

    Declaration statementsused for declaring variables

    Conditional statementscontrols the flow of execution based on conditions

    Loop statementsused to repeat a set of statements

    Blocks are logical groups of statements which are enclosed in {.}.

    Loop statements have certain blocks of code to be executed.

    Conditional statements may have multiple blocks of statements among which the right block is chosen during execution.

    A block of statement is associated with each class and method in the class.

  • Copyright 2012-2014, Simplilearn, All rights reserved. 23

    Flow Control

    The flow control structures in a program define the flow of execution of the program.

    The flow may change based on various conditions that arise during program execution or due to user input which is accepted interactively during the program execution.

    Following are the structures used in Java to control the program flow:

    If statement

    If-else statement /If-else-if ladder

    Nested-if statement

    Ternary operator

    Switch statement

    Loop statements

    for loop

    while loop

    do-while loop

    break and continue statements

  • Copyright 2012-2014, Simplilearn, All rights reserved. 24

    If Statement

    An if statement is used to check whether a condition or set of conditions holds good. If the condition holds good a block of statements is executed.

    Syntax of an if statement:

    if(condtion)

    {

    /* block of statements*/

    }

    The code demonstrates the use of if statement.

    class ConditionDemo{

    public static void main(String args[]){

    int a=10,b=5;

    if(a>b){

    System.out.println(a+is greater);

    }

    }

    }

  • Copyright 2012-2014, Simplilearn, All rights reserved. 25

    Variants of if Statement

    An if-else statement is used when the program has to make a choice between two blocks of statements.

    Syntax of if-else statement:

    if(condition){

    /* Statement block 1*/

    }

    else{

    /*statement block 2*/

    }

    An if-else-if ladder is used when there are multiple conditions to be checked sequentially and there are statements to be executed with each condition. Syntax of if-else-if ladder:

    if(condition1){

    /*Statement Block 1 */

    }

    else

    if(condition 2){

    /*Statement block2*/

    }

    else

    if(condition3){

    /*Statement block 3*/

    }

  • Copyright 2012-2014, Simplilearn, All rights reserved. 26

    Nested-If Statement

    A nested-if statement is used when multiple conditions are required to be checked one after the other and there are statements to be executed at each level of condition. Syntax of nested-if statement:

    if(Condition1)

    {

    /* statement block*/

    If(condition2){

    /* statements*/

    }

    }

    The code demonstrates the use of Nested-if statement.

    public class NestedIfUsage {

    public static void main(String args[]){

    int a,b,c;

    a=b=c=10;

    if(a==b){

    if(a==c){

    System.out.println("a,b,c are equal");

    }

    }

    else

    if(b==c){

    System.out.println("b,c are equal");

    }

    }

    }

  • Copyright 2012-2014, Simplilearn, All rights reserved. 27

    Switch Statement

    Using a switch statement is an efficient way of coding when a choice has to be made among multiple conditions.

    Syntax for a switch statement:

    switch(case){

    case 1: /* Block of statements*/

    break;

    case 2: /* Block of statements*/

    break;

    ..

    default: /* default block of statements*/

    break;

    }

    The code in the next slide demonstrates the usage of the switch statement.

  • Copyright 2012-2014, Simplilearn, All rights reserved. 28

    Switch Statement (contd.)

    public class SwitchUsage {

    public static void main(String args[]) throws

    IOException{

    int a,b=20,c=20,result = 0;

    System.out.println("1--> addition");

    System.out.println("2--> subtraction");

    System.out.println("3--> multiplication");

    System.out.println("4--> divison");

    System.out.println("5--> quit");

    a = (char)System.in.read();

    switch(a){

    case'1':result = c+b;

    break;

    case'2':result = c-b;

    break;

    case'3':result = c*b;

    break;

    case'4':result = c/b;

    break;

    case'5': break;

    default : System.out.println("Enter right

    choice");

    break;

    }

    System.out.println(result);

    }

    }

  • Copyright 2012-2014, Simplilearn, All rights reserved. 29

    Loop Statements

    Loop statements are used when a block of statements are expected to be executed multiple times.

    There are three variants of loops

    for loop

    while loop

    do-while loop

    A for loop is useful when the programmer knows exactly how many times the block of statements is expected to execute.

    Syntax:

    for(initialization; exit condition; navigation){

    /* Block of statements*/

    }

    The code shows the usage of for loop.

    public class LoopUsage {

    public static void main(String args[]) throws IOException {

    /* program to create multiplication table*/

    int a=5;

    for(int i=1;i

  • Copyright 2012-2014, Simplilearn, All rights reserved. 30

    Loop Statements (contd.)

    A while loop is used when a certain block of statements are expected to repeat until a certain condition is met.

    Syntax for using a while loop:

    while(condition){

    /* Block of statements*/

    }

    The code shows the usage of while loop.

    A do-while loop is similar in function to the while loop, it is used when the program requires execution of the block of statements at least once.

    Syntax for using a do-while loop:

    do{

    /* Block of statements*/

    } while(condition);

    public class LoopUsage { public static void main(String args[]) throws IOException { /* program to create multiplication table*/ int a=5, i=1; while((a*i)

  • Copyright 2012-2014, Simplilearn, All rights reserved. 31

    Break and Continue Statements

    Break and continue are also flow control statements used along with the loop statement.

    When a break statement is encountered in a block of code then the execution flow does not proceed further to the subsequent statements after the break, the execution continues from the next block of statements.

    When a continue statement is encountered the execution of the current iteration is terminated and proceeds with the next iteration in the loop.

  • Copyright 2012-2014, Simplilearn, All rights reserved. 32

    Basic Java Constructs

    All Java programs are written as classes. There are some essential classes that are required for executing a simple program in Java. Packages are a group of related classes in Java. java.lang package has all basic constructs required to execute the Java programs.

    Following are some of the important classes in java.lang package:

    Math class

    This class has all the methods which implement the mathematical operations and functions.

    String class

    This class has all the string handling methods.

    Scanner class

    This class is used to read input from the console.

  • Copyright 2012-2014, Simplilearn, All rights reserved. 33

    Arrays

    Arrays are defined as a collection of variables of the same type.

    Syntax for declaring an array:

    data type array_name = new datatype[size];

    The number of elements in the array must be declared while creating the array.

    Memory for the elements of the array is allocated as a continuous block.

    There are two variants of an array:

    One-dimensional array

    Two-dimensional array

    In one-dimensional array a group of variables are declared and accessed through an index.

    The code demonstrates the creation and usage of an array.

    public class ArrayUsage {

    public static void main(String args[]) {

    int array[] = new int[10];

    int i;

    Scanner input = new Scanner(System.in);

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

    array[i] = input.nextInt();

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

    System.out.println("This is array[" + i + "]: " + array[i]);

    }

    }

  • Copyright 2012-2014, Simplilearn, All rights reserved. 34

    Arrays (contd.)

    Two-dimensional arrays are an extended version of one-dimensional array, following are their features:

    The data in a two-dimensional array is organized like a table structure.

    The variables are arranged as rows and columns according to the index of the array.

    Following is declaration example of a two-dimensional array:

    int two_dimen[][] = new int[2][3];

    This declaration allocates memory according to the following pattern:

    The code displays the usage of a two-dimensional memory.

    public class TwoDimen {

    public static void main(String args[]){

    int t, i;

    int table[][] = new int[10][10];

    for(t=0; t < 10; ++t) {

    for(i=0; i < 10; ++i) {

    table[t][i] = (t+1)*(i+1);

    System.out.print(table[t][i] + " ");

    }

    System.out.println();

    }

    }

    }

    [0][0] [0][1] [0][2]

    [1][0] [1][1] [1][2]

  • Copyright 2012-2014, Simplilearn, All rights reserved. 35

    JAVA CLASSES AND METHODS

  • Copyright 2012-2014, Simplilearn, All rights reserved. 36

    Classes

    In a Java program all the data are modeled in terms of classes, objects, and methods.

    A class is a generic definition of an object such as person, vehicle, and so on.

    It defines certain properties and behavior of the object.

    The properties imply to the data pertaining to variables and the behavior is implemented by the methods in the class.

    Following is the syntax of class definition:

    class classname {

    // declare variables which signify the properties of the object

    type var1;

    // declare methods which signify the behavior of the object

    type method1(parameters) {

    // body of method

    }

    }

  • Copyright 2012-2014, Simplilearn, All rights reserved. 37

    Objects

    Objects are instances of a class.

    If a class is analogous to the plan of a building then object is analogous to actually constructing the building.

    Class creates a template, object materializes the template.

    Objects are created by allocating memory for the object using a new operator.

    Following is the syntax for creating an instance of a class:

    Class name object_identifier = new Classname();

    A class can have class variables and instance variables.

    Class variables are common to all the objects in the class.

    Instance variables are used by specific instances of the class.

    The code demonstrates an example of class with its variables and methods

    Example of a class and object:

    class Car{

    // properties of the car

    String color;

    String model name;

    Float engine_capacity;

    // behavior of the car

    void acceleration(){

    // implementation of the method i.e how acceleration actually happens in the car

    }

    void braking(){

    //implementation of braking i.e how brakes actually work in the car

    }}

    Car c = new Car(); //object or instance of a car

  • Copyright 2012-2014, Simplilearn, All rights reserved. 38

    Methods

    Methods are the implementation of the behavior of a class, here are its features:

    All the methods have a return type.

    A method may or may not have parameters.

    Java allows method overloading. This implies that the method has same name but different parameters.

    Appropriate method is invoked based on the parameter list.

    The code shows usage of methods and implementation of method overloading in a class.

    public class MethodsUsage { int add(int a, int b){ return a*b; } String add(String c, String d){ return c+d; } public static void main(String args[]){ MethodsUsage m = new MethodsUsage(); int result=m.add(4,5); String out = m.add("first","second"); System.out.println(result); System.out.println(out); } }

  • Copyright 2012-2014, Simplilearn, All rights reserved. 39

    Access Modifiers

    Java defines access modifiers to determine the access to various members of the class.

    Access to the data in the classes can be restricted by access modifiers.

    Java defines three types of access modifiers: public, private and protected.

    A class member declared public is accessible to other class members.

    A class member declared private, can be accessed only by the members of that class.

    A class member declared protected can be accessed by all the members in the package.

  • Copyright 2012-2014, Simplilearn, All rights reserved. 40

    Summary

    Let us summarize the topics covered in this lesson:

    Java is an object-oriented programming language.

    It is used with Hadoop to write Mapper and Reducer programs.

    Java bytecode and Java Virtual Machine enable portability of Java applications.

    Java supports primitive data types and their object interpretations.

    Apart from the usual operators, Java has a rich set of classes with predefined functions

    Java uses classes and objects as the basic building blocks of applications.

    Data encapsulation and data hiding are implemented through access modifierspublic, private, and protected.

  • Copyright 2012-2014, Simplilearn, All rights reserved. 41

    Thank You


Recommended