java'lang and java'util - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

java'lang and java'util

Description:

A wrapper is a class (complex type) designed to hold a primitive ... Immutability of Strings. Whenever you create a String, it is read only (you can't change it! ... – PowerPoint PPT presentation

Number of Views:112
Avg rating:3.0/5.0
Slides: 24
Provided by: jeffch8
Category:
Tags: immutability | java | lang | util

less

Transcript and Presenter's Notes

Title: java'lang and java'util


1
java.lang and java.util
2
Overview
  • java.lang and java.util are packages that contain
    some important classes
  • no need to import java.lang (free)
  • Inside these packages are
  • Wrappers
  • Math
  • Strings
  • Collections

3
The Wrapper Classes
  • A wrapper is a class (complex type) designed to
    hold a primitive
  • Primitives are passed by value and classes are
    pass by reference
  • Wrappers provide functionality for converting
    data types (parseInt)

4
The Wrapper Classes
  • Boolean - hold a boolean
  • Byte - holds a byte
  • Character - holds a char
  • Double - holds a double
  • Float - holds a float
  • Integer - holds an int
  • Long - holds a long
  • Short - holds a short

5
Using Wrapper Classes
  • Usually have 2 constructors
  • one with primitive data type
  • one with a String passed
  • Character class has one constructor
  • To extract value, use the Xvalue( ), where X is
    the name of the primitive
  • public byte byteValue( )
  • Wrappers override equals( )

6
Math Class
  • Has several static methods
  • abs( ) returns absolute value of a number (ignore
    sign)
  • ceil( ) returns the ceiling of a number (round up
    no matter what!)
  • floor( ) returns the floor of a number (round
    down no matter what)
  • max( ) takes in two numbers and returns the
    greater of the two

7
Math Class
  • Static methods continued
  • min( ) returns the minimum of two numbers
  • random( ) returns a value between 0.0d and 1.0d
  • round( ) finds the closest integer to that number
    (.5 and above)
  • sqrt( ) returns the square root of a positive
    number
  • sin( ), cos( ), tan( ) are trig functions

8
String Operators
  • You can use and operators on Strings
  • Example
  • String s Jeff rox
  • s the world
  • Another example
  • s 5

9
Immutability of Strings
  • Whenever you create a String, it is read only
    (you cant change it!)
  • Before we saw the and operators!?
  • These operators actually create a new String!

10
Pools of Strings
  • There are 2 ways to create Strings in Java
  • Using new
  • Using literals
  • Each Java class has a String pool for literals
  • Eliminates duplicate copies
  • So if we said
  • String s1 Jeff String s2 Jeff
  • String s3 Jeff String s4 Jeff
  • We would only open up enough space for 1 String,
    and s1, s2, s3, and s4 would point to it!

11
Pools continued
  • Look at this code
  • String s1 Jeff
  • String s2 Jeff
  • if (s1 s2) System.out.println (Yes!)
  • s1 and s2 point to same String in pool

12
Pool Quiz
  • String s1, s2
  • s1 Jeff
  • s2 new String (Jeff)
  • if (s1 s2)
  • System.out.println ( )
  • if (s1.equals(s2) )
  • System.out.println (equals( ))
  • What would print?

13
String MethodsCheck API for more
  • charAt( ) - character at a position
  • concat( ) - adds to end of String
  • endsWith( ) - boolean using a String
  • equals( ) - char by char
  • equalsIgnoreCase( )
  • indexOf( ) - uses characters or substrings

14
String MethodsContinued
  • lastIndexOf( ) - last occurrence
  • length( ) - size of the String
  • replace( ) - replaces occurrences
  • startsWith( ) - returns boolean
  • substring( ) - returns a substring
  • toLowerCase( )
  • toUpperCase( )

15
String Methods
  • trim( ) - trims leading and trailing white space
    (returns, tabs, etc)
  • valueOf( ) - used for returning the String
    version of primitive data types

16
The String Buffer Class
  • Like a String, but you can change it
  • Whenever you want to manipulate the contents of a
    String, you should use a String buffer
  • Has methods like
  • append( )
  • insert( )
  • reverse( )
  • setCharAt( )

17
Collections
  • These give us the ability to store complex data
    types
  • Example the Vector class
  • Have multiple ways we can organize this
  • can restrict to only one occurrence
  • having keys to look up data
  • speed etc...

18
For Basic Collections
  • Collection - unordered with no restrictions. Can
    have multiple occurences
  • Lists - ordered (meaning indexed, not sorted) and
    can have multiple occurences.
  • Sets - Do not allow duplicate values
  • Maps - Have to explain this one

19
Map/Hash
  • Use data that youre about to store must have a
    unique key
  • Have a hash function to do this
  • Example
  • Jeff may generate 10566
  • bob may generate 2152

20
Ways that these Collections can Store Data
  • Arrays - seen these
  • Linked Lists - having one node point to another
    (draw this on board)
  • Trees - having one node point to multiple nodes
  • Hash Tables - generating values from keys

21
Linked ListsFrom Java I
5
17
43
98
22
TreesFrom Java I
51
27
70
13
29
64
87
23
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com