basics of java language (introduction)

Upload: anik

Post on 30-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Basics Of java language (Introduction)

    1/28

    JavaLanguage Basics

  • 8/14/2019 Basics Of java language (Introduction)

    2/28

    Keywords

    Keywords of Java are given below abstract continue for new

    switch

    assert*** default goto* packagesynchronized

    boolean do if privatethis

    break double implements protected throw

    byte else import public throws

    case enum**** instanceof return transient

    catch extends int short trychar final interface static void

    class finally long strictfp** volatile

    const* float native super while

    * not used

    ** added in 1.2

  • 8/14/2019 Basics Of java language (Introduction)

    3/28

  • 8/14/2019 Basics Of java language (Introduction)

    4/28

    Rules for naming Identifiers

    Any keyword should not be used as anidentifier.

    The name should consist of onlyalphabets, numbers and underscore.

    First character should be any alphabet orunderscore.

    The name cannot start with a number.

    Lowercase and uppercase characters areconsidered as different in C++.

  • 8/14/2019 Basics Of java language (Introduction)

    5/28

    Basic data Types

    In Java data types can be classifiedunder four major categoriesdepending upon the space requiredfor storing data in the memory.

    Integers Floating Point Numbers

    Characters

    Boolean

  • 8/14/2019 Basics Of java language (Introduction)

    6/28

    Integers

    Integers are used to store wholevalued numbers.

    All of these are signed, positive ornegative values.

    Java does not support unsignedintegers.

    Java defines four integer types byte,

    short, int and long.

  • 8/14/2019 Basics Of java language (Introduction)

    7/28

    Integers

    Data

    Type

    Size in

    Bytes

    Range

    byte 1 -128 to 127

    short 2 -32768 to 32767

    int 4 -2,147,483,648 to 2,147,483,647Long 8 -9,223,372,036,854,775,808 to

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

  • 8/14/2019 Basics Of java language (Introduction)

    8/28

    Floating Point Numbers

    Floating point numbers are also known asreal numbers and are used to storefractional precision values.

    ava defines two types of floating pointtypes :

    float

    double

  • 8/14/2019 Basics Of java language (Introduction)

    9/28

    Floating Point Numbers

    Data

    Type

    Size in

    Bytes

    Range

    float 4 1.4e-045 to 3.4e+038

    double 8 4.9e-324 to 1.8e+308

  • 8/14/2019 Basics Of java language (Introduction)

    10/28

    Characters

    char data type is used to store characters. char is a 16 bit type because Java uses

    Unicode to represent characters instead ofASCII.

    Unicode is a international character set havingrepresentation of all the character set found inthe dozens of languages like English, Latin,Greek, etc. Therefore to store a character

    value in Java we require 2 bytes (16 bits). Therange of a character set is 0 to 65,536.

  • 8/14/2019 Basics Of java language (Introduction)

    11/28

    Booleans

    Booleans are used to store logicalvalues, i.e. true or false.

    Boolean data types are used to store thevalues returned by the conditional

    expressions, such asi >= j.

  • 8/14/2019 Basics Of java language (Introduction)

    12/28

    Operators

    Operators are used to perform anyarithmetic or logical operations ongiven expressions.

    The variables, constants, or values are

    joined to build an expression.The variables or constants on which

    operator is applied are known as

    operands.An operator is applied to one or morethan one expression or operands toget some value.

  • 8/14/2019 Basics Of java language (Introduction)

    13/28

    Unary vs Binary Operator

    Some operators need one operand and someneed two to operate. Unary operatorsrequire one operand to yield result, whilebinary operators require two operands to

    operate.For example, in the following expression

    40 20

    We are using () as a binary operator

    because it is operating on two operands, 40and 20.

    Lets take another expression

    15

  • 8/14/2019 Basics Of java language (Introduction)

    14/28

    Java Operators

    In Java, operators can be divided intofollowing categories

    Increment and Decrement Operator Assignment Operator

    Arithmetic operators Relational Operators Logical Operators

  • 8/14/2019 Basics Of java language (Introduction)

    15/28

    Increment / Decrement Operator

    The unary operator ++ and -- are used toincrement or decrement the value of anyvariable by 1.

    ++ is the increment operator and is the

    decrement operator.These operators may be used either as

    prefix or postfix notations.

    In prefix notation, the value of the variableis increased by 1 before the variable isused.

    In the postfix notation, the value of the

    variable is increased by one after usingthe variable.

  • 8/14/2019 Basics Of java language (Introduction)

    16/28

    Increment / Decrement Operator

    For example, if i = 5, then in the followingexpressions,

    j = i++

    k = ++i

    In the first expression, the value of i willbe assigned to j and then incremented,i.e.

    j = 5 and i = 5.

    In the second expression, the value of iwill be incremented first and then thisvalue will be assigned to k i.e.

    i =5 and k = 5.

  • 8/14/2019 Basics Of java language (Introduction)

    17/28

    Assignment Operator

    Assignment operator is used to assign avalue to any variable. In Java, = sign isused as assignment operator.

    For example, in the following expression

    a = 10We can assign a single value to more than

    one variable in a single expression

    a = b = c = 10;

    Here, all the three variables, i.e. a, b and care assigned the value 10.

  • 8/14/2019 Basics Of java language (Introduction)

    18/28

    Assignment Operator

    In Java, we should also assign a value to avariable using compound assignmentoperator. For example,

    a = a + 10;

    Above expression can also be written as,a += 10;

    Here, += is a compound assignmentoperator.

  • 8/14/2019 Basics Of java language (Introduction)

    19/28

    Assignment Operator

    Similarly we should used followingcompound operators

    a - = 10;

    a * = 10;

    a / = 10; a % = 10;

  • 8/14/2019 Basics Of java language (Introduction)

    20/28

    Arithmetic Operator

    Arithmetic operators are used to performnumeric calculations on the operands.

    There are five arithmetic operators

    Operator Description Example

    + Addition a + b

    - Subtraction a b

    * Multiplication a * b

    / Division a / b

    % Modulus (returns the remainder after division) a % b

    xamp e:

  • 8/14/2019 Basics Of java language (Introduction)

    21/28

    xamp e:// Program to display the arithmetic operationspublic class Calc {

    public static void main(String args[]) {

    int a = 5, b =7, result = 0;result = a + b;System.out.println(Sum = +

    result);result = a - b;

    System.out.println(Difference = + result);

    result = a * b;System.out.println(Dividend = +

    result);result = a / b;System.out.println(Remainder =

    + result);}

    }

  • 8/14/2019 Basics Of java language (Introduction)

    22/28

  • 8/14/2019 Basics Of java language (Introduction)

    23/28

    Relational Operators

    Operator Description Example

    < Less than a < b

    > Greater than a > b

    = = Equal to a = = b

    ! = Not equal to a ! = b

    = b

  • 8/14/2019 Basics Of java language (Introduction)

    24/28

    Logical Operators

    Logical operators are used to combineand evaluate two or more expressions.These operators produce 0 or 1 as

    result.The logical operator table is given below

    Operator Description Example

    && AND (a = = 5) && (b > a)

    || OR (a = = 5) || (b > a)

    ! NOT ! (a = = 5)

  • 8/14/2019 Basics Of java language (Introduction)

    25/28

    Expressions

    Combination of operands andoperatorsArithmetic expression

    a = b + 10;Relational expression

    a

  • 8/14/2019 Basics Of java language (Introduction)

    26/28

    Type Casting

    Type Casting refers to changingan entity of one data type intoanother. This is important for thetype conversion in developingany application. If you will storea int value into a byte variabledirectly, this will be illegal

    operation. For storing yourcalculated int value in a bytevariable you will have to change

    the type of resultant data which

  • 8/14/2019 Basics Of java language (Introduction)

    27/28

  • 8/14/2019 Basics Of java language (Introduction)

    28/28