
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of java packages, focusing on the packages that come with java and how to use them. It also covers the concept of strings in java, including special characters, casting, and various methods and functions. Students will learn how to import classes, create and manipulate strings, and understand the difference between primitive types and objects.
Typology: Slides
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















Package : See JavaSummary.pptx, slide 20
(1) Java classes that are contained in a specific directory on
your hard drive (it may also contain sub-packages) or
(2) Packages of Java classes that come with Java,
e.g. packages java.lang, javax.swing.
Consider first the packages that come with Java. We show
you:
(1) How to refer to them
(2) How to find out how to use them, using the API
(Application Programmer Interface) specifications.
On page that opens, left col, you see Classes in package
java.lang.
Click on Character
Main pane now contains description of class Character:
Find method compareTo
See a 1 - sentence description
Click on method name
Takes you to a complete
description in Method detail
section
More on class
Character later
To be able to use just JFrame, put an import
statement before the class definition:
import javax.swing.JFrame;
public class C {
public void m(…) {
JFrame jf= new JFrame();
Imports only class JFrame.
Use the asterisk, as in line
below, to import all classes
in package:
import javax.swing.*;
One can put a bunch of logically related classes into a package,
which means they will all be in the same directory on hard drive.
Reasons for doing this? We may discuss much later.
Image of Eclipse
Package Explorer:
3 projects:
project has
default
package and
package
pack
Default package has
2 classes:
Rec02, Rec02Tester
pack1 has 1 class: C
import pack1.*;
public class Rec02 {
public Rec02() {
C v= new C();
package pack 1 ;
public class C {
/** Constructor: */
public C() {
Every class in package
pack1 must start with
the package statement
Every class outside the
package should import its
classes in order to use them
char fred= 'a';
char wilma= 'b';
System.out.println(fred);
a
( int ) 'a' gives 97
( char ) 97 gives 'a'
( char ) 2384 gives 'ॐ'
Cast a char to an int using unary prefix operator ( int ),
Gives unicode representation of char, as an int
No operations on char s (values of type char)! BUT , if
used in relation or arith, a char is automatically cast to type int.
Relations < > <= >= == != ==
'a' < 'b' same as 97 < 98, i.e. false
'a' + 1 gives 98
Om, or Aum, the sound of
the universe (Hinduism)
An object of class Character wraps a single char (has a field that contains
the char )
Character c1= new Character( 'b' );
Character c2= new Character( 'c' );
Character@a 1
'b'
charValue()
compareTo(Character)
equals(Object)
Character@b 9
'c'
charValue()
compareTo(Character)
equals(Object)
Character@a 1 c
Character@b c
Don’t know
field name
Lots of static functions. You have to look to see what is available.
Below are examples
isAlphabetic(c)
isDigit(c)
isLetter(c)
isLowerCase(c)
isUpperCase(c)
isWhitespace(c)
toLowerCase(c)
toUpperCase(c)
These return the obvious
boolean value for parameter
c, a char
Whitespace chars are the space ‘ ‘,
tab char, line feed, carriage return,
etc.
These return a char.
c 1 == c 2
c 3 == c 1
c 1 == c 1
c 1 .equals(c 2 )
c 3 .equals(c 1 )
Character@a
'b'
charValue()
compareTo(Character)
equals(Object)
Character@b
'b'
charValue()
compareTo(Character)
equals(Object)
Character@a c Character@b c 2 null c
true iff c1, c2 contain same values
true iff c 2 is also a Character
object and contains same char
as c 1
false
false
true
true
Error!!!
"abc" + "12$" evaluates to "abc1 2 $"
Catenation, or
concatenation
(1 + 2) + "ab$" evaluates to "3ab$"
If one operand of catenation is a String and the other isn’t,
the other is converted to a String.
Sequence of + done left to right
"ab$" + 1 + 2 evaluates to "ab$ 12 "
System.out.println("c is: " + c +
“, d is: " + d +
“, e is: " + e);
c 32 d
e 201
Output:
c is: 32, d is: - 3, e is: 201
Can use + to advantage in println statement. Good debugging tool.
Using several
lines increases
readability