Data Types Variables Arrays

download Data Types Variables Arrays

of 18

Transcript of Data Types Variables Arrays

  • 8/15/2019 Data Types Variables Arrays

    1/18

     

    1.7 Data Types, Variables, and Arrays

    1.7.1 Java Is a Strongly Typed Language 

    It is important to state at the outset that Java is a strongly

    Indeed, part of Java‘s safety and robustness comes from

    Let‘s see what this means.

    o  F irst, every vari able has a type, e

    is str ictl y defined.

    Second, all assignments, whethmethod calls, are checked for typ

  • 8/15/2019 Data Types Variables Arrays

    2/18

     

    These can be put in four groups:

    o  I ntegers:  This group includes byte, short, int, and lo

    o  F loating-poin t numbers: This group includes float an

    fractional precision.

    Characters: This group includes char, which represen

    numbers.

    o  Boolean: This group includes boolean, which is a spe

    The primitive types represent single values — not

    Although Java is otherwise completely object-oriThey are analogous to the simple types found in m

  • 8/15/2019 Data Types Variables Arrays

    3/18

     // Compute Product of Large numbers.

    class Product

    {

     public static void main(String args[])

    {

     byte a;

    short b

    int c;

    long d;

    long prod;

     prod = a*b*c*d;

    System.out.print(" the product of large numb}

    }

  • 8/15/2019 Data Types Variables Arrays

    4/18

     

    1.7.2.3 Characters 

    In Java, the data type used to store characters is char.

    However, C/C++ programmers beware:

    o  char in Java is not the same as char in C or C++.

    This is not the case in Java. Instead, Java uses Un

    defines a fully international character set that can

    human languages.

    o  It is a unification of dozens of character sets, suc

    Katakana, Hangul, and many more.

    For this purpose, it requires 16 bits. Thus, in Jav

    is 0 to 65 536 There are no negative chars

  • 8/15/2019 Data Types Variables Arrays

    5/18

     

    // char variables behave like integers.

    class CharDemo2 {

     public static void main(String args[]) {

    char ch1;

    ch1 = 'X';

    System.out.println("ch1 contains " + ch1);

    ch1++; // increment ch1

    System.out.println("ch1 is now " + ch1);

    }

    }

    Th

    her

    ch

    ch

    In the program, ch1 is first given the value X. Next, ch1

    Thi lt i h1 t i i Y th t h t i th

  • 8/15/2019 Data Types Variables Arrays

    6/18

     

    The output generated by this program is shown here:

     b is false

     b is true

    This is executed.

    10 > 9 is true

    Explanation of program

    There are three interesting things to notice about this pro

    First, as you can see, when a boolean value is out

    displayed.

    Second, the value of a boolean variable is sufficie

  • 8/15/2019 Data Types Variables Arrays

    7/18

     

    1.7.3.2 Floating-Point Literals

    o  Floating-point numbers represent decimal values

    Example, 2.0, 3.14159, and 0.6667 represent vali

    The exponent is indicated by an E or e followed b

    or negative.

    o  Examples include 6.022E23, 314159E – 05, and 2e

    Note: F loating-point l iterals in Java defaul t to double

    must append an F or f to the constant.

  • 8/15/2019 Data Types Variables Arrays

    8/18

     

    1.7.3.5 String Literals

    String literals in Java are specified like they are in

    a sequence of characters between a pair of double

    Examples of string literals are

    ―Hello World‖ 

    ―two\nlines‖ 

    ―\‖This is in quotes\‖― 

  • 8/15/2019 Data Types Variables Arrays

    9/18

     

    Example:

    int num;

    int num1, num2;

    int num=10;

    int num1=10, num2=20;

    int a, b, c; // declares three

    int d = 3, e, f = 5; // declare

     byte z = 22; // initializes z.

    double pi = 3.14159; // dec

    char x = 'x'; // the variable x

    1.7.4.2 Dynamic Initialization 

    // Demonstrate dynamic initialization.

    class DynInit{

    bli i id i (S i [])

  • 8/15/2019 Data Types Variables Arrays

    10/18

     

    o  However, if that method has parameters, they to

    o  As a general rule, variables declared inside a sco

    that is defined outside that scope.

    o  Thus, when you declare a variable within a scop

     protecting it from unauthorized access and/or m

    Indeed, the scope rules provide the foundation fo

    // Demonstrate block scope.

    class Scope

    { public static void main(String args[])

    {

  • 8/15/2019 Data Types Variables Arrays

    11/18

     

    1.7.5.1 Java’s Automatic Conversions 

    When one type of data is assigned to another type of var

     place if the following two conditions are met: 

      The two types are compatible.

      The destination type is larger than the source typ

    1.7.5.2 Casting Incompatible Types 

    o  What if you want to assign an int value to a byte

    This conversion will not be performed automatico  This kind of conversion is sometimes called a na

  • 8/15/2019 Data Types Variables Arrays

    12/18

     

    Conversion of double to int.

    d and i 323.142 323

    Conversion of double to byte.

    d and b 323.142 67

    1.7.5.3 Automatic Type Promotion in Expression

    In an expression, the precision required of an intermediat

    either operand.

    For example,

     byte a = 40; byte b = 50;

  • 8/15/2019 Data Types Variables Arrays

    13/18

     

    b = (byte)(b * 2); which yields the correct value of 100.

    1.7.5.4 The Type Promotion Rules

    Java defines several type promotion rules that apply to ex

    1)  All byte, short, and char values are

    2)  Then, if one operand is a long, the

    3)  If one operand is a float, the entire

    4)  If any of the operands is double, th

    1.7.6 Arrays 

  • 8/15/2019 Data Types Variables Arrays

    14/18

     o  new is a special operator that allocates memory.

    o  The general form of new as it applies to one-dime

    array-var = new type[size];

    o  Example:

    month_days = new int[12];

    After this statement executes, month_days will re

    o  Further, all elements in the array will be initialize

    Let‘s review: Obtaining an array is a two -step pro

    F irst, you must declare a variable of the

    Second, you must all ocate the memory th

    assign i t to the array vari able

  • 8/15/2019 Data Types Variables Arrays

    15/18

     For example, to store the number of days in each month,

    array of integers:

    // An improved version of the previous program.

    class AutoArray

    { public static void main(String args[])

    {

    int Number[] = { 1, 2, 3, 4, 5 };

    for( int i=0; i

  • 8/15/2019 Data Types Variables Arrays

    16/18

     This program generates the following output:

    0 1 2 3 4

    5 6 7 8 9

    10 11 12 13 14

    15 16 17 18 19

  • 8/15/2019 Data Types Variables Arrays

    17/18

     For example, the following program creates a two-dimen

    dimension are unequal.

    // manually allocate differing size second dimensions.

    class TwoDAgain{

     public static void main(String args[])

    {

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

    twoD[0] = new int[1];

    twoD[1] = new int[2];

    twoD[2] = new int[3];

    twoD[3] = new int[4];

    fo{

    }

    }

    }

  • 8/15/2019 Data Types Variables Arrays

    18/18

     For example, the following two declarations are equivale

    int al[] = new int[3];

    int[] a2 = new int[3];

    The following declarations are also equivalent:

    char twod1[][] = new char[3][4];

    char[][] twod2 = new char[3][4];

    This alternative declaration form offers convenience whe

    For example,

    int[] nums, nums2, nums3; // create three arrays c

    It is the same as writing

    int nums[], nums2[], nums3[]; // create three arrays