SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados (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
 

Semelhante a Pi j4.1 packages

Semelhante a 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
 

Mais de 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
 

Mais de 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
 

Último

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 

Último (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

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