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”
- 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
- Object variables
- Contain references to objects
- Two object variables can refer to the same object
- Simple type variables (e.g., int)
- 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