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

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 

Dernier (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

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