CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
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.
Road Map Introduction to object oriented programming. Classes
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
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.
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.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Constructors & Garbage Collection Ch. 9 – Head First Java.
COMP Primitive and Class Types Yi Hong May 14, 2015.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Learners Support Publications Constructors and Destructors.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Constructors and Destructors
Primitive/Reference Types and Value Semantics
Strings, StringBuilder, and Character
Java Primer 1: Types, Classes and Operators
Object Oriented Programming
Multiple variables can be created in one declaration
The Building Blocks Classes: Java class library, over 1,800 classes:
Constructors and Destructors
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Java Programming Language
Review for Midterm 3.
Review of Java Fundamentals
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when Objects Become Eligible for Garbage Collection 1/15

 Local variables (method variables) live on the stack.  Objects and their instance variables live on the heap. 2

 Integer literals can be decimal, octal (e.g. 013), or hexadecimal (e.g. 0x3d).  Literals for longs end in L or l.  Float literals end in F or f, double literals end in a digit or D or d.  The boolean literals are true and false.  Literals for chars are a single character inside single quotes: 'd'. 3/15

 Scope refers to the lifetime of a variable.  There are four basic scopes:  Static variables live basically as long as their class lives.  Instance variables live as long as their object lives.  Local variables live as long as their method is on the stack; however, if their method invokes another method, they are temporarily unavailable.  Block variables (e.g., in a for or an if) live until the block completes. 4/15

 Literal integers are implicitly ints.  Integer expressions always result in an int-sized result, never smaller.  Floating-point numbers are implicitly doubles (64 bits).  Narrowing a primitive truncates the high order bits.  Compound assignments (e.g. +=), perform an automatic cast.  A reference variable holds the bits that are used to refer to an object.  Reference variables can refer to subclasses of the declared type but not to superclasses.  When creating a new object, e.g., Button b = new Button();, three things happen:  Make a reference variable named b, of type Button  Create a new Button object  Assign the Button object to the reference variable b 5/15

 When an array of objects is instantiated, objects within the array are not instantiated automatically, but all the references get the default value of null.  When an array of primitives is instantiated, elements get default values.  Instance variables are always initialized with a default value.  Local/automatic/method variables are never given a default value. If you  attempt to use one before initializing it, you'll get a compiler error. 6/15

 Methods can take primitives and/or object references as arguments.  Method arguments are always copies.  Method arguments are never actual objects (they can be references to objects).  A primitive argument is an unattached copy of the original primitive.  A reference argument is another copy of a reference to the original object.  Shadowing occurs when two variables with different scopes share the same name. This leads to hard-to- find bugs, and hard-to-answer exam questions. 7/15

 Arrays can hold primitives or objects, but the array itself is always an object.  When you declare an array, the brackets can be left or right of the name.  It is never legal to include the size of an array in the declaration.  You must include the size of an array when you construct it (using new) unless you are creating an anonymous array.  Elements in an array of objects are not automatically created, although primitive array elements are given default values.  You'll get a NullPointerException if you try to use an array element in an object array, if that element does not refer to a real object.  Arrays are indexed beginning with zero.  An ArrayIndexOutOfBoundsException occurs if you use a bad index value. 8/15

 Arrays have a length variable whose value is the number of array elements.  The last index you can access is always one less than the length of the array.  Multidimensional arrays are just arrays of arrays.  The dimensions in a multidimensional array can have different lengths.  An array of primitives can accept any value that can be promoted implicitly to the array's declared type;. e.g., a byte variable can go in an int array.  An array of objects can hold any object that passes the IS-A (or instanceof) test for the declared type of the array. For example, if Horse extends Animal, then a Horse object can go into an Animal array.  If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to.  You can assign an array of one type to a previously declared array reference of one of its supertypes. For example, a Honda array can be assigned to an array declared as type Car (assuming Honda extends Car). 9/15

 Static initialization blocks run once, when the class is first loaded.  Instance initialization blocks run every time a new instance is created. They run after all super-constructors and before the constructor's code has run.  If multiple init blocks exist in a class, they follow the rules stated above, AND they run in the order in which they appear in the source file. 10/15

 The wrapper classes correlate to the primitive types.  Wrappers have two main functions:  To wrap primitives so that they can be handled like objects  To provide utility methods for primitives (usually conversions)  The three most important method families are  xxxValue() Takes no arguments, returns a primitive  parseXxx() Takes a String, returns a primitive, throws NFE  valueOf() Takes a String, returns a wrapped object, throws NFE  Wrapper constructors can take a String or a primitive, except for Character, which can only take a char.  Radix refers to bases (typically) other than 10; octal is radix = 8, hex = /15

 As of Java 5, boxing allows you to convert primitives to wrappers or to convert wrappers to primitives automatically.  Using == with wrappers created through boxing is tricky; those with the same small values (typically lower than 127), will be ==, larger values will not be ==. 12/15

 Primitive widening uses the "smallest" method argument possible.  Used individually, boxing and var-args are compatible with overloading.  You CANNOT widen from one wrapper type to another. (IS-A fails.)  You CANNOT widen and then box. (An int can't become a Long.)  You can box and then widen. (An int can become an Object, via an Integer.)  You can combine var-args with either widening or boxing. 13/15

 In Java, garbage collection (GC) provides automated memory management.  The purpose of GC is to delete objects that can't be reached.  Only the JVM decides when to run the GC, you can only suggest it.  You can't know the GC algorithm for sure.  Objects must be considered eligible before they can be garbage collected.  An object is eligible when no live thread can reach it.  To reach an object, you must have a live, reachable reference to that object. 14/15

 Java applications can run out of memory.  Islands of objects can be GCed, even though they refer to each other.  Request garbage collection with System.gc(); (only before the SCJP 6).  Class Object has a finalize() method.  The finalize() method is guaranteed to run once and only once before the garbage collector deletes an object.  The garbage collector makes no guarantees, finalize() may never run.  You can uneligibilize an object for GC from within finalize(). 15/15