290 likes | 334 Views
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;
E N D
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 • String name; 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
Appended after the object reference. Method being invoked Reference variables name • 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 “James Bond”
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
38 num1 Before: 96 num2 38 num1 After: 38 num2 Assignment revisited • The act of assignment • Takes a copy of a value and stores it in a variable • For primitive types: num2 = num1;
"Steve Jobs" name1 Before: "Steve Wozniak" name2 "Steve Jobs" name1 After: name2 Reference assignment • For object references • Assignment copies the address name2 = name1;
12 num1 name2 = name1; num1 = num2; 12 num2 aliases Aliases • Effect of assignment on primitive values • Effect of assignment on object references 5 num1 12 num2 name1 “string1” name1 “string1” “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 • 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
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 Packages • Classes of the JAVA standard library • are grouped into packages • Each class is part of a particular package • Stringis part of the java.lang package • Scanneris part of the java.util package • Different packages exist in JAVA
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 • 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 • 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 • 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