Eryq (Erik Dorfman) Zeegee Software Inc. eryq@zeegee.com. A Crash Course in Java 1



Similar documents
CS106A, Stanford Handout #38. Strings and Chars

public static void main(string[] args) { System.out.println("hello, world"); } }

java Features Version April 19, 2013 by Thorsten Kracht

Week 1: Review of Java Programming Basics

Java Interview Questions and Answers

INPUT AND OUTPUT STREAMS

Overview. java.math.biginteger, java.math.bigdecimal. Definition: objects are everything but primitives The eight primitive data type in Java

Computer Programming I

Quick Introduction to Java

Files and input/output streams

Introduction to Java

13 File Output and Input

Introduction to Java. CS 3: Computer Programming in Java

Java String Methods in Print Templates

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

CS170 Lab 11 Abstract Data Types & Objects

Variables, Constants, and Data Types

Java CPD (I) Frans Coenen Department of Computer Science

JAVA - FILES AND I/O

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

AP Computer Science Java Subset

Java from a C perspective. Plan

Chapter 2: Elements of Java

VB.NET - STRINGS. By calling a formatting method to convert a value or object to its string representation

Creating a Simple, Multithreaded Chat System with Java

CS 111 Classes I 1. Software Organization View to this point:

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Building a Multi-Threaded Web Server

Introduction to Java Lecture Notes. Ryan Dougherty

Stream Classes and File I/O

JAVA IN A NUTSHELL O'REILLY. David Flanagan. Fifth Edition. Beijing Cambridge Farnham Köln Sebastopol Tokyo

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

What is an I/O Stream?

J a v a Quiz (Unit 3, Test 0 Practice)

Java Basics: Data Types, Variables, and Loops

Chapter 13 - Inheritance

Chapter 2 Introduction to Java programming

1 of 1 24/05/ :23 AM

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

WRITING DATA TO A BINARY FILE

An Overview of Java. overview-1

Introduction to Data Structures

C++ INTERVIEW QUESTIONS

Performance Improvement In Java Application

Text Processing In Java: Characters and Strings

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Merkle Hash Trees for Distributed Audit Logs

CSC 551: Web Programming. Fall 2001

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

D06 PROGRAMMING with JAVA

Contents. 9-1 Copyright (c) N. Afshartous

3.GETTING STARTED WITH ORACLE8i

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Java SE 7 Programming

Java Crash Course Part I

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Web Programming Step by Step

For live Java EE training, please see training courses

CmpSci 187: Programming with Data Structures Spring 2015

arrays C Programming Language - Arrays

LAB 3 Part 1 OBJECTS & CLASSES

Java Programming Fundamentals

Carron Shankland. Content. String manipula3on in Java The use of files in Java The use of the command line arguments References:

Python Lists and Loops

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Member Functions of the istream Class

An overview of FAT12

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Lab Experience 17. Programming Language Translation

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 3: Restricted Structures Page 1

Basic Programming and PC Skills: Basic Programming and PC Skills:

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Compiler Construction

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Chapter 2 Elementary Programming

Semantic Analysis: Types and Type Checking

Lecture 5: Java Fundamentals III

Programmation 2. Introduction à la programmation Java

Readings and References. Topic #10: Java Input / Output. "Streams" are the basic I/O objects. Input & Output. Streams. The stream model A-1.

Storing Measurement Data

Sample CSE8A midterm Multiple Choice (circle one)

Storage Optimization in Cloud Environment using Compression Algorithm

Maria Litvin Phillips Academy, Andover, Massachusetts. Gary Litvin Skylight Publishing, Andover, Massachusetts

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

Stack Allocation. Run-Time Data Structures. Static Structures

Continuous Integration Part 2

CS 1302 Ch 19, Binary I/O

MS Access: Advanced Tables and Queries. Lesson Notes Author: Pamela Schmidt

This example illustrates how to copy contents from one file to another file. This topic is related to the I/O (input/output) of

Transcription:

A Crash Course in Java Eryq (Erik Dorfman) Zeegee Software Inc. eryq@zeegee.com 1

Because without text, life would be nothing but 6E 75 6D 62 65 72 73 2

java.lang.string Not a primitive type, but it is a special class: String literals are automatically converted to instances of String. Java uses + operator for concatenation. A + will promote its operands to String if either operand is a String (e.g., "Super" + 8 = "Super8" ). Special tostring() method of Object for turning that object into a String. 3

String access charat(i) compareto(s) equals(s) equalsignorecase(s) indexof(sub) lastindexof(sub) length() startswith(s) endswith(s) substring(from, thru) Get char at given index (0-based). Is this string <, =, > the other? Does this string have the same characters as the other string. 0-based pos'n of leftmost/rightmost occurrence of given substring/char. Get number of characters. Does this string start/end with given string? Return new substring of this. 4

String "manipulation" replace(oldc, newc) touppercase() tolowercase() trim() Return new string with char replaced. Return new, up/down-cased string. Return new string with leading and trailing whitespace removed....hey, why do these all create new Strings...? 5

Strings are constant! There's no way to actually modify a String. Rationale: when you know object is constant, tremendous amount of optimization is achievable, esp. in a multithreaded environment. Since many apps are string-heavy, this gives major performance gains. Think of this classname as really being "StringConstant". 6

java.lang.stringbuilder An editable String-like object. Lacks many "interesting" methods of String. Primarily append(x) and insert(index, x): Work for most primitive types x, and also any Object (stringified with tostring()). Methods can be chained together. How + really works: "Hello " + person + '\n' (new StringBuilder()).append("Hello ").append(pperson).append('\n').tostring() 7

StringBuilder optimization Predeclare approx size in constructor. Minimizes reallocs. Especially important for many small appends. Defer tostring() until "edits" are done. StringBuffer's tostring() does copy-on-write: lets new String share its internal char buffer, copies buffer only if subsequent edits made. If no edits, no copy! Don't mix appends with + Redundant and inefficient. Pick one or the other. 8

Beware substrings! To avoid unnecessary memory allocations, substring() returns String with start/end "pointers" into parent String. Perfectly legit, since Strings are constant. (See? Optimizations abound!) However, that means parent can't be garbage-collected until all children are gone! static String[] prefix = new String[10000];... while (averylongline = readline(input)) { prefix[i++] = averylongline.substring(1, 3); } 9

Constructing safer substrings Solution is simple: create copy of substring and let the substring be garbage-collected: while (averylongline = readline(input)) { prefix[i++] = new String(aVeryLongLine.substring(1, 3)); } 10

String input/output Remember: a String is composed of an array of char... not an array of byte! If you want to read/write chars or Strings, use the classes in java.io whose names end with Reader/Writer (e.g. FileReader). If you want to read/write bytes, use the classes in java.io whose names end with Stream(e.g. InputStream). InputStreamReader reads bytes into chars. OutputStreamWriter writes chars as bytes. 11