Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6.

Similar presentations


Presentation on theme: "Chapter 6."— Presentation transcript:

1 Chapter 6

2 6.1.1 Use of the System class for input and output
Data traveling to and from objects is called a stream Stream objects can be used to represent and control the flow of data java.lang.System has 3 stream objects that provide basic input/output capabilities

3 6.1.2 Input and output using java.lang.System
3 streams of basic I/O Standard Output (stdout) is used to send messages to the console Standard Error (stderr) is used to send messages to the error logging device Standard Input (stdin) is used to read input from the keyboard

4 6.2 Strings No string primitive
Created by assigning a reference to a literal, or by instantiating a String object String name = “Jody”; String name = new String(“Jody”); Strings are immutable – They can never change A reference to a String can be assigned to another string

5 6.2.1 String storage & methods
If a String is created using a literal, it is stored in the String pool If a String object is instantiated, it is stored on the heap The String class has many useful methods The ‘+’ operator can be used to concatenate strings Use the String.equals() method to test for equality. Using ‘==‘ compares the reference variables

6 6.2.3 Casting, conversion & StringBuffer
Primitive values can be assigned to a String reference by concatenating the primitive with a literal String int x = 5; string s1 = “” + x; Objects are converted to a String using their “toString()” method The StringBuffer class can be used to manage strings that need to change StringBuffer contains methods for changing the string

7 6.3.1 StringBuffer methods The StringBuffer class can be used to manage strings that need to change StringBuffer contains methods for changing the string

8 6.5.1 Introduction to wrapper classes
Java does not treat primitives as objects Wrapper classes exist to “wrap” the primitive in an object Wrapper class have methods useful with primitives Wrapper classes are immutable Many wrapper methods are static

9 6.6.1 The Math class The Math class is useful for performing mathematical functions Methods are static, so no instance is required The BigNumber and BigDecimal classes are used to store very large values

10 6.8.1 Creating dates 3 classes for storing and manipulating dates
java.util.Date java.util.Calendar java.text.DateFormat Date represents a date, Calendar sets or changes the date, and DateFormat is used to display the Date in different formats Use DateFormat.parse() to obtain an instance of Date Date someDate; DateFormat df = DateFormat.getInstance(DateFormat.FULL, Locale.US); someDate = rf.parse(“Monday, May 30, 2002”);

11 6.8.2 Setting dates Date methods that change the date should not be used The Calendar class can be used to set the date

12 6.8.3 Formatting dates The DateFormat class can be used to convert a Date in to a readable String

13 6.8.4 Random numbers The static Math.random() method returns a double between 0.0 and 1.0 The java.util.Random class can be used to work with random numbers of int, long, float or double Random number generators require a seed. If the same seed is used, the results will be the same The default constructor of the Random class uses the system clock as the seed


Download ppt "Chapter 6."

Similar presentations


Ads by Google