VARIABLES AND TYPES CITS1001

Similar documents
EXPRESSIONS AND ASSIGNMENT CITS1001

Introduction to Programming Using Java (98-388)

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

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

Full file at

An Introduction to Processing

[0569] p 0318 garbage

Java Basic Datatypees

Getting started with Java

Programming with Java

Chapter 2: Using Data

Exercise: Using Numbers

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Visual C# Instructor s Manual Table of Contents

printf( Please enter another number: ); scanf( %d, &num2);

Java Identifiers, Data Types & Variables

CS 231 Data Structures and Algorithms, Fall 2016

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

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.

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS112 Lecture: Primitive Types, Operators, Strings

int[] a; a = new int[8]; a[0] = 1; a[1] = 1; for (int i = 2; i < 8; i++) a[i] = a[i-1] - a[i-2];

Datatypes, Variables, and Operations

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

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

PROGRAMMING FUNDAMENTALS

Review for Test 1 (Chapter 1-5)

Fall 2017 CISC124 9/16/2017

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.

Exercise: Inventing Language

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CHAPTER 7 OBJECTS AND CLASSES

Zheng-Liang Lu Java Programming 45 / 79

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Full file at

LECTURE 3 C++ Basics Part 2

Computational Expression

Control Structures in Java if-else and switch

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

CIS133J. Working with Numbers in Java

3. Simple Types, Variables, and Constants

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Lexical Structure (Chapter 3, JLS)

QUIZ. What is wrong with this code that uses default arguments?

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

Introduction to C++ with content from

Java Fall 2018 Margaret Reid-Miller

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

Chapter 2: Using Data

The type of all data used in a C (or C++) program must be specified

CPS122 Lecture: From Python to Java

Assumptions. History

Pointers (continued), arrays and strings

Variables. Data Types.

COMP-202: Foundations of Programming

CHAPTER 7 OBJECTS AND CLASSES

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Understand Computer Storage and Data Types

A brief introduction to C programming for Java programmers

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Basics of Java Programming

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

1007 Imperative Programming Part II

Peer Instruction 1. Elementary Programming

Values, Variables, Types & Arithmetic Expressions. Agenda

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

index.pdf January 21,

Pointers (continued), arrays and strings

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

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

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Introduction to Computer Science and Object-Oriented Programming

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CS260 Intro to Java & Android 03.Java Language Basics

Lecture 2 Tao Wang 1

Chapter 02: Using Data

Declaration and Memory

ITI Introduction to Computing II

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Unit 2: Java in the small

ITI Introduction to Computing II

CSc Introduction to Computing

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS242 COMPUTER PROGRAMMING

Ticket Machine Project(s)

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

Values and Variables 1 / 30

Basic data types. Building blocks of computation

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Transcription:

VARIABLES AND TYPES CITS1001

Scope of this lecture Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule

Primitive types Every piece of data that is stored or moved around in Java must have a type Java supplies eight primitive types for the most fundamental pieces of data short! int! long! float! double! byte boolean! char!

Integer primitive types Four of the types are used for storing whole numbers Each occupies a fixed amount of memory byte uses 1 byte and stores integers from -128 to 127 short uses 2 bytes and stores integers from 32768 to 32767 int uses 4 bytes and stores integers from 2147483648 to 2147483647 long uses 8 bytes and stores integers from 9223372036854775808 to 9223372036854775807 (that is 2 63 to 2 63 1)

Floating point primitive types There are two types used for floating point values in other words, numbers with decimal points float uses 4 bytes and stores floating point numbers, positive or negative, in the range 1.4e-45 to 3.4028235e38 double uses 8 bytes and stores floating point numbers, positive or negative, in the range 4.9e-324 to 1.7976931348623157e308

Declaring variables In Java, all variables must be declared before they are used The declaration specifies the type and name of the variable When a primitive variable is created, a fixed amount of memory is set aside for that variable byte b;! double temp;! b! int balance;! temp! 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 balance!

Assignments to numeric types An assignment sets the value of a variable int x;!!// this is a declaration! x = 100;! Here are some legal assignments int x;! double y;! double z;!! x = 200;! y = -354.278;!!// this is an assignment! z = 23e5;!// this is scientific notation

Typecasting The compiler will warn you if you attempt to use a smallersized variable than it thinks the value needs For example, if you attempt float b;! b = 12.35;! then the compiler will warn you of a possible loss of precision because 12.35 is regarded as a double If this is really what you want, then you can cast the value float b;! b = (float) 12.35; for literals, there is a quick cast b = 12.35f;!

Non-numeric primitive types boolean is used to hold one of the two boolean values true or false! boolean isdone; // declaration! isdone = false;! // assignment! char uses two bytes and holds a Unicode character! char ch;!!// declaration! ch = A ;!!// assignment!

Literals Actual values that you type into the source code are called literals - these are things like 100, -23, true, Z, 37, 1e-5, 22, 3.1415926! Everything in Java has a type, even the literals, so the types of these are respectively!int, int, boolean, char, int, double, int, double!

Initialization If a variable is given an initial value directly after it is declared, then you can combine the two steps into one int x;! x = 10;! boolean isfinished;! isfinished = true;! is equivalent to int x = 10;! boolean isfinished = true;!

Type-checking The Java compiler does extensive type-checking to make sure that the program does not have any obvious problems The compiler will not allow the wrong type of value to be assigned to a variable int x;! x = true; // cannot assign boolean to int Similarly, the compiler will not allow the wrong type of value to be used as a method or constructor argument What about this? int x = 20;! double y;! y = x;! Although the types don t match perfectly, the computer knows that there is a value of type double corresponding to any value of type int in this case the value 20.0 is assigned to y!

Variable declarations Declaring reference types is just the same as for primitive types, with the type occurring before the name String s;! BankAccount b1;! BankAccount b2;! SimpleCanvas sc;! This has created the variables the names for the objects but we have not actually created the objects themselves Creating objects is (almost always) done with the Java keyword new

Creating objects To actually create the object, the constructor is called sc = new SimpleCanvas();! b1 = new BankAccount( BillG,12345,10000);! An object is fundamentally different from a primitive value It does not have a fixed size when it is created Its size can change during runtime For this reason, objects are stored in a different part of the computer s memory, and the value that is stored in the variable is a reference to the object A reference contains the information needed to find the actual object in the computer s memory The variable refers to an object (rather than contains an object)

References b1! 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 10 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 Garbage The variable b1 contains a reference to some location in the heap The actual object that b1 refers to is stored on the heap Some other object The Heap

The Golden Rule The behaviour of reference types and primitive types appears quite different, but it all hinges on the following observation A variable of primitive type contains a value A variable of reference type refers to an object The Golden Rule Whenever a variable is used, it is the contents of the shoebox that is used this is the value for a variable of primitive type this is the reference for a variable of reference type

Summary Java supplies eight primitive types for the most fundamental pieces of data A variable name and type must be declared before the variable can be used in a program. The Java compiler does extensive type-checking to make sure that the program does not have any obvious problems Creating objects is (almost always) done with the Java keyword new! Every variable has a name used to refer to it Every variable has a value, that changes during program run time.