Variables in Java A variable holds either

Slides:



Advertisements
Similar presentations
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
Expressions, Data Conversion, and Input
Class Library, Formatting, Wrapper Classes, and JUnit Testing
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
CSE 1201 Object Oriented Programming Using Classes and Objects.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
1 Wrapper Classes  Sometimes we want to use a primitive type as an object, so there are wrapper classes that will help us.  In particular, we need to.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
3-1 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
1 CMPT 126 Java Basics Using Classes and Objects.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5: Enhancing Classes
Formatting Output & Enumerated Types & Wrapper Classes
Using Classes and Objects
CSC 1051 – Data Structures and Algorithms I
Data Conversion & Scanner Class
Creating Objects & String Class
Classes, Libraries & Packages
Chapter 3: Using Classes and Objects
Using Classes and Objects
Objects and Primitive Data
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Packages & Random and Math Classes
Outline Creating Objects The String Class The Random and Math Classes
Drawing Shapes (Graphics Class) Creating Objects Packages
Presentation transcript:

Variables in Java A variable holds either Primitive value int num; Or an object Scanner scan; Once declared => un-initialized - num - scan

Object declaration Variable referring to an object must be declared with the class name as the object type String name; => name is an object reference variable No object is created with this declaration doesn’t hold an object, but a reference to the object can be set to null => does not refer to an object name = null;

Creating an object Generally, we use the new operator To create an object Creating an object is called instantiation Returns a reference to the newly created object The object is said to be an instance of a particular class name = new String (“James Bond"); This calls the String constructor, which is a special method that sets up the object

Reference variables Object reference variable name “James Bond” Object reference variable Pointer to the location of the created object in memory uses the dot operator to access the objects methods int count ; count = name.length( ); A method invocation can be thought of As asking an object to perform a service Appended after the object reference. Method being invoked

Shortcuts Initializing the variable in the declaration String title = new String(“JAVA software solution”); Whenever a string literal appears A String object is created automatically => String city = “London”; // statement is VALID For String, the explicit use of new can be eliminated

Assignment revisited The act of assignment Before: After: Takes a copy of a value and stores it in a variable For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 After:

Reference assignment For object references Before: After: Assignment copies the address name1 name2 Before: "Steve Jobs" "Steve Wozniak" name2 = name1; name1 name2 After: "Steve Jobs"

Aliases Effect of assignment on primitive values Effect of assignment on object references 5 12 num1 num2 num1 num1 = num2; 12 num2 name1 “string1” aliases name1 “string1” name2 = name1; “string2” name2 name2

Aliases (cont’d) 2 or more references referring to same object Are called aliases of each other => multiple reference variable can be used To access the object Changing an object thru one reference Changes it for all of its aliases Because there is really only one object

Garbage collection When an object In other languages (like C++) no longer has any valid references to it It becomes useless, and therefore is called garbage Java performs automatic garbage collection periodically Returning an object’s memory to the system for future use In other languages (like C++) The programmer is responsible for Performing garbage collection

Background information Keep in mind Object reference variable stores an address interaction with an object occurs via reference variables You can use a variable, ONLY if you have reference to it When last reference to object is lost Object can no longer contribute to the program At this point, the object is called garbage Occasionally, JAVA collects all garbage and free memory

Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

Class library JAVA class library Provides support when developing JAVA programs includes classes containing valuable methods String class is part of the JAVA standard class library was created by employees at Sun (who created JAVA) is made up of clusters of related classes Called JAVA APIs, or application programming interfaces

Packages Classes of the JAVA standard library are grouped into packages Each class is part of a particular package String is part of the java.lang package Scanner is part of the java.util package Different packages exist in JAVA Package java.lang java.applet java.awt javax.swing java.net Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication

import declaration To use classes from any package, either Fully qualify the reference including the package name, Every time it is referenced java.util.Scanner scan = new java.util.Scanner(System.in) This however becomes tiring!!

import declaration Or, use an import declaration To simplify these references Import the class, and then use just the class name import java.util.Scanner Refer to Import_example.java to import all classes in a particular package You can use the * wildcard character import java.util.*;

Different forms of import declaration Import uses an asterisk to indicate Any class inside the package might be used import java.util.*; the name of the particular class If only one class of a certain package is needed import java.util.Scanner;

Java.lang package import java.lang.*; All classes of the java.lang package are imported automatically into all programs It is as if all programs contain the following line That is why we didn’t have to import String classes The Scanner class Is part of java.util package And therefore must be imported import java.lang.*;

Java.lang java.lang package Automatically imported Fundamental, thought of as basic extension of language Therefore Any class in java.lang package can be used Such as System, String, etc… Without an explicit import declaration Any program contains: import java.lang.*

JAVA 10 commandments You must declare every identifier that is not a JAVA reserved word Not doing so results in an error message JAVA is a case-sensitive language so two identifiers that are capitalized differently are treated differently Check for mismatched quotes in char literals Each char literal starts and ends with an apostrophe In an assignment statement, make sure that the identifier to the left of = is a variable and not a named constant

JAVA 10 commandments (cont’d) Make sure your statements end with semicolons The file name that holds the program must be the same as the name of the public class but with the extension .java If the public class is named Driver => file: Driver.java Be careful when using the /* */ to delimit comments If you forget the */, then everything that follows will be treated as a comment Confirm that every open brace { in the program is matched by a close brace }

JAVA 10 commandments (cont’d) To print a double quote within a literal string, use the symbol \” Make sure that the application class and main are public

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Interactive programs Programs generally It is often useful That way, Need input on which to operate It is often useful To design a program that reads data interactively During execution That way, New results are computed each time the program is run Depending on the data that is entered

The Scanner class How to design programs Scanner class reading data from the user interactively? Scanner class provides convenient methods for reading input values of various types Can be setup to read input from the keyboard Keyboard input is represented by the System.in object

Reading input First, you create a Scanner object The following line creates the Scanner object That reads from the keyboard The new operator creates the Scanner object Use the Scanner object to invoke methods answer = scan.nextLine(); Reads the input line and returns it as one String scan.nextInt(), scan.nextDouble Used to read data of particular types Scanner scan = new Scanner (System.in);

Example See Echo.java The Scanner class is part of the java.util library that must be imported into a program to use Scanner Methods such as nextInt()and nextDouble() Read data of particular types See GasMileage.java