Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Similar documents
CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Identifiers, Data Types & Variables

ECE 122 Engineering Problem Solving with Java

Computational Expression

Introduction to Programming Using Java (98-388)

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Zheng-Liang Lu Java Programming 45 / 79

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Java Notes. 10th ICSE. Saravanan Ganesh

3. Java - Language Constructs I

Programming in C++ 6. Floating point data types

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Full file at

Java Primer 1: Types, Classes and Operators

1 Shyam sir JAVA Notes

Declaration and Memory

Static Members. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

ARG! Language Reference Manual

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Programming. Syntax and Semantics

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Chapter-8 DATA TYPES. Introduction. Variable:

CS242 COMPUTER PROGRAMMING

Values and Variables 1 / 30

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS 251 Intermediate Programming Java Basics

More Programming Constructs -- Introduction

1.3b Type Conversion

UNIT- 3 Introduction to C++

PROGRAMMING FUNDAMENTALS

Pace University. Fundamental Concepts of CS121 1

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

VARIABLES AND TYPES CITS1001

Chapter 2 Elementary Programming

CSI33 Data Structures

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

The Warhol Language Reference Manual

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Programming with Java

Java Fall 2018 Margaret Reid-Miller

Objectives. In this chapter, you will:

Java Lecture Note. Prepared By:

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CMPT 125: Lecture 3 Data and Expressions

Java+- Language Reference Manual

COMP6700/2140 Data and Types

Short Notes of CS201

Program Elements -- Introduction

Program Fundamentals

Java Overview An introduction to the Java Programming Language

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CS201 - Introduction to Programming Glossary By

corgi Language Reference Manual COMS W4115

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Datatypes, Variables, and Operations

Array. Prepared By - Rifat Shahriyar

Lecture 3: C Programm

Chapter 2: Using Data

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Operators and Expressions

Visual C# Instructor s Manual Table of Contents

Language Reference Manual

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Type Checking. Chapter 6, Section 6.3, 6.5

egrapher Language Reference Manual

Chapter 2 Primitive Data Types and Operations. Objectives

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Comments in a Java Program. Java Overview. Identifiers. Identifier Conventions. Primitive Data Types and Declaring Variables

3. Java - Language Constructs I

Lexical Considerations

1 Lexical Considerations

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

VLC : Language Reference Manual

CSc Introduction to Computing

Chapter 2: Data and Expressions

Flow Control. CSC215 Lecture

Programming for Engineers Iteration

Data Conversion & Scanner Class

B.V. Patel Institute of BMC & IT, UTU 2014

CPSC 3740 Programming Languages University of Lethbridge. Data Types

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Basics of Java Programming

The pixelman Language Reference Manual. Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

Full file at

INTRODUCTION 1 AND REVIEW

Chapter 2. Elementary Programming

Transcription:

Data Types, Variables and Arrays OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani

Identifiers in Java Identifiers are the names of variables, methods, classes, packages and interfaces. Identifiers must be composed of letters, numbers, the underscore _ and the dollar sign $. Identifiers may only begin with a letter, the underscore or a dollar sign. After the first character, an identifier can have any combination of characters. A Java keyword cannot be used as an identifier. Identifiers in Java are case sensitive, foo and Foo are two different identifiers. Example: Valid AvgTemp Count A4 $test This_is_ok Example: Invalid 2count High-temp Not/ok

Java Keywords

Java defines eight primitive (simple) types: 1)byte 8-bit integer type Data Types 2)short 16-bit integer type 3)int 32-bit integer type integer 4)long 64-bit integer type 5)float 32-bit floating-point type 6)double 64-bit floating-point type Floating point numbers 7)char symbols in a character set 8)boolean logical values true and false

byte: 8-bit integer type. Range: -128 to 127. Example: byte b = -15; byte c = 100; Usage: particularly when working with data streams. short: signed 16-bit integer type. Range: -32768 to 32767. Example: short c = 1000; Usage: probably the least used java type.

int: 32-bit integer type. Range: -2147483648 to 2147483647. Example: int b = -50000; Usage: 1) Most common integer type. 2) Typically used to control loops and to index arrays. 3) Expressions involving the byte, short and int values are promoted to int before calculation.

long: 64-bit integer type. Range: -9223372036854775808 to 9223372036854775807. Example: long l = 10000000000000000; Usage: 1) useful when int type is not large enough to hold the desired value float: 32-bit floating-point number. Range: 1.4e-045 to 3.4e+038. Example: float f = 1.5; Usage: 1) fractional part is needed 2) large degree of precision is not required

double: 64-bit floating-point number. Range: 4.9e-324 to 1.8e+308. Example: double pi = 3.1416; Usage: 1) accuracy over many iterative calculations 2) manipulation of large-valued numbers

char: 16-bit data type used to store characters. Range: 0 to 65536. (there are no negative char) Example: char c = a ; Usage: 1) Represents both ASCII and Unicode character sets; Unicode defines a character set with characters found in (almost) all human languages. 2) Not the same as in C/C++ where char is 8-bit and represents ASCII only.

boolean: Two-valued type of logical values. Range: values true and false. Example: boolean b = (1<2); Usage: 1) returned by relational operators, such as 1<2 2) required by branching expressions such as if or for

Variables declaration how to assign a type to a variable initialization how to give an initial value to a variable scope how the variable is visible to other parts of the program lifetime how the variable is created, used and destroyed type conversion how Java handles automatic type conversion type casting how the type of a variable can be narrowed down type promotion how the type of a variable can be expanded

Variables Java uses variables to store data. To allocate memory space for a variable JVM requires: 1) to specify the data type of the variable 2) to associate an identifier with the variable 3) optionally, the variable may be assigned an initial value All done as part of variable declaration.

Basic Variable Declaration datatype identifier [=value]; datatype must be A simple datatype User defined datatype (class type) Identifier is a recognizable name confirm to identifier rules Value is an optional initial value.

Variable Declaration We can declare several variables at the same time: type identifier [=value][, identifier [=value] ]; Examples: int a, b, c; int d = 3, e, f = 5; byte g = 22; double pi = 3.14159; char ch = 'x'; Dynamic initialization of variables is also possible.

Variable Scope Scope determines the visibility of program elements with respect to other program elements. In Java, scope is defined separately for classes and methods: 1) variables defined by a class have a global scope 2) variables defined by a method have a local scope A scope is defined by a block: { }

A variable declared inside the scope is not visible outside: { int n; } n = 1;// this is illegal Scopes can be nested.

Variable Lifetime Variables are created when their scope is entered and destroyed when their scope is left. A variable declared in a method will not hold its value between different invocations of this method. A variable declared in a block looses its value when the block is left. Initialized in a block, a variable will be re-initialized with every re-entry. Variables lifetime is confined to its scope!

Carried out by compiler automatically Ex: Carried out by programmer using casting int a; byte b; b= (byte) a;

Type Conversion Size Direction of Data Type Widening Type Conversion (Casting down) Smaller Data Type Larger Data Type Narrowing Type Conversion (Casting up) Larger Data Type Smaller Data Type

NOTE: A different type of conversion will occur when a floating point value is assigned to an integer type: truncation.

Type Conversion Widening Type Converstion Implicit conversion by compiler automatically byte -> short, int, long, float, double short -> int, long, float, double char -> int, long, float, double int -> long, float, double long -> float, double float -> double L 3.6

Type Conversion Narrowing Type Conversion Programmer should describe the conversion explicitly byte -> char short -> byte, char char -> byte, short int -> byte, short, char long -> byte, short, char, int float -> byte, short, char, int, long double -> byte, short, char, int, long, float L 3.7

Type Casting General form: (targettype) value Examples: 1) integer value will be reduced module bytes range: int i; byte b = (byte) i; 2) floating-point value will be truncated to integer value: float f; int i = (int) f; L 3.9

Type Promotion Rules 1.All byte, short and char values are promoted to int. 2.If one operand is a long, the whole expression is promoted to long. 3.If one operand is a float, the entire expression is promoted to float. 4.If any of the operands is double, the result is double.

Arrays An array is a group of liked-typed variables referred to by a common name. Arrays of any type can be created and may have one or more dimensions. name, with individual variables accessed by their index. Arrays are: 1) Declared 2) Created 3) Initialized 4) Used Also, arrays can have one or several dimensions.

Array Declaration Array declaration involves: 1) declaring an array identifier 2) declaring the number of dimensions 3) declaring the data type of the array elements Two styles of array declaration: data_type array-variable[]; or data_type [] array-variable;

Array Creation After declaration, no array actually exists. It is set to null, which represents an array with no values. In order to create an array, use the new operator, which allocates memory and returns the address of space allocated. data_type array-variable[]; array-variable = new data_type[size]; This creates a new array to hold size elements of type data_type, which reference will be kept in the variable array-variable. NOTE: The elements in the array allocated by new will automatically be initialized to zero.

It is possible to combine the declaration of the array variable with the allocation of the array itself. data_type array-variable[ ] = new data_type[size]; Ex: int sort[] = new int[5]; Array1.java

Array Indexing We can refer to the elements of an array through their indexes. array-variable[index] The array index always starts with zero. The Java run-time system strictly checks to makes sure that all array indexes are in the correct range, otherwise raises a run-time error.

Array Initialization Arrays can be initialized when they are declared. An array initializer is a list of comma-separated expressions surrounded by curly braces. The commas separate the values of the array elements. int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Note: 1) there is no need to use the new operator 2) the array is created large enough to hold all specified elements 3) month_days.length gives the length of array.

Multidimensional Arrays Multidimensional arrays are arrays of arrays. 1) declaration: int twod[][]; 2) creation: int twod = new int[4][5]; This allocates a 4 by 5 array and assigns it to twod. 3) Initialization : 2 by 3 matrix. int twod1[][] = { {1, 2, 3}, {4, 5, 6} };

When you allocate memory for a multi-dimensional array, you need only specify the memory for the first(leftmost) dimension. You can allocate the remaining dimensions separately. Ex: 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]; twod[0].length = 1; twod[1].length = 2; twod[2].length = 3; twod[4].length = 4; A row can have variable length columns.

Alternative Array Declaration Syntax int a1 [] = 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]; Alternative declaration is convenient when declaring several arrays at the same time. int [] num1, num2, num3; Same as: int num1[], num2[], num3[]; The alternative declaration is useful when specifying an array as a return type for a method.

References Herbert Schildt, The Complete Reference, JAVA, 7 th ed