Comp1004: Programming in Java I Variables - Primitives, Objects and Scope.

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

Outline Java program structure Basic program elements
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Understanding class definitions Looking inside classes.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Writing Classes (Chapter 4)
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
What does a computer program look like: a general overview.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Chapter 4 Introduction to Classes, Objects, Methods and strings
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
OOP Basics Classes & Methods (c) IDMS/SQL News
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Comp1004: Conclusions Revision Session. Coming up Recap – The Basics – The Pillars – The Extras Feedback + Course Evaluation Structure of the Exam Some.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Introduction CMSC 202 Fall Instructors Mr. Ryan Bergeron – Lecture Section 01 Tues/Thu 1:00 – 2:15 am, Sondheim 111 – Lecture Section 04 Tues/Thu.
Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming.
The need for Programming Languages
Computer Organization and Design Pointers, Arrays and Strings in C
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter 7 User-Defined Methods.
Content Programming Overview The JVM A brief look at Structure
Computer Programming Methodology Introduction to Java
Chapter 2.
An Introduction to Java – Part I, language basics
Comp1202: Introduction III
Encapsulation and Constructors
Sridhar Narayan Java Basics Sridhar Narayan
Chapter 2 Programming Basics.
LCC 6310 Computation as an Expressive Medium
C Programming Lecture-17 Storage Classes
Variables and Constants
Coming up Variables Quick look at scope Primitives Objects
Object-Oriented Programming and class Design
Presentation transcript:

Comp1004: Programming in Java I Variables - Primitives, Objects and Scope

Coming up Quick Recap: – Objects vs. Classes – If/else Variables: Primitives and Objects – How they are Defined – How they are Stored – How they are Passed Introduction to Scope

Recap

Classes and Objects 1 Class 101 Objects

The JVM So the JVM is software that allows your Java program to run on any platform where there is a JVM The JVM? Your Program The Machine

Dog.java Structure public class Dog { public String noise = “Woof”; public void bark() { System.out.println(noise); } Source file Class Member Variable Method Statement

The main method Creates the objects you need and tells them what to do, a bit like a conductor in an orchestra It doesn’t matter which class you put the main method in. You can put it in its own one if you like – But there must be 1 and only 1 in the whole program (program is a set of classes that work together) Dog.java public class Dog { public String noise = “Woof”; public void bark() { System.out.println(noise); } public static void main(String[] args){ Dog d = new Dog(); d.bark(); }

if/else public class Account{ int balance;//the bank balance boolean active;// true if the account is active active = true;//set active to true //some code omitted public void withdrawFiver{ if(!active){ System.out.println(“Your account isn’t active”); System.out.println(“No withdrawal allowed”); }else { balance = balance - 5; }

Variables: Primitives and Objects

Variables We know that variables store ‘things’ like ints, booleans and Elephants Variables that are part of a class are called member variables – Sometimes called properties or fields Variables can store a primitive type Variables can store an object reference

Primitive TypeObject Reference Defined? Stored? Passed? Primitive Types are used for simple pieces of Data Object References are used for handling Objects They differ in how they are defined, stored and passed

PrimitivesObjects Defined?defined in Java Stored? Passed? Primitive types are for simple data, e.g: – intwhole numbers – floatfloating point numbers ( i.e. decimals) – chara single character – booleantrue or false They are an integral part of the Java Language. We just use them

PrimitivesObjects Defined?defined in Javadefined in Classes Stored? Passed? Objects can be of a type defined by: – a class you write – a class someone else writes Java includes a library of useful classes, ones that can read files on your computer or output sounds and many other tasks. We use these a lot later on.

A variable is a space in memory that the computer uses to store a value It’s like a cup int myNumber; myNumber = 7; A primitive ‘fits into’ a cup myNumber int PrimitivesObjects Defined?defined in Javadefined in Classes Stored?stored directly in variables Passed?

An object does not fit into a cup... Elephant nellie; nellie = new Elephant(); So what does Java do? nellie Elephant?!?! PrimitivesObjects Defined?defined in Javadefined in Classes Stored?stored directly in variablesa reference is stored in the variable Passed?

nellie Elephant?!?! PrimitivesObjects Defined?defined in Javadefined in Classes Stored?stored directly in variablesa reference is stored in the variable Passed? Java leaves the elephant object in memory somewhere... And references it – like using a student ID to refer to a student

nellie Elephant Reference PrimitivesObjects Defined?defined in Javadefined in Classes Stored?stored directly in variablesa reference is stored in the variable Passed?

int a; a = 10; int b; b = 5; int c; c = a; c = 2*c; b = a*c; a int b c PrimitivesObjects Defined?defined in Javadefined in Classes Stored?stored directly in variablesa reference is stored in the variable Passed?Pass by copy

Elephant a; a = new Elephant(); Elephant b = new Elephant(); Elephant c; c = a; a = b; c = null; a Elephant b c PrimitivesObjects Defined?defined in Javadefined in Classes Stored?stored directly in variablesa reference is stored in the variable Passed?Pass by copyPass by reference Garbage collected

Introduction to Scope

A word about Local variables Member variables (object properties) are one sort of variable. – They store values through the life of an object. – They are accessible in any method in the class. Methods can include shorter-lived local variables. – They exist only as long as the method is being executed. – They are only accessible from within the method. This is called scope. It is covered in detail later in the course.

The rule of thumb......is that a variable can be seen anywhere within the {} that it was declared in

public class Account{ int balance = 100; public void withdrawFiver(){ balance = balance -5; } }

public class Account{ int balance = 100; public void withdrawFiver(){ balance = balance -5; } } Balance is a member variable, and is visible in every method of the class

public class Account{ int balance = 100; public void withdrawFiver(){ balance = balance -5; } public void withdrawTenner(){ int tenner = 10; balance = balance – tenner; }

public class Account{ int balance = 100; public void withdrawFiver(){ balance = balance -5; } public void withdrawTenner(){ int tenner = 10; balance = balance – tenner; } Tenner is a local variable, it only exists for the duration of the method in which it is declared.

public class Account{ int balance = 100; public void withdrawFiver(){ balance = balance -5; } public void withdrawTenner(){ int tenner = 10; balance = balance – tenner; } public void withdrawFifty(){ balance = balance – (tenner * 5); }

public class Account{ int balance = 100; public void withdrawFiver(){ balance = balance -5; } public void withdrawTenner(){ int tenner = 10; balance = balance – tenner; } public void withdrawFifty(){ balance = balance – (tenner * 5); } Because tenner is a local variable, it cannot be seen in any other methods – so the withdrawFifty method shown here will not compile

public class Account{ int balance = 100; public void withdrawFiver(){ balance = balance -5; } public void withdrawTenner(){ int tenner = 10; balance = balance – tenner; } public void withdrawFifty(){ balance = balance – (tenner * 5); } public void closeAccount(){ int balance = 0; }

public class Account{ int balance = 100; public void withdrawFiver(){ balance = balance -5; } public void withdrawTenner(){ int tenner = 10; balance = balance – tenner; } public void withdrawFifty(){ balance = balance – (tenner * 5); } public void closeAccount(){ int balance = 0; } Here we get a conflict because balance has been declared twice, in the class as a member variable and in the closeAccount method as a local variable. Any modifications to balance in closeAccount will default to the local version, and the member variable will remain unchanged

public class Account{ int balance = 100; public void withdrawFiver(){ balance = balance -5; } public void withdrawTenner(){ int tenner = 10; balance = balance – tenner; } public void withdrawFifty(){ balance = balance – (tenner * 5); } public void closeAccount(){ int balance = 0; } Balance is a member variable. It has class scope (can be seen in every method of the class)

public class Account{ int balance = 100; public void withdrawFiver(){ balance = balance -5; } public void withdrawTenner(){ int tenner = 10; balance = balance – tenner; } public void withdrawFifty(){ balance = balance – (tenner * 5); } public void closeAccount(){ int balance = 0; } Tenner (and the second balance variable) are local variables. They have local scope (can only be seen in the method in which they are declared)

Summary Quick Recap: – Objects vs. Classes – If/else Variables: Primitives and Objects – How they are Defined – How they are Stored – How they are Passed Introduction to Scope