SlideShare une entreprise Scribd logo
1  sur  20
Java Programming Language
Objectives


                In this session, you will learn to:
                   Write a program that uses command-line arguments and
                   system properties
                    Write a program that reads from standard input
                    Describe the C-type formatted input and output
                    Write a program that can create, read, and write files
                    Describe the basic hierarchy of collections
                    Write a program that uses sets and lists
                    Write a program to iterate over a collection
                    Write a program that uses generic collections




     Ver. 1.0                        Session 8                          Slide 1 of 20
Java Programming Language
Command-Line Arguments


               • Command-line arguments are the parameters passed to a
                 Java application at run time.
               • Each command-line argument is placed in the args array
                 that is passed to the static main method. For example:
                  public static void main(String[] args)




    Ver. 1.0                       Session 8                      Slide 2 of 20
Java Programming Language
System Properties


                System properties are a feature that replaces the concept of
                environment variables (which are platform-specific).
                System properties include information about the current
                user, the current version of the Java runtime, and the
                character used to separate components of a file path name.
                The System.getProperties() method returns a
                Properties object.
                 – The System.getProperty(String) method returns a
                   String representing the value of the named property.
                 – The System.getProperty(String, String)
                   method enables you to supply a default string value (second
                   parameter), which is returned if the named property does not
                   exist.



     Ver. 1.0                        Session 8                            Slide 3 of 20
Java Programming Language
Console I/O


                • Applications interact with the user using console I/O.
                • Java 2 SDK supports console I/O with three public
                  variables in the java.lang.System class:
                    – The variable System.out enables you to write to standard
                      output. It is an object of type PrintStream.
                    – The variable System.in enables you to read from standard
                      input. It is an object of type InputStream.
                    – The variable System.err enables you to write to standard
                      error. It is an object of type PrintStream.




     Ver. 1.0                          Session 8                           Slide 4 of 20
Java Programming Language
Writing to Standard Output


                • The println() method print the argument and a newline
                  character (n).
                • The print() method print the argument without a newline
                  character.
                • The print() and println() methods are overloaded for
                  most primitive types (boolean, char, int, long,
                  float, and double) and for char[], Object, and
                  String.
                • The print(Object) and println(Object) methods
                  call the toString() method on the argument.




     Ver. 1.0                       Session 8                      Slide 5 of 20
Java Programming Language
Reading from Standard Input


                  The application program can use the following methods of
                  the java.io package to read from the standard input:
                     Read characters from the keyboard and convert the raw bytes
                     into Unicode characters:
                       InputStreamReader ir=new
                       InputStreamReader(system.in);
                     Create a buffered reader to read each line from the keyboard:
                       BufferedReader in = new BufferedReader(ir);
                   – The BufferedReader(in) provides a readLine() method
                     to read from standard input one line at a time:
                      s=in.readLine();
                • The Scanner class of java.util package provides
                  formatted input functionality.


     Ver. 1.0                         Session 8                            Slide 6 of 20
Java Programming Language
Files and File I/O


                The java.io package enables you to do the following:
                   Create File objects
                   Manipulate File objects
                   Read and write to file streams




     Ver. 1.0                        Session 8                   Slide 7 of 20
Java Programming Language
Files and File I/O (Contd.)


                Creating a new File Object:
                 File myFile;
                The File class provides several utilities:
                 myFile = new File("myfile.txt");
                 myFile = new File("MyDocs", "myfile.txt");
                Directories are treated just like files in Java; the File class
                supports methods for retrieving an array of files in the
                directory, as follows:
                 File myDir = new File("MyDocs");
                 myFile = new File(myDir, "myfile.txt");




     Ver. 1.0                       Session 8                           Slide 8 of 20
Java Programming Language
Files and File I/O (Contd.)


                For file input:
                 – Use the FileReader class to read characters.
                 – Use the BufferedReader class to use the readLine()
                   method.
                For file output:
                 – Use the FileWriter class to write characters.
                 – Use the PrintWriter class to use the print() and
                   println() methods.




     Ver. 1.0                      Session 8                          Slide 9 of 20
Java Programming Language
Files and File I/O (Contd.)


                The application program can use the following methods of
                the java.io package to read input lines from the keyboard
                and write each line to a file:
                   Create file
                    File file = new File(args[0]);
                   Create a buffered reader to read each line from the keyboard
                    InputStreamReader isr=new
                    InputStreamReader(System.in);
                    BufferedReader in = new BufferedReader(isr);
                   Create a print writer on this file
                    PrintWriter out = new PrintWriter(new
                    FileWriter(file));




     Ver. 1.0                       Session 8                           Slide 10 of 20
Java Programming Language
Files and File I/O (Contd.)


                   Read each line from the input stream and print to a file one line
                   at a time:
                    s = in.readLine();
                    out.println(s);
                The application program can use the following methods of
                the java.io package to read from a text file and display
                each line on the standard output.
                   Create file:
                    File file = new File(args[0]);
                   Create a buffered reader to read each line from the keyboard:
                    BufferedReader in = new BufferedReader(new
                    FileReader(file));




     Ver. 1.0                        Session 8                             Slide 11 of 20
Java Programming Language
Files and File I/O (Contd.)


                 Read each line from the file and displays it on the standard
                 output:
                  s = in.readLine();
                  System.out.println(s);




     Ver. 1.0                      Session 8                            Slide 12 of 20
Java Programming Language
Demonstration


               Lets see how to read data from a file and display the output on
               the standard output device. This demo also shows how to run a
               program with user provided command line arguments.




    Ver. 1.0                         Session 8                        Slide 13 of 20
Java Programming Language
The Collections API


                A collection is a single object representing a group of
                objects known as its elements.
                The Collections API contains interfaces that group objects
                as one of the following:
                 – Collection: A group of objects called elements; any specific
                   ordering (or lack of) and allowance of duplicates is specified by
                   each implementation.
                 – Set: An unordered collection; no duplicates are permitted.
                 – List: An ordered collection; duplicates are permitted.




     Ver. 1.0                         Session 8                            Slide 14 of 20
Java Programming Language
Generics


                Generics are described as follows:
                   Provides compile-time type safety
                   Eliminates the need for casts
                   Example of before Generics code:
                    ArrayList list = new ArrayList();
                    list.add(0, new Integer(42));
                    int total =
                    ((Integer)list.get(0)).intValue();
                   Example of after Generics code:
                    ArrayList<Integer> list = new
                    ArrayList<Integer>();
                    list.add(0, new Integer(42));
                    int total = list.get(0).intValue();



     Ver. 1.0                      Session 8              Slide 15 of 20
Java Programming Language
Demonstration


               Lets see how to use the Collection API and generics in a Java
               program.




    Ver. 1.0                         Session 8                       Slide 16 of 20
Java Programming Language
Iterators


                Iteration is the process of retrieving every element in a
                collection.
                An iterator of a set is unordered.
                A ListIterator of a List can be scanned forwards
                (using the next method) or backwards (using the previous
                method).
                  List list = new ArrayList();
                  // add some elements
                  Iterator elements = list.iterator();
                  while ( elements.hasNext() ) {
                  System.out.println(elements.next());
                  }


     Ver. 1.0                      Session 8                        Slide 17 of 20
Java Programming Language
Enhanced for Loop


                The enhanced for loop has the following characteristics:
                   Simplified iteration over collections
                   Much shorter, clearer, and safer
                   Effective for arrays
                   Simpler when using nested loops
                   Iterator disadvantages removed
                Iterators are error prone:
                   Iterator variables occur three times per loop.
                   This provides the opportunity for code to go wrong.




     Ver. 1.0                         Session 8                          Slide 18 of 20
Java Programming Language
Summary


               In this session, you learned that:
                – A program can be parameterized by command-line arguments
                  and system properties.
                – Applications interaction with the user is accomplished using
                  text input and output to the console.
                – The Scanner class provides formatted input functionality. It is
                  a part of the java.util package.
                – Java.io package enables you to create file objects, manipulate
                  them, and read and write to the file streams.
                – The J2SE platform supports file input in two forms:
                    • The FileReader class to read characters.
                    • The BufferedReader class to use the readLine method.
                  The J2SE platform supports file output in two forms:
                    • The FileWriter class to write characters.
                    • The PrintWriter class to use the print and println methods.


    Ver. 1.0                          Session 8                                 Slide 19 of 20
Java Programming Language
Summary (Contd.)


                 A collection is a single object representing a group of objects.
                 These objects are known as elements. There are two types of
                 collections:
                  • Set: It is an unordered collection, where no duplicates are
                    permitted.
                  • List: It is an ordered collection, where duplicates are permitted.
               – The Iterator interface enables you to scan forward through
                 any collection. The enhanced for loop can be used to iterate
                 through a collection.
               – Generics provide compile-time type safety and eliminate the
                 need for casts.




    Ver. 1.0                         Session 8                                Slide 20 of 20

Contenu connexe

Tendances (20)

Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Java Notes
Java Notes Java Notes
Java Notes
 
Core java
Core javaCore java
Core java
 
Java features
Java featuresJava features
Java features
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_java
 
Introduction to java 101
Introduction to java 101Introduction to java 101
Introduction to java 101
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Core java
Core java Core java
Core java
 
Java notes
Java notesJava notes
Java notes
 
Java training in delhi
Java training in delhiJava training in delhi
Java training in delhi
 
Core Java
Core JavaCore Java
Core Java
 
Introduction to JAVA
Introduction to JAVAIntroduction to JAVA
Introduction to JAVA
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Java notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esectionJava notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esection
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 

Similaire à Java session08

Java programming Chapter 4.pptx
Java programming Chapter 4.pptxJava programming Chapter 4.pptx
Java programming Chapter 4.pptxssusera0d3d2
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECSreedhar Chowdam
 
Java programming basics
Java programming basicsJava programming basics
Java programming basicsHamid Ghorbani
 
Java OOP Concepts 1st Slide
Java OOP Concepts 1st SlideJava OOP Concepts 1st Slide
Java OOP Concepts 1st Slidesunny khan
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output ConceptsVicter Paul
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptxcherryreddygannu
 
Java byte code & virtual machine
Java byte code & virtual machineJava byte code & virtual machine
Java byte code & virtual machineLaxman Puri
 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptxRathanMB
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1sotlsoc
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OWebStackAcademy
 
Java-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsJava-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsbuvanabala
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdfSudhanshiBakre1
 

Similaire à Java session08 (20)

Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Java programming Chapter 4.pptx
Java programming Chapter 4.pptxJava programming Chapter 4.pptx
Java programming Chapter 4.pptx
 
Unit IV Notes.docx
Unit IV Notes.docxUnit IV Notes.docx
Unit IV Notes.docx
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Java basic
Java basicJava basic
Java basic
 
Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java OOP Concepts 1st Slide
Java OOP Concepts 1st SlideJava OOP Concepts 1st Slide
Java OOP Concepts 1st Slide
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
Java byte code & virtual machine
Java byte code & virtual machineJava byte code & virtual machine
Java byte code & virtual machine
 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptx
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
 
Java-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsJava-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oops
 
Introduction java programming
Introduction java programmingIntroduction java programming
Introduction java programming
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
 

Plus de Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Dernier

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 

Dernier (20)

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 

Java session08

  • 1. Java Programming Language Objectives In this session, you will learn to: Write a program that uses command-line arguments and system properties Write a program that reads from standard input Describe the C-type formatted input and output Write a program that can create, read, and write files Describe the basic hierarchy of collections Write a program that uses sets and lists Write a program to iterate over a collection Write a program that uses generic collections Ver. 1.0 Session 8 Slide 1 of 20
  • 2. Java Programming Language Command-Line Arguments • Command-line arguments are the parameters passed to a Java application at run time. • Each command-line argument is placed in the args array that is passed to the static main method. For example: public static void main(String[] args) Ver. 1.0 Session 8 Slide 2 of 20
  • 3. Java Programming Language System Properties System properties are a feature that replaces the concept of environment variables (which are platform-specific). System properties include information about the current user, the current version of the Java runtime, and the character used to separate components of a file path name. The System.getProperties() method returns a Properties object. – The System.getProperty(String) method returns a String representing the value of the named property. – The System.getProperty(String, String) method enables you to supply a default string value (second parameter), which is returned if the named property does not exist. Ver. 1.0 Session 8 Slide 3 of 20
  • 4. Java Programming Language Console I/O • Applications interact with the user using console I/O. • Java 2 SDK supports console I/O with three public variables in the java.lang.System class: – The variable System.out enables you to write to standard output. It is an object of type PrintStream. – The variable System.in enables you to read from standard input. It is an object of type InputStream. – The variable System.err enables you to write to standard error. It is an object of type PrintStream. Ver. 1.0 Session 8 Slide 4 of 20
  • 5. Java Programming Language Writing to Standard Output • The println() method print the argument and a newline character (n). • The print() method print the argument without a newline character. • The print() and println() methods are overloaded for most primitive types (boolean, char, int, long, float, and double) and for char[], Object, and String. • The print(Object) and println(Object) methods call the toString() method on the argument. Ver. 1.0 Session 8 Slide 5 of 20
  • 6. Java Programming Language Reading from Standard Input The application program can use the following methods of the java.io package to read from the standard input: Read characters from the keyboard and convert the raw bytes into Unicode characters: InputStreamReader ir=new InputStreamReader(system.in); Create a buffered reader to read each line from the keyboard: BufferedReader in = new BufferedReader(ir); – The BufferedReader(in) provides a readLine() method to read from standard input one line at a time: s=in.readLine(); • The Scanner class of java.util package provides formatted input functionality. Ver. 1.0 Session 8 Slide 6 of 20
  • 7. Java Programming Language Files and File I/O The java.io package enables you to do the following: Create File objects Manipulate File objects Read and write to file streams Ver. 1.0 Session 8 Slide 7 of 20
  • 8. Java Programming Language Files and File I/O (Contd.) Creating a new File Object: File myFile; The File class provides several utilities: myFile = new File("myfile.txt"); myFile = new File("MyDocs", "myfile.txt"); Directories are treated just like files in Java; the File class supports methods for retrieving an array of files in the directory, as follows: File myDir = new File("MyDocs"); myFile = new File(myDir, "myfile.txt"); Ver. 1.0 Session 8 Slide 8 of 20
  • 9. Java Programming Language Files and File I/O (Contd.) For file input: – Use the FileReader class to read characters. – Use the BufferedReader class to use the readLine() method. For file output: – Use the FileWriter class to write characters. – Use the PrintWriter class to use the print() and println() methods. Ver. 1.0 Session 8 Slide 9 of 20
  • 10. Java Programming Language Files and File I/O (Contd.) The application program can use the following methods of the java.io package to read input lines from the keyboard and write each line to a file: Create file File file = new File(args[0]); Create a buffered reader to read each line from the keyboard InputStreamReader isr=new InputStreamReader(System.in); BufferedReader in = new BufferedReader(isr); Create a print writer on this file PrintWriter out = new PrintWriter(new FileWriter(file)); Ver. 1.0 Session 8 Slide 10 of 20
  • 11. Java Programming Language Files and File I/O (Contd.) Read each line from the input stream and print to a file one line at a time: s = in.readLine(); out.println(s); The application program can use the following methods of the java.io package to read from a text file and display each line on the standard output. Create file: File file = new File(args[0]); Create a buffered reader to read each line from the keyboard: BufferedReader in = new BufferedReader(new FileReader(file)); Ver. 1.0 Session 8 Slide 11 of 20
  • 12. Java Programming Language Files and File I/O (Contd.) Read each line from the file and displays it on the standard output: s = in.readLine(); System.out.println(s); Ver. 1.0 Session 8 Slide 12 of 20
  • 13. Java Programming Language Demonstration Lets see how to read data from a file and display the output on the standard output device. This demo also shows how to run a program with user provided command line arguments. Ver. 1.0 Session 8 Slide 13 of 20
  • 14. Java Programming Language The Collections API A collection is a single object representing a group of objects known as its elements. The Collections API contains interfaces that group objects as one of the following: – Collection: A group of objects called elements; any specific ordering (or lack of) and allowance of duplicates is specified by each implementation. – Set: An unordered collection; no duplicates are permitted. – List: An ordered collection; duplicates are permitted. Ver. 1.0 Session 8 Slide 14 of 20
  • 15. Java Programming Language Generics Generics are described as follows: Provides compile-time type safety Eliminates the need for casts Example of before Generics code: ArrayList list = new ArrayList(); list.add(0, new Integer(42)); int total = ((Integer)list.get(0)).intValue(); Example of after Generics code: ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0, new Integer(42)); int total = list.get(0).intValue(); Ver. 1.0 Session 8 Slide 15 of 20
  • 16. Java Programming Language Demonstration Lets see how to use the Collection API and generics in a Java program. Ver. 1.0 Session 8 Slide 16 of 20
  • 17. Java Programming Language Iterators Iteration is the process of retrieving every element in a collection. An iterator of a set is unordered. A ListIterator of a List can be scanned forwards (using the next method) or backwards (using the previous method). List list = new ArrayList(); // add some elements Iterator elements = list.iterator(); while ( elements.hasNext() ) { System.out.println(elements.next()); } Ver. 1.0 Session 8 Slide 17 of 20
  • 18. Java Programming Language Enhanced for Loop The enhanced for loop has the following characteristics: Simplified iteration over collections Much shorter, clearer, and safer Effective for arrays Simpler when using nested loops Iterator disadvantages removed Iterators are error prone: Iterator variables occur three times per loop. This provides the opportunity for code to go wrong. Ver. 1.0 Session 8 Slide 18 of 20
  • 19. Java Programming Language Summary In this session, you learned that: – A program can be parameterized by command-line arguments and system properties. – Applications interaction with the user is accomplished using text input and output to the console. – The Scanner class provides formatted input functionality. It is a part of the java.util package. – Java.io package enables you to create file objects, manipulate them, and read and write to the file streams. – The J2SE platform supports file input in two forms: • The FileReader class to read characters. • The BufferedReader class to use the readLine method. The J2SE platform supports file output in two forms: • The FileWriter class to write characters. • The PrintWriter class to use the print and println methods. Ver. 1.0 Session 8 Slide 19 of 20
  • 20. Java Programming Language Summary (Contd.) A collection is a single object representing a group of objects. These objects are known as elements. There are two types of collections: • Set: It is an unordered collection, where no duplicates are permitted. • List: It is an ordered collection, where duplicates are permitted. – The Iterator interface enables you to scan forward through any collection. The enhanced for loop can be used to iterate through a collection. – Generics provide compile-time type safety and eliminate the need for casts. Ver. 1.0 Session 8 Slide 20 of 20