SlideShare a Scribd company logo
Programming in Java
5-day workshop
The Java SDLC
Matt Collison
JP Morgan Chase 2021
PiJ4.1: Java Packages
Packages
A package is a group of related classes, interfaces and resources:
• Built-in packages
• java.lang (imported automatically)
Object, String, Math, primitive wrapper classes, ...
• java.util
Scanner, List, ArrayList, Random, HashSet, HashMap, ...
• java.io
File, FileReader, FileWriter, InputStream, OutputStream, IOexception, ...
...
• User-defined packages (including the ‘no-name’ package)
Advantages of using packages
1. Reusability
2. Better organisation
3. Name conflicts: we can define two classes with the same name in
different packages
package package.name;
User defined packages
package shape; //package declaration at the beginning of the program
public class Rectangle implements Shape {
...
}
• A class can have only one package declaration.
• Package name is all lowercase, by convention.
Compiling user-defined packages
• Compile the code:
>>javac -d destination_folder fileName.java
• Example:
>> javac -d . Shape.java Rectangle.java
• After compilation, the byte codes will be located at:
./shape/Shape.class
./shape/Rectangle.class
Note the dot referring to the
current directory.
./shape is added as this
represents the package
Using user-defined packages
• A package name corresponds to the directory structure for storing the
class files.
import shape.Triangle; //compiler will look for shape/Triangle.class
• In general, a company uses its reversed Internet domain name for its
package names.
//compiler will look for com/apple/computers/*.class
import com.apple.computers.*;
Using user-defined packages
Three options for using the package scope:
1. No import – use fully qualified name without the import keyword:
shape.Shape rt = new shape.Rectangle();
2. Import the class – only import the specific class you are interested in using:
import shape.Shape;
3. Import the package – a wildcard import for all classes and interfaces in the package:
import shape.*;
Note: It is generally not good practise to import package name.* unless you really need
many package members — Why?
Filesystem and packages
• Packages in Java are often groups in subdirectories but are not
hierarchical (although they may seem to be by looking at them).
For example:
• javax.crypto
• javax.crypto.interfaces
• javax.crypto.spec
• import javax.crypto.*;
• will import all the members of javax.crypto
• but not javax.crypto.interfaces or javax.crypto.spec package members
Filesystem and packages
• It is common to arrange your source and class directories separately.
• Example:
• The package source code: ./src/shape/*.java
• The package class files: ./bin/shape/*.class
• Q: What is the command for the above directory structure?
>>javac -d bin/ src/shape/*.java
Classpath
• To use this shape package, you may
either copy the shape/*.class into the current working directory
• or:
>>javac -classpath bin/ ShapeApp.java
>>java -classpath bin/:. ShapeApp
or:
Set CLASSPATH System Variable to locate where this package is.
The ant build tool
• Ant enable the
configuration of your
project classpath,
manifest file, target
directory, etc in a
structured xml
>> ant compile
>> ant jar
>> ant run
The maven (mvn) build tool
• Similar to ant but a stricter build lifecycle phases:
• Maven enables:
• dynamic dependency crawling
• Plugin execution for fully operational devops
Packages summary
• Keywords:
package, import
• Declare a package
package shape;
• Import a package member
import shape.Rectangle;
• Package management is best handled with maven
Documentation
• Java supports three types of comments. During compilation, all the
comments are ignored.
• single line comment // ...
• multiple lines comment /* ... */
• documentation comment /** ... */
• JavaDoc: A JDK tool automatically creates fancy HTML-based
documentation based on /** ...*/ in your source files.
>> javadoc NaturalLanguageNumbers.java
Javadocs
import java.util.Scanner; //not used
import java.util.Random; //not used
/**
* must put the class documentation directly before
* the class definition.
*/
public class NaturalLanguageNumbers {
/**
* And method documentation directly before the method definition.
*/
public static String intToString( int number ){ … }
…
}
Javadoc tags
In addition, you can include special javadoc tags in the documentation
comments that provide specific information used by JavaDoc to format
the documentation pages.
• @author Provides information about the author.
• @version Indicates the version number.
• @since Indicate the version.
• @param Provides the name and description of a method.
• @return Provides a description of a method’s return value.
• @throws Indicates exceptions that are thrown by a method.
Javadocs tags
import java.util.Scanner;
import java.util.Random;
/**
* A class for converting numbers to natural language representation.
* @author Matt Collison
* @version 1.0
*/
public class NaturalLanguageNumbersTags {
/** Defines the language */
public static final String language = “English”;
/**
* A method to convert integer values into natural language expressions.
* @param number an integer value
* @return the number expressed as words in natural language
*/
public static String intToString( int number ){ … }
…
}
Java Documentation
• >> javadoc –d docs
NaturalLanguageNumbersTags.java
Annotations
• An annotation always starts with the symbol @ followed by the
annotation name.
• Different from the doc tag, annotations are checked by Java compiler.
• Q: Where have you seen annotations before?
Pre-defined annotations
Commonly used pre-defined annotations:
• @Deprecated
• @Override
• @SuppressWarnings
Custom annotations could be defined by using @interface.
• More details see:
https://docs.oracle.com/javase/tutorial/java/annotations/index.html
Javadoc tags vs annotations example
• @Deprecated - indicates a marked element should no longer be used.
It is usually also documented using the Javadoc @deprecated tag
/**
* @deprecated ← a doc tag, not checked by compiler
* explanation of why it was deprecated
*/
@Deprecated ← an annotation, checked by compiler
void anyMethod() { ... }
Learning resources
The workshop homepage
https://mcollison.github.io/JPMC-java-intro-2021/
The course materials
https://mcollison.github.io/java-programming-foundations/
• Session worksheets – updated each week
Additional resources
• Think Java: How to think like a computer scientist
• Allen B Downey (O’Reilly Press)
• Available under Creative Commons license
• https://greenteapress.com/wp/think-java-2e/
• Oracle central Java Documentation –
https://docs.oracle.com/javase/8/docs/api/
• Other sources:
• W3Schools Java - https://www.w3schools.com/java/
• stack overflow - https://stackoverflow.com/
• Coding bat - https://codingbat.com/java

More Related Content

PDF
Java packages
DOCX
Class notes(week 7) on packages
PPTX
Packages and interfaces
PDF
Java - Interfaces & Packages
PPTX
Java packages
PPTX
Packages in java
PPTX
OCA JAVA - 1 Packages and Class Structure
PPTX
Packages and Interfaces
Java packages
Class notes(week 7) on packages
Packages and interfaces
Java - Interfaces & Packages
Java packages
Packages in java
OCA JAVA - 1 Packages and Class Structure
Packages and Interfaces

What's hot (20)

PPT
Packages in java
PPTX
Java packages
PPTX
Introduction to java
PPTX
Unit3 part3-packages and interfaces
PPTX
Java package
PPTX
Packages,static,this keyword in java
PDF
JAVA PROGRAMMING – Packages - Stream based I/O
PPT
Packages in java
PPT
Packages and interfaces
PPTX
Unit3 packages & interfaces
PPT
packages and interfaces
PPTX
Packages in java
PPTX
Unit 5 java-awt (1)
PPT
Java packages
PPTX
5.interface and packages
PPT
Java package
PPTX
Package In Java
PPTX
Package in Java
PPT
Java access modifiers
PPTX
Java program structure
Packages in java
Java packages
Introduction to java
Unit3 part3-packages and interfaces
Java package
Packages,static,this keyword in java
JAVA PROGRAMMING – Packages - Stream based I/O
Packages in java
Packages and interfaces
Unit3 packages & interfaces
packages and interfaces
Packages in java
Unit 5 java-awt (1)
Java packages
5.interface and packages
Java package
Package In Java
Package in Java
Java access modifiers
Java program structure
Ad

Similar to Pi j4.1 packages (20)

PPTX
Java packages oop
PDF
javapackage
PDF
Class notes(week 7) on packages
PPT
Javapackages 4th semester
PPT
4.Packages_m1.ppt
DOCX
Unit4 java
PPT
packages in java programming language ppt
PPT
packages.ppt
PPT
packages.ppt
PPT
7.Packages and Interfaces(MB).ppt .
PDF
JAVA 2-studenttrreadexeceptionpackages.pdf
PPT
packages unit 5 .ppt
PDF
JAVA 3.1.pdfdhfksuhdfshkvbhdbsjfhbvjdzfhb
PPT
packages.ppt
PPT
Packages in java
PPTX
Chapter 1 :
PPTX
API workshop: Deep dive into Java
PPT
9 cm604.26
PPT
Packages,interfaces and exceptions
PPT
Packages(9 cm604.26)
Java packages oop
javapackage
Class notes(week 7) on packages
Javapackages 4th semester
4.Packages_m1.ppt
Unit4 java
packages in java programming language ppt
packages.ppt
packages.ppt
7.Packages and Interfaces(MB).ppt .
JAVA 2-studenttrreadexeceptionpackages.pdf
packages unit 5 .ppt
JAVA 3.1.pdfdhfksuhdfshkvbhdbsjfhbvjdzfhb
packages.ppt
Packages in java
Chapter 1 :
API workshop: Deep dive into Java
9 cm604.26
Packages,interfaces and exceptions
Packages(9 cm604.26)
Ad

More from mcollison (11)

PPTX
Pi j4.2 software-reliability
PPTX
Pi j3.1 inheritance
PPTX
Pi j3.2 polymorphism
PPTX
Pi j3.4 data-structures
PPTX
Pi j2.3 objects
PPTX
Pi j2.2 classes
PPTX
Pi j1.0 workshop-introduction
PPTX
Pi j1.4 loops
PPTX
Pi j1.3 operators
PPTX
Pi j1.2 variable-assignment
PPTX
Pi j1.1 what-is-java
Pi j4.2 software-reliability
Pi j3.1 inheritance
Pi j3.2 polymorphism
Pi j3.4 data-structures
Pi j2.3 objects
Pi j2.2 classes
Pi j1.0 workshop-introduction
Pi j1.4 loops
Pi j1.3 operators
Pi j1.2 variable-assignment
Pi j1.1 what-is-java

Recently uploaded (20)

PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Trump Administration's workforce development strategy
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
PPTX
Introduction to Building Materials
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
RMMM.pdf make it easy to upload and study
PDF
1_English_Language_Set_2.pdf probationary
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Orientation - ARALprogram of Deped to the Parents.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Hazard Identification & Risk Assessment .pdf
Final Presentation General Medicine 03-08-2024.pptx
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Weekly quiz Compilation Jan -July 25.pdf
Trump Administration's workforce development strategy
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
Unit 4 Skeletal System.ppt.pptxopresentatiom
Introduction to Building Materials
Final Presentation General Medicine 03-08-2024.pptx
A systematic review of self-coping strategies used by university students to ...
RMMM.pdf make it easy to upload and study
1_English_Language_Set_2.pdf probationary
202450812 BayCHI UCSC-SV 20250812 v17.pptx

Pi j4.1 packages

  • 1. Programming in Java 5-day workshop The Java SDLC Matt Collison JP Morgan Chase 2021 PiJ4.1: Java Packages
  • 2. Packages A package is a group of related classes, interfaces and resources: • Built-in packages • java.lang (imported automatically) Object, String, Math, primitive wrapper classes, ... • java.util Scanner, List, ArrayList, Random, HashSet, HashMap, ... • java.io File, FileReader, FileWriter, InputStream, OutputStream, IOexception, ... ... • User-defined packages (including the ‘no-name’ package)
  • 3. Advantages of using packages 1. Reusability 2. Better organisation 3. Name conflicts: we can define two classes with the same name in different packages package package.name;
  • 4. User defined packages package shape; //package declaration at the beginning of the program public class Rectangle implements Shape { ... } • A class can have only one package declaration. • Package name is all lowercase, by convention.
  • 5. Compiling user-defined packages • Compile the code: >>javac -d destination_folder fileName.java • Example: >> javac -d . Shape.java Rectangle.java • After compilation, the byte codes will be located at: ./shape/Shape.class ./shape/Rectangle.class Note the dot referring to the current directory. ./shape is added as this represents the package
  • 6. Using user-defined packages • A package name corresponds to the directory structure for storing the class files. import shape.Triangle; //compiler will look for shape/Triangle.class • In general, a company uses its reversed Internet domain name for its package names. //compiler will look for com/apple/computers/*.class import com.apple.computers.*;
  • 7. Using user-defined packages Three options for using the package scope: 1. No import – use fully qualified name without the import keyword: shape.Shape rt = new shape.Rectangle(); 2. Import the class – only import the specific class you are interested in using: import shape.Shape; 3. Import the package – a wildcard import for all classes and interfaces in the package: import shape.*; Note: It is generally not good practise to import package name.* unless you really need many package members — Why?
  • 8. Filesystem and packages • Packages in Java are often groups in subdirectories but are not hierarchical (although they may seem to be by looking at them). For example: • javax.crypto • javax.crypto.interfaces • javax.crypto.spec • import javax.crypto.*; • will import all the members of javax.crypto • but not javax.crypto.interfaces or javax.crypto.spec package members
  • 9. Filesystem and packages • It is common to arrange your source and class directories separately. • Example: • The package source code: ./src/shape/*.java • The package class files: ./bin/shape/*.class • Q: What is the command for the above directory structure? >>javac -d bin/ src/shape/*.java
  • 10. Classpath • To use this shape package, you may either copy the shape/*.class into the current working directory • or: >>javac -classpath bin/ ShapeApp.java >>java -classpath bin/:. ShapeApp or: Set CLASSPATH System Variable to locate where this package is.
  • 11. The ant build tool • Ant enable the configuration of your project classpath, manifest file, target directory, etc in a structured xml >> ant compile >> ant jar >> ant run
  • 12. The maven (mvn) build tool • Similar to ant but a stricter build lifecycle phases: • Maven enables: • dynamic dependency crawling • Plugin execution for fully operational devops
  • 13. Packages summary • Keywords: package, import • Declare a package package shape; • Import a package member import shape.Rectangle; • Package management is best handled with maven
  • 14. Documentation • Java supports three types of comments. During compilation, all the comments are ignored. • single line comment // ... • multiple lines comment /* ... */ • documentation comment /** ... */ • JavaDoc: A JDK tool automatically creates fancy HTML-based documentation based on /** ...*/ in your source files. >> javadoc NaturalLanguageNumbers.java
  • 15. Javadocs import java.util.Scanner; //not used import java.util.Random; //not used /** * must put the class documentation directly before * the class definition. */ public class NaturalLanguageNumbers { /** * And method documentation directly before the method definition. */ public static String intToString( int number ){ … } … }
  • 16. Javadoc tags In addition, you can include special javadoc tags in the documentation comments that provide specific information used by JavaDoc to format the documentation pages. • @author Provides information about the author. • @version Indicates the version number. • @since Indicate the version. • @param Provides the name and description of a method. • @return Provides a description of a method’s return value. • @throws Indicates exceptions that are thrown by a method.
  • 17. Javadocs tags import java.util.Scanner; import java.util.Random; /** * A class for converting numbers to natural language representation. * @author Matt Collison * @version 1.0 */ public class NaturalLanguageNumbersTags { /** Defines the language */ public static final String language = “English”; /** * A method to convert integer values into natural language expressions. * @param number an integer value * @return the number expressed as words in natural language */ public static String intToString( int number ){ … } … }
  • 18. Java Documentation • >> javadoc –d docs NaturalLanguageNumbersTags.java
  • 19. Annotations • An annotation always starts with the symbol @ followed by the annotation name. • Different from the doc tag, annotations are checked by Java compiler. • Q: Where have you seen annotations before?
  • 20. Pre-defined annotations Commonly used pre-defined annotations: • @Deprecated • @Override • @SuppressWarnings Custom annotations could be defined by using @interface. • More details see: https://docs.oracle.com/javase/tutorial/java/annotations/index.html
  • 21. Javadoc tags vs annotations example • @Deprecated - indicates a marked element should no longer be used. It is usually also documented using the Javadoc @deprecated tag /** * @deprecated ← a doc tag, not checked by compiler * explanation of why it was deprecated */ @Deprecated ← an annotation, checked by compiler void anyMethod() { ... }
  • 22. Learning resources The workshop homepage https://mcollison.github.io/JPMC-java-intro-2021/ The course materials https://mcollison.github.io/java-programming-foundations/ • Session worksheets – updated each week
  • 23. Additional resources • Think Java: How to think like a computer scientist • Allen B Downey (O’Reilly Press) • Available under Creative Commons license • https://greenteapress.com/wp/think-java-2e/ • Oracle central Java Documentation – https://docs.oracle.com/javase/8/docs/api/ • Other sources: • W3Schools Java - https://www.w3schools.com/java/ • stack overflow - https://stackoverflow.com/ • Coding bat - https://codingbat.com/java