Understanding Java Packages, Importing Classes, and Working with Strings, Slides of Object Oriented Programming

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

2013/2014

Uploaded on 01/29/2014

sundar
sundar 🇮🇳

4.7

(9)

104 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Packages,
Characters,
Strings
Arguments to method main
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Understanding Java Packages, Importing Classes, and Working with Strings and more Slides Object Oriented Programming in PDF only on Docsity!

Packages,

Characters,

Strings

Arguments to method main

Package

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.

API packages that come with Java

On page that opens, left col, you see Classes in package

java.lang.

Click on Character

Specs for Class Character

Main pane now contains description of class Character:

  1. The header of its declaration.
  2. A description, including info about Unicode
  3. Nested class summary (skip it)
  4. Field summary (skip it)
  5. Constructor summary (read)
  6. Method summary (read)
  7. Field detail (skip it)
  8. Method detail (read)

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

Use the import statement!

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

Other packages on you hard drive

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

Importing the package

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

Primitive type char

char fred= 'a';

char wilma= 'b';

System.out.println(fred);

Unicode: 2-byte representation

Visit www.unicode.org/charts/

to see all unicode chars

a

Use single quotes

Casting char values

( 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)

Class Character

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

Static methods in class Character

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.

== versus equals

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

Operator +

"abc" + "12$" evaluates to "abc1 2 $"

Catenation, or

concatenation

  • is overloaded

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

Operator +

System.out.println("c is: " + c +

“, d is: " + d +

“, e is: " + e);

c 32 d

  • 3

e 201

Output:

c is: 32, d is: - 3, e is: 201

Can use + to advantage in println statement. Good debugging tool.

  • Note how each output number is annotated to know what it is.

Using several

lines increases

readability