Chapter 01 Intro to Java

Embed Size (px)

Citation preview

  • 8/21/2019 Chapter 01 Intro to Java

    1/20

    Comp414 AdvancedProgramming

  • 8/21/2019 Chapter 01 Intro to Java

    2/20

    Chapter 01 Introduction to Java

    Contents:

    Introduction

    Data types, Variables and Keywords

    Selection Statements and Loops

    Methods and Arrays

    Chapter 01 Introduction to Java Comp414 Advanced Programming 2

  • 8/21/2019 Chapter 01 Intro to Java

    3/20

    IntroductionThe Genesis of Java:

    During the late 1970s and early 1980s, C became the dominant computerprogramming language.

    Need for C++:

    Complexity is the big problem in C programming

    Once a program exceeds somewhere between 25,000 and 100,000 lines of code, itbecomes so complex that it is difficult to grasp as a totality

    C++ was invented by Bjarne Stroustrup in 1979

    C++ is extends of C by adding object-oriented features.

    World Wide Web and Internet are created one more revolution in theprogramming: is given the stage for the Java

    Creation of Java

    In 1991 the new language is developed by Sun Microsystems. Itsnamed as Javain 1995

    Java is the platform-independent language (architecture neutral)

    Platform-independent language that could be used to produce code that wouldrun on a variety of CPUs under differing environments.

    Programs are inbuilt with the internet for active programs

    Chapter 01 Introduction to Java Comp414 Advanced Programming 3

  • 8/21/2019 Chapter 01 Intro to Java

    4/20

    Cont.,Java Characteristics

    Java is simplein that it retains much familiar syntax of C++. It is strongly typed. This means that every thing must have a

    data type.

    Java performs its own memory management avoiding thememory leaks that plague programs written in languages like C

    and C++.

    Java is completely object oriented.

    Java is multi-threaded, allowing a program to do more than onething at a time.

    Network-Savvy: extensive library of routines for coping withTCP/IP protocols like HTTP and FTP.

    Secure and Robust: Java is intended for writing programs thatare reliable and secure in a variety of ways.

    Portable and architecture neutral.

    Chapter 01 Introduction to Java Comp414 Advanced Programming 4

  • 8/21/2019 Chapter 01 Intro to Java

    5/20

    Data Types

    In java there are two categories of data types: Primitive (Basic/Simple) types: consists of int, float, char

    and Boolean.

    Reference types: consists of interfaces, classes, and arrays

    Chapter 01 Introduction to Java Comp414 Advanced Programming 5

  • 8/21/2019 Chapter 01 Intro to Java

    6/20

    Cont.,1. Integers:

    Java defines four integer types: byte, short, int, and long. All of these aresigned, positive and negative values. Java does not support unsigned,positive-only integers.

    byte:

    The smallest integer type is byte. This is a signed 8-bit type that has a rangefrom 128 to 127. Byte variables are declared by use of the byte keyword. For

    example:byte b, c;

    short:

    short is a signed 16-bit type. It has a range from 32,768 to 32,767. It isprobably the least-used Java type. example of short variable declarations:

    short b;int:

    The most commonly used integer type is int. It is a signed 32-bit type thathas a range from 2,147,483,648 to 2,147,483,647. In addition to other uses,variables of type int are commonly employed to control loops and to indexarrays.

    int a, b;Chapter 01 Introduction to Java Comp414 Advanced Programming 6

  • 8/21/2019 Chapter 01 Intro to Java

    7/20

    Cont.,long:

    long is a signed 64-bit type and is useful for those occasions where an int type is not largeenough to hold the desired value. The range of a longis quite large.

    long seconds;

    2. Floating-Point types:

    Floating-point numbers, also known as real numbers, are used when evaluating expressionsthat require fractional precision. There are two kinds of floating-point , float anddouble.

    float hightemp; double area;

    3. Character and String

    In C/C++, char is an integer type that is 8 bits wide. This is not the case in Java. Instead,Java uses Unicode to represent characters. Unicode defines a fully international character.In Java char is a 16-bit type. The range of a char is 0 to 65,536. It is used to represent a singlecharacter. Character literal is enclosed by single quotation mark

    char ch1=A

    String:It represent a string of character. String literal is enclosed by double quotation mark

    String mess=WelcometoJava;

    4. Boolean:

    Java has a simple type, called boolean, for logical values. It can have only one of 2 possiblevalues, true or false. This is the type returned by all relational operators, such as a < b.

    boolean b;Chapter 01 Introduction to Java Comp414 Advanced Programming 7

  • 8/21/2019 Chapter 01 Intro to Java

    8/20

    VariablesThe variable is the basic unit of storage in a Java program. A variable isdefined by the combination of an identifier, a type, and an optional

    initializer.Declaring Variable:

    In Java, all variables must be declared before they can be used. The basicform of a variable declaration is shown here:

    type identifier [ = value][, identifier [= value] ...] ;

    The type is one of Javasatomic types, or the name of a class or interface.The identifier is the name of the variable. You can initialize the variable byspecifying an equal sign and a value is called constant variable.

    int a, b, c; // declares three ints, a, b, and c.

    int d = 3, e, f = 5; //constant variable

    double pi = 3.14159; // declares an approximation of pi.char x = 'x';

    Dynamic declaration:

    Java allows variables to be initialized dynamically, using any expressionvalid at the time the variable is declared.

    double c = Math.sqrt(a * a + b * b);

    Chapter 01 Introduction to Java Comp414 Advanced Programming 8

  • 8/21/2019 Chapter 01 Intro to Java

    9/20

    Type ConversionAssign a value of one type to a variable of another type. If the two types

    are compatible, then Java will perform the conversion automatically.Automatic conversion:

    Automatic conversion is following two conditions:

    The two types are compatible.

    The destination type is larger than the source type.

    For example int to long is possible by automaticallyIncompatible Types:

    Type conversion between two incompatible types is possible by cast. It hasthis general form:

    (target-type) value

    Int to byteint a; byte b; b = (byte) a;

    double to int

    double a; int b; b = (double) a;

    Chapter 01 Introduction to Java Comp414 Advanced Programming 9

  • 8/21/2019 Chapter 01 Intro to Java

    10/20

    Selection StatementJava supports two selection statements: ifandswitch. These statements allow youto control the flow of your programsexecution based upon conditions known only

    during run time.ifA simple if statement executes an action if and only if the condition is true.Syntax: if(boolean-exoression)

    statement;

    IfElse statement To take alternative actions when the condition is false, you can use a n ifelse

    statement.Syntax: if(boolean-expression)

    statement for true case;else

    statement for false case;Chapter 01 Introduction to Java Comp414 Advanced Programming 10

  • 8/21/2019 Chapter 01 Intro to Java

    11/20

    Cont.,Nested ifs:A nested if is an if statement that is the target of another if or else. Nested ifs are

    very common in programming. When you nest ifs, the main thing to remember isthat an elsestatement always refers to the nearest ifstatement.if(i == 10) {

    if(j < 20) a = b;if(k > 100)

    c = d;

    else a = c;}else a = d;if-else-if Ladder:if(condition)

    statement;else if(condition)

    statement;else if(condition)

    statement;..else

    statement;Chapter 01 Introduction to Java Comp414 Advanced Programming 11

  • 8/21/2019 Chapter 01 Intro to Java

    12/20

    Cont.,Switch:

    The switch statement is Javas multiway branch statement. It provides an

    easy way to dispatch execution to different parts of your code based on thevalue of an expression. As such, it often provides a better alternative thana large series of if-else-if statements.

    Syntax:

    switch (switch-expression)

    {case value1: statement(s)1;

    break;

    case value2: statement(s)2;

    break;

    ...case valueN: statement(s)N;

    break;

    default: statement(s)-for-default;

    }

    Chapter 01 Introduction to Java Comp414 Advanced Programming 12

  • 8/21/2019 Chapter 01 Intro to Java

    13/20

    loopsJavas iteration statements are for, while, anddo-while. These statements createwhat we commonly call loops. A loop is repeatedly executes the same set of

    instructions until a termination condition is met.While Loop:Syntax:while (condition)

    {

    // Loop body

    Statement(s);}

    The condition can be any Boolean expression. The body of the loop will be executed as

    long as the conditional expression is true. When condition becomes false, control passes

    to the next line of code immediately following the loop.

    do while Loop

    do-while loop always executes its body at least once, because its conditional expression is

    at the bottom of the loop. Its general form is:

    do

    {

    // body of loop

    } while (condition);

    Chapter 01 Introduction to Java Comp414 Advanced Programming 13

  • 8/21/2019 Chapter 01 Intro to Java

    14/20

    Cont.,

    Chapter 01 Introduction to Java Comp414 Advanced Programming 14

    ForFor loop is a powerful and versatile construct. Here is the general form ofthe for statement:Syntax:

    for(initialization; condition; iteration) {// body

    }It is important to understand that the initialization expression is only executedonce. Next, condition is evaluated. This must be a Boolean expression. Itusually tests the loop control variable against a target value. If this expressionis true, then the body of the loop is executed. If it is false, the loop terminates.Next, the iteration portion of the loop is executed. This is usually an expressionthat increments or decrements the loop control variable.

  • 8/21/2019 Chapter 01 Intro to Java

    15/20

    MethodsSyntax:

    modifier returnValueTypemethodName(list of parameters){

    // method body;

    }

    Method overloading

    In Java it is possible to define two or more methods within the same class thatshare the same name, as long as their parameter declarations are different. When

    this is the case, the methods are said to be overloaded, and the process is referred

    to as method overloading. Method overloading is one of the ways that Java

    implements polymorphism.

    void test()

    void test(int a)

    void test(int a, int b)

    double test(double a)

    Chapter 01 Introduction to Java Comp414 Advanced Programming 15

  • 8/21/2019 Chapter 01 Intro to Java

    16/20

    ArrayAn array is a group of like-typed variables that are referred to by a common name.Arrays of any type can be created and may have one or more dimensions. A specificelement in an array is accessed by its index.One dimensional array:The general form of a one-dimensional array declaration is:type var-name[ ]; double myList[];In fact, the value of myList is set to null, which represents an array with no value. Tolink myList with an actual, physical array of integers, you must allocate one usingnew and assign it to myList. new is a special operator that allocates memory.

    array-var = new type[size];myList = new double[10];

    But we can make it this two step procedures byOne step:type array-var=new type[size]

    double myList = new int[10];Processing array:Type onedouble myList={5.6, 4.5, 3.3, 1.2, 4, 3.3, 3, 4, 99, 1}

    Type two2. for(int i=0; i

  • 8/21/2019 Chapter 01 Intro to Java

    17/20

    Cont.,Multidimensional array:

    In Java, multidimensional arrays are actually arrays of arrays. To declare a

    multidimensional array variable, specify each additional index using another set of

    square brackets. It used to represents table or matrix. For example:

    int twoD[][] = new int[4][5];

    Processing 2D array:

    for(i=0; i

  • 8/21/2019 Chapter 01 Intro to Java

    18/20

    Access ControlAs you know, encapsulation links data with the code that manipulates it.Encapsulation provides another important attribute: access control. youcan control what parts of a program can access the members of a class. Bycontrolling access, you can prevent misuse.

    Javas access specifiers are public, private, and protected. protected appliesonly when inheritance is involved.

    public:When a member of a class is modified by the public specifier, then thatmember can be accessed by any other code.

    private:

    When a member of a class is specified as private, then that member can

    only be accessed by other members of its class.

    Chapter 01 Introduction to Java Comp414 Advanced Programming 18

  • 8/21/2019 Chapter 01 Intro to Java

    19/20

    keywordsThere are 49 reserved keywords currently defined in the Java language. Thesekeywords cannot be used as names for a variable, class, or method.

    Chapter 01 Introduction to Java Comp414 Advanced Programming 19

  • 8/21/2019 Chapter 01 Intro to Java

    20/20

    End of Chapter 01

    Questions

    &

    Doubts

    Ch t 01 I t d ti t J C 414 Ad d P i 20