SlideShare a Scribd company logo
1 of 23
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

What's hot (20)

Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages
Java packagesJava packages
Java packages
 
Introduction to java
Introduction to  javaIntroduction to  java
Introduction to java
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 
Java package
Java packageJava package
Java package
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Unit 5 java-awt (1)
Unit 5 java-awt (1)Unit 5 java-awt (1)
Unit 5 java-awt (1)
 
Java packages
Java packagesJava packages
Java packages
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
 
Java package
Java packageJava package
Java package
 
Package In Java
Package In JavaPackage In Java
Package In Java
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Java program structure
Java program structureJava program structure
Java program structure
 

Similar to Pi j4.1 packages

Similar to Pi j4.1 packages (20)

Chapter 1 :
Chapter 1 : Chapter 1 :
Chapter 1 :
 
Packages
PackagesPackages
Packages
 
API workshop: Deep dive into Java
API workshop: Deep dive into JavaAPI workshop: Deep dive into Java
API workshop: Deep dive into Java
 
9 cm604.26
9 cm604.269 cm604.26
9 cm604.26
 
Java Packages
Java Packages Java Packages
Java Packages
 
20-packages-jar.ppt
20-packages-jar.ppt20-packages-jar.ppt
20-packages-jar.ppt
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Packages(9 cm604.26)
Packages(9 cm604.26)Packages(9 cm604.26)
Packages(9 cm604.26)
 
JAVA PROGRAMMING
JAVA PROGRAMMINGJAVA PROGRAMMING
JAVA PROGRAMMING
 
Standards For Java Coding
Standards For Java CodingStandards For Java Coding
Standards For Java Coding
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
 
Packages
PackagesPackages
Packages
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
 
Object Oriented Programming - Java
Object Oriented Programming -  JavaObject Oriented Programming -  Java
Object Oriented Programming - Java
 
oop unit1.pptx
oop unit1.pptxoop unit1.pptx
oop unit1.pptx
 
Package in java
Package in javaPackage in java
Package in java
 
Code Documentation. That ugly thing...
Code Documentation. That ugly thing...Code Documentation. That ugly thing...
Code Documentation. That ugly thing...
 
PACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in java
PACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in javaPACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in java
PACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in java
 
CS8392 OOP
CS8392 OOPCS8392 OOP
CS8392 OOP
 

More from mcollison

Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliabilitymcollison
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritancemcollison
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Pi j3.4 data-structures
Pi j3.4 data-structuresPi j3.4 data-structures
Pi j3.4 data-structuresmcollison
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objectsmcollison
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classesmcollison
 
Pi j1.0 workshop-introduction
Pi j1.0 workshop-introductionPi j1.0 workshop-introduction
Pi j1.0 workshop-introductionmcollison
 
Pi j1.4 loops
Pi j1.4 loopsPi j1.4 loops
Pi j1.4 loopsmcollison
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operatorsmcollison
 
Pi j1.2 variable-assignment
Pi j1.2 variable-assignmentPi j1.2 variable-assignment
Pi j1.2 variable-assignmentmcollison
 
Pi j1.1 what-is-java
Pi j1.1 what-is-javaPi j1.1 what-is-java
Pi j1.1 what-is-javamcollison
 

More from mcollison (11)

Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliability
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Pi j3.4 data-structures
Pi j3.4 data-structuresPi j3.4 data-structures
Pi j3.4 data-structures
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classes
 
Pi j1.0 workshop-introduction
Pi j1.0 workshop-introductionPi j1.0 workshop-introduction
Pi j1.0 workshop-introduction
 
Pi j1.4 loops
Pi j1.4 loopsPi j1.4 loops
Pi j1.4 loops
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
Pi j1.2 variable-assignment
Pi j1.2 variable-assignmentPi j1.2 variable-assignment
Pi j1.2 variable-assignment
 
Pi j1.1 what-is-java
Pi j1.1 what-is-javaPi j1.1 what-is-java
Pi j1.1 what-is-java
 

Recently uploaded

Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 

Recently uploaded (20)

Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.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