Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables in Java A variable holds either

Similar presentations


Presentation on theme: "Variables in Java A variable holds either"— Presentation transcript:

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

2 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;

3 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

4 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

5 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

6 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:

7 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"

8 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

9 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

10 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

11 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

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

13 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

14 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

15 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!!

16 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.*;

17 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;

18 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.*;

19 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.*

20 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

21 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 }

22 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

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

24 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

25 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

26 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);

27 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


Download ppt "Variables in Java A variable holds either"

Similar presentations


Ads by Google