VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
String Escape Sequences
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Lecture #5 Introduction to C++
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1.2 Primitive Data Types and Variables
CPS120: Introduction to Computer Science Variables and Constants.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 2 Variables.
Chapter 2 Basic Computation
Programming Fundamentals
Object Oriented Programming
Multiple variables can be created in one declaration
Variables and Primative Types
Type Conversion, Constants, and the String Object
Chapter 2 Basic Computation
Principles of Computer Programming (using Java) Chapter 2, Part 1
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Chapter 2 Variables.
C++ Data Types Data Type
Introduction to Primitives
Introduction to Primitives
Java Basics Data Types in Java.
Names of variables, functions, classes
Chapter 2 Variables.
Variables and Constants
Presentation transcript:

VARIABLES AND TYPES CITS1001

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

byte short int long float double 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 boolean char Primitive types float double 3

Four of the types are used for storing whole numbers Each occupies a fixed amount of memory Each can represent numbers in a certain range Integer primitive types byteone byte–128 to 127 shorttwo bytes–32768 to intfour– to longeight– to

Two of the types are used for storing “real numbers” In other words, numbers with decimal points Both positive and negative numbers in these ranges But most numbers are represented only approximately = = = = ? Floating point primitive types floatfour bytes 1.4e-45 to e38 doubleeight4.9e-324 to e308 5 Try 0.3 – 0.2 in the BlueJ Code Pad

In Java, a variable must be declared before it is used The declaration gives 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; int balance; b temp balance Declaring variables 6

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

The compiler will warn you if you attempt to use a “smaller-sized” variable than it thinks the value needs e.g. if you attempt float b; b = 12.35; the compiler will warn you of a “possible loss of precision”, because is regarded as a double If this is really what you want, you can cast the value float b; b = (float) ; For literals, there is a “quick cast” b = 12.35f; Typecasting 8

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 ch = ‘?’;// assignment Non-numeric primitive types 9

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

If a variable is given an initial value directly after it is declared, 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; Initialization 11

The Java compiler does extensive type-checking, to ensure that a program has no 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 Type-checking 12

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 creates 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 Variable declarations 13

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 these reasons, 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) Creating objects 14

The Heap b1 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 Garbage References 15

The behaviour of reference types and primitive types appears to be 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 The Golden Rule 16

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 ensure that a program has no obvious problems Creating objects is (almost always) done with the Java keyword new Every variable has a name that is used to refer to it Every variable has a value that can change during program execution Summary 17