Java I/O & Data Types: PrintStream, BufferedReader, StringTokenizer, AbstractClasses, Inte, Slides of Computer Science

Examples and explanations of various java input/output (i/o) classes and data types, including printstream, bufferedreader, stringtokenizer, abstractclasses, interfaces, file, fileinputstream, randomaccessfile, and generic types. Topics covered include printing textual representations of java data types, reading strings from the console, string equality testing, and file i/o operations.

Typology: Slides

2012/2013

Uploaded on 03/27/2013

ekana
ekana 🇮🇳

4

(44)

370 documents

1 / 60

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Java and C++
Both are “object oriented”
Class-based
Java is interpreted, and garbage collected
C++ has more potential for memory leaks (you must
explicitly delete objects)
Java intended to be explicitly platform
independent
Similarities in syntax
See Gary Shute’s document on Java for C
Programmers (available from course calendar)
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c

Partial preview of the text

Download Java I/O & Data Types: PrintStream, BufferedReader, StringTokenizer, AbstractClasses, Inte and more Slides Computer Science in PDF only on Docsity!

1

Java and C++

  • Both are “object oriented”
    • Class-based
  • Java is interpreted, and garbage collected
    • C++ has more potential for memory leaks (you must explicitly delete objects)
  • Java intended to be explicitly platform independent
  • Similarities in syntax
  • See Gary Shute’s document on Java for C Programmers (available from course calendar)

2

Some Web Resources

  • API Specification http://java.sun.com/j2se/1.5.0/docs/api/index.html http://java.sun.com/javase/6/docs/api/ https://mustang.dev.java.net/
  • Online language reference http://java.sun.com/docs/books/jls/
  • “The Java Tutorials”
    • http://java.sun.com/docs/books/tutorial/index.html
  • Safari Books (UMD)
    • Java Gently
      • http://proquest.safaribooksonline.com/
    • Java Examples in a Nutshell
      • http://proquest.safaribooksonline.com/
  • http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html Docsity.com

Java

Type Contains Size, Coding, or

Values

boolean Truth value true, false

char Character Unicode characters (

bytes)

byte Signed integer 8 bit 2’s compliment

short Signed integer 16 bit 2’s compliment

int Signed integer 32 bit 2’s compliment

long Signed integer 64 bit 2’s compliment

float Real number 32 bit IEEE 754 floating point

double Real number 64 bit IEEE 754 floating point

5

Java Objects

  • Similar to C++ objects
    • Instance of a class
  • Object variables
    • Contain references to objects
    • Two object variables can refer to the same object
  • Simple type variables (e.g., int)
    • Contain value copies
  • No explicit pointer types in Java

7

Hello World Program

// put this into a file named “A.java”

public class A {

// need the following method in a ‘main’ program // need the “Strings args[]” public static void main(String args[]) { //program code System.out.println("Hello world"); }

}

// Compile: javac A.java

// Run: java A Docsity.com

8

java.lang.System

  • Provide platform-independent access to system functions - Class may not be instantiated (you don’t create new objects of type java.lang.System)
  • Includes these data members

public static PrintStream err; public static PrintStream out; public static InputStream in;

  • System standard input, output & error streams
  • Includes this method

public static void exit(int status);

  • Exit the program Docsity.com

10

Example

public class print {

public static void main(String args[]) {

int r = 100;

System.out.print("value of r: ");

System.out.println(r);

System.exit(0);

Output: value of r: 100

11

Easier Technique: With Strings

public class print {

public static void main(String args[]) { int r = 100; System.out.println("value of r: " + r); // overloaded “+”: String concatenation }

} (^) Output:

value of r: 100

13

Control Structures: If-else

if (1 == 2) { System.out.println("Oh my god!");

} else {

System.out.println("Life is good.");

}

14

While Loop

while (x > 0) { System.out.print("x= " + x + "\n"); x--;

}

for loops , do while,

break & continue,

switch

Same as in C and C++ languages

16

// example of reading a string from the console import java.io.BufferedReader; import java.io.InputStreamReader;

public class ReadFromConsole { public static void main(String args[]) throws Exception { System.out.print("Please enter a string: "); BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in)); String s = bufIn.readLine(); System.out.println("You entered: " + s); } }

17

java.io.Console (Java SE 6)

Console console = System.console();

String formatString = "%1$4s %2$10s %3$10s%n";

console.printf(formatString, "Idx", "A", "B");

String name = console.readLine("[Please Provide Your Name]: "); // String returned from readLine doesn’t contain newline

http://www.javalobby.org/java/forums/t84689.html

19

import

  • Don’t need to import
    • Some common standard classes
    • E.g., java.lang.System java.io.PrintStream
  • Must explicitly import
    • User defined classes defined externally and various standard classes (e.g., provided in a typical Java installation)

20

Strings: java.lang.String

  • Sequences of characters
  • String objects are created by Java compiler when it encounters a string constant
  • String objects can be created, but once created, they cannot be modified - They are immutable - use StringBuffer to modify contents of a string
  • But, can re-assign to create new String objects as needed
  • Methods

public static String valueOf

  • Convert basic types (e.g., int, char) to strings
  • Other methods include
  • charAt, compareTo, equals, equalsIgnoreCase Docsity.com