SlideShare une entreprise Scribd logo
1  sur  30
KNOW YOUR VARIABLES So far we have discussed variables in two contexts: As object state (instance variables) As local variables (variables declared within a method) Now we continue to discuss variables as: Arguments (values sent to a method by the calling code) Return types (values sent back to the caller of the method) LIS4930 © PIC
Declaring a variable LIS4930 © PIC Java cares about type. It won’t let you do something bizarre or dangerous, like stuff a Bicycle reference into a Dog variable – what would happen if someone tries to ask the bicycle to bark()? For the compiler to catch these potential problems, Java requires us to declare the type of the variable. Is it an integer? a Dog? a single Character? Variables come in two flavors: primitives object references
Declaring Variables LIS4930 © PIC You must follow two declaration rules: variables must have a type 1 variables must have a name 2 type name int count;
Think of Cups! LIS4930 © PIC Think of Java variables as different types of cups. Coffee cups, tea cups, beer cups, massive popcorn cups, Double Gulp cups.
“I’d like a tall Mocha, no make that an int.” LIS4930 © PIC A variable is just a cup. 	A container. 	It holds something. 	It has a size and a type. tall venti short grande When you go to Starbucks you know they have different sized cups, so you order what you want and tell them the size you would like. Just as they write your name on your cup in Starbucks, Java also requires you to give your variable a name. int short long byte In Java, primitives come in different sizes, and those sizes have names. The four containers here are for the four integer primitives in Java.
Java Primitive Types LIS4930 © PIC Note the ‘f’. Java assumes anything with a floating point is a double otherwise.
Avoid Spillage! LIS4930 © PIC Now that you know the limits of each of the primitive Java types it is important not to assign a variable of a bigger type into a smaller one. This would be called spillage. And the compiler will complain! For example: intx = 24; 	byte b = x;	//won’t work! You can assign  a value to a variable in one of several ways including: ,[object Object]
 assign the value of one variable to another (x = y)
 use an expression combining the two (x = y + 43),[object Object]
boolean boo = x;
intg = 17;
inty = g;
y = y + 10;
short s;
s = y;
byte b = 3;
byte v = b;
short n = 12;
v = n;
byte k = 128;,[object Object]
Reserved Words LIS4930 © PIC public 	static 	void boolean	char		byte		short	int	long	float	double …or any of these! (don’t memorize these) Don’t use any of these for you own variable names  (memorize these) Even if you don’t need to know what they mean, you still need to know you can’t use them yourself.
Remote Controlling Your Dog! LIS4930 © PIC You know how to declare a primitive variable and assign it a value. But now what about non-primitive variables? In other words, what about objects? ,[object Object]
 There’s only an object reference variable.
 An object reference variable holds bits that represent a way to 	access an object.
 It doesn’t hold the object itself, but it holds something like a 	pointer,  or mapping to where the object is stored on the 	heap. We do know that whatever it is, it represents one 	and only one object. The JVM knows how to use the 	reference to get to the object.,[object Object]
An Object Reference is Just Another Variable Value LIS4930 © PIC Reference Variable Dog rufus = new Dog( ); The Dog object itself does not go into the Variable! Primitive Variable byte x = 7; The bits representing 7 go into the variable. (00000111) 00000111 int Dog
3 Steps To Making An Object LIS4930 © PIC Declare a reference variable 1 Dog rufus= new Dog( ); Dog rufus = new Dog( ); Dog rufus=new Dog( ); Create an object 2 Link the object and the reference 3
What else about Object References? LIS4930 © PIC Can a reference variable be programmed to refer to something else – can a Dog reference variable be programmed to reference a Cat object? Can a Dog reference variable be programmed to reference a different Dog object? What about objects marked as final? Can a reference variable ever refer to nothing at all? Is null a value? If an object is the only reference to a particular object, and then its set to null (deprogrammed), it means that now nobody can get to that object it once referred to.
Life On The Garbage-Collectible Heap LIS4930 © PIC b Book b = new Book(); 1 Book c = new Book(); Book object Book HEAP Book object 2 c References: 2 Objects: 2 Book
Life On The Garbage-Collectible Heap LIS4930 © PIC b Book d = c; 1 Book object Book HEAP Book object 2 c d Book References: 3 Objects: 2 Book

Contenu connexe

Tendances

Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Threeamiable_indian
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxingGeetha Manohar
 
String handling session 5
String handling session 5String handling session 5
String handling session 5Raja Sekhar
 
Function Java Vector class
Function Java Vector classFunction Java Vector class
Function Java Vector classNontawat Wongnuk
 
Writer Monad for logging execution of functions
Writer Monad for logging execution of functionsWriter Monad for logging execution of functions
Writer Monad for logging execution of functionsPhilip Schwarz
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)teach4uin
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and StringsEduardo Bergavera
 
Solid Deconstruction
Solid DeconstructionSolid Deconstruction
Solid DeconstructionKevlin Henney
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answersRojaPriya
 
Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Peter Antman
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with PythonSushant Mane
 
Definitions of Functional Programming
Definitions of Functional ProgrammingDefinitions of Functional Programming
Definitions of Functional ProgrammingPhilip Schwarz
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And AnswersH2Kinfosys
 

Tendances (20)

Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
 
M C6java7
M C6java7M C6java7
M C6java7
 
Function Java Vector class
Function Java Vector classFunction Java Vector class
Function Java Vector class
 
05 object behavior
05 object behavior05 object behavior
05 object behavior
 
Java strings
Java   stringsJava   strings
Java strings
 
Python Fundamentals Class 11
Python Fundamentals Class 11Python Fundamentals Class 11
Python Fundamentals Class 11
 
Writer Monad for logging execution of functions
Writer Monad for logging execution of functionsWriter Monad for logging execution of functions
Writer Monad for logging execution of functions
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Solid Deconstruction
Solid DeconstructionSolid Deconstruction
Solid Deconstruction
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)
 
Strings
StringsStrings
Strings
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with Python
 
Definitions of Functional Programming
Definitions of Functional ProgrammingDefinitions of Functional Programming
Definitions of Functional Programming
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
 

En vedette (9)

15a gui
15a gui15a gui
15a gui
 
Sdlc
SdlcSdlc
Sdlc
 
12 abstract classes
12 abstract classes12 abstract classes
12 abstract classes
 
07 java api and inheritance
07 java api and inheritance07 java api and inheritance
07 java api and inheritance
 
Mysocial
MysocialMysocial
Mysocial
 
03 objects
03 objects03 objects
03 objects
 
14a exceptions
14a exceptions14a exceptions
14a exceptions
 
06a methods original
06a methods original06a methods original
06a methods original
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 

Similaire à 04 variables (20)

04 Variables
04 Variables04 Variables
04 Variables
 
12 constructors
12 constructors12 constructors
12 constructors
 
11 polymorphism
11 polymorphism11 polymorphism
11 polymorphism
 
09 polymorphism
09 polymorphism09 polymorphism
09 polymorphism
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Chapter - 3.pptx
Chapter - 3.pptxChapter - 3.pptx
Chapter - 3.pptx
 
Python-04| Fundamental data types vs immutability
Python-04| Fundamental data types vs immutabilityPython-04| Fundamental data types vs immutability
Python-04| Fundamental data types vs immutability
 
Python basics
Python basicsPython basics
Python basics
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive Wrappers
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2
 
P1
P1P1
P1
 
10 abstract
10 abstract10 abstract
10 abstract
 
03 Variables - Chang.pptx
03 Variables - Chang.pptx03 Variables - Chang.pptx
03 Variables - Chang.pptx
 
DEF CON 27 - workshop - EIGENTOURIST - hacking with monads
DEF CON 27 - workshop - EIGENTOURIST - hacking with monadsDEF CON 27 - workshop - EIGENTOURIST - hacking with monads
DEF CON 27 - workshop - EIGENTOURIST - hacking with monads
 
13 interfaces
13 interfaces13 interfaces
13 interfaces
 
11 interfaces
11 interfaces11 interfaces
11 interfaces
 
Data handling CBSE PYTHON CLASS 11
Data handling CBSE PYTHON CLASS 11Data handling CBSE PYTHON CLASS 11
Data handling CBSE PYTHON CLASS 11
 

Plus de Program in Interdisciplinary Computing (20)

Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
Database basics
Database basicsDatabase basics
Database basics
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
01 intro tousingjava
01 intro tousingjava01 intro tousingjava
01 intro tousingjava
 
Xhtml
XhtmlXhtml
Xhtml
 
Webdev
WebdevWebdev
Webdev
 
Web architecture
Web architectureWeb architecture
Web architecture
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Html5
Html5Html5
Html5
 
Frameworks
FrameworksFrameworks
Frameworks
 
Drupal
DrupalDrupal
Drupal
 
Database
DatabaseDatabase
Database
 
Javascript2
Javascript2Javascript2
Javascript2
 
15b more gui
15b more gui15b more gui
15b more gui
 
14b exceptions
14b exceptions14b exceptions
14b exceptions
 
13 life and scope
13 life and scope13 life and scope
13 life and scope
 

04 variables

  • 1. KNOW YOUR VARIABLES So far we have discussed variables in two contexts: As object state (instance variables) As local variables (variables declared within a method) Now we continue to discuss variables as: Arguments (values sent to a method by the calling code) Return types (values sent back to the caller of the method) LIS4930 © PIC
  • 2. Declaring a variable LIS4930 © PIC Java cares about type. It won’t let you do something bizarre or dangerous, like stuff a Bicycle reference into a Dog variable – what would happen if someone tries to ask the bicycle to bark()? For the compiler to catch these potential problems, Java requires us to declare the type of the variable. Is it an integer? a Dog? a single Character? Variables come in two flavors: primitives object references
  • 3. Declaring Variables LIS4930 © PIC You must follow two declaration rules: variables must have a type 1 variables must have a name 2 type name int count;
  • 4. Think of Cups! LIS4930 © PIC Think of Java variables as different types of cups. Coffee cups, tea cups, beer cups, massive popcorn cups, Double Gulp cups.
  • 5. “I’d like a tall Mocha, no make that an int.” LIS4930 © PIC A variable is just a cup. A container. It holds something. It has a size and a type. tall venti short grande When you go to Starbucks you know they have different sized cups, so you order what you want and tell them the size you would like. Just as they write your name on your cup in Starbucks, Java also requires you to give your variable a name. int short long byte In Java, primitives come in different sizes, and those sizes have names. The four containers here are for the four integer primitives in Java.
  • 6. Java Primitive Types LIS4930 © PIC Note the ‘f’. Java assumes anything with a floating point is a double otherwise.
  • 7.
  • 8. assign the value of one variable to another (x = y)
  • 9.
  • 13. y = y + 10;
  • 16. byte b = 3;
  • 17. byte v = b;
  • 18. short n = 12;
  • 20.
  • 21. Reserved Words LIS4930 © PIC public static void boolean char byte short int long float double …or any of these! (don’t memorize these) Don’t use any of these for you own variable names (memorize these) Even if you don’t need to know what they mean, you still need to know you can’t use them yourself.
  • 22.
  • 23. There’s only an object reference variable.
  • 24. An object reference variable holds bits that represent a way to access an object.
  • 25.
  • 26. An Object Reference is Just Another Variable Value LIS4930 © PIC Reference Variable Dog rufus = new Dog( ); The Dog object itself does not go into the Variable! Primitive Variable byte x = 7; The bits representing 7 go into the variable. (00000111) 00000111 int Dog
  • 27. 3 Steps To Making An Object LIS4930 © PIC Declare a reference variable 1 Dog rufus= new Dog( ); Dog rufus = new Dog( ); Dog rufus=new Dog( ); Create an object 2 Link the object and the reference 3
  • 28. What else about Object References? LIS4930 © PIC Can a reference variable be programmed to refer to something else – can a Dog reference variable be programmed to reference a Cat object? Can a Dog reference variable be programmed to reference a different Dog object? What about objects marked as final? Can a reference variable ever refer to nothing at all? Is null a value? If an object is the only reference to a particular object, and then its set to null (deprogrammed), it means that now nobody can get to that object it once referred to.
  • 29. Life On The Garbage-Collectible Heap LIS4930 © PIC b Book b = new Book(); 1 Book c = new Book(); Book object Book HEAP Book object 2 c References: 2 Objects: 2 Book
  • 30. Life On The Garbage-Collectible Heap LIS4930 © PIC b Book d = c; 1 Book object Book HEAP Book object 2 c d Book References: 3 Objects: 2 Book
  • 31. Life On The Garbage-Collectible Heap LIS4930 © PIC b c = b; 1 Book object Book HEAP X Book object 2 c d Book References: 3 Objects: 2 Book
  • 32. Life and Death on the Heap LIS4930 © PIC b Book b = new Book(); 1 Book c = new Book(); Book object Book HEAP Book object 2 c References: 2 Objects: 2 Book
  • 33. LIS4930 © PIC Life and Death on the Heap This guy is toast. b b= c; 1 Book object Book HEAP Book object 2 c Active References: 2 Reachable Objects: 1 Abandoned Objects: 1 Book
  • 34. LIS4930 © PIC Life and Death on the Heap Still toast. b c = null; 1 Book object Book HEAP Book object X 2 c Active References: 1 null References: 1 Reachable Objects: 1 Abandoned Objects: 1 Book
  • 35. Array Variables LIS4930 © PIC An array variable is a remote to an array object. Declare an intarray variable. int[ ] nums; 1 Create a new int array with a length of 6, and assign it to the previously declared int[] variable nums nums = new int[6]; Give each elements in the array an intvalue. Remember, elements in an intarray are just intvariables. nums[0] = 6; nums[1] = 19; nums[2] = 44; nums[3] = 42; nums[4] = 10; nums[5] = 20; 2 3
  • 36. Array Variables LIS4930 © PIC 44 42 10 19 6 20 int int int int int int int[ ] 4 2 0 5 3 1 int array object (int[ ]) nums NOTE: The array is an object, even though it’s an array of primitives.
  • 37. LIS4930 © PIC Array Variables Every element in an array is just a variable. In other words, one of the eight primitive variable types or a reference variable. Anything you would put in a variable of that type can be assigned to an array element of that type. So in an array of type int (int[ ]), each element can hold an int. So in a Dog array, each element can hold a remote control to a Dog. Arrays are always objects, whether they’re declared to hold primitives or object references. The array itself is never a primitive.
  • 38. Make an Array of Dogs LIS4930 © PIC Declare an Dog array variable. Dog[ ] pets; Create a new Dog array with a length of 6, and assign it to the previously declared Dog[ ] variable pets. pets = new Dog[6]; 1 2 Dog Dog Dog Dog Dog Dog Dog[ ] 4 2 0 5 3 1 Dog array object (Dog[ ]) pets What’s Missing?
  • 39. Make an Array of Dogs LIS4930 © PIC Create new Dog objects, and assign them to the array elements. Remember, elements in a Dog array are just Dog reference variables. We still need Dogs!!! pets[0] = new Dog; pets[1] = new Dog; pets[2] = new Dog; 3 Dog objects Dog[ ] Dog Dog Dog Dog Dog Dog pets 4 2 0 5 3 1 Dog array object (Dog[ ])
  • 40. Control Your Dog LIS4930 © PIC name Dog rufus = new Dog(); rufus.name = “Rufus”; String Dog object Dog rufus name bark() eat() chaseCat() What happens if the Dog is in a Dog array? Dog
  • 41. Dog Array LIS4930 © PIC Dog[ ] myDogs = new Dog[3]; myDogs[0] = new Dog(); myDogs[0].name = “Rufus”; myDogs[0].bark(); Dog name Dog Remote bark() eat() chaseCat() When the Dog is in an array, we don’t have an actual variable name (like rufus). Instead we use array notation and push the remote control button (dot operator) on an object at a particular index (position) in the array. Eg: myDogs[1].bark(); chaseCat bark eat myDogs[0]
  • 42. Java Cares About Type LIS4930 © PIC Once you’ve declared an array, you can’t put anything in it except things that are of the declared array type. You can’t put a Cat into a Dog array, or a double into an int array (spillage, remember?). You can, however, put a byte into an intarray, because a byte willalways fit into an int-sized cup. This is known as implicit widening. We’ll get into the details later, but for now just remember that the compiler won’t let you put the wrong thing in an array, based on the array’s declared type. I don’t’ belong here.
  • 43. Dog Example LIS4930 © PIC Do you understand everything in this code? What does this mean? Dog name bark() eat() chaseCat()

Notes de l'éditeur

  1. When you see a statement like: “an object of type X”, think of type and class as synonyms.