16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.

Slides:



Advertisements
Similar presentations
Language Features for JDK7 Neal Gafter Joshua Bloch Google.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Software Engineering Implementation Lecture 3 ASPI8-4 Anders P. Ravn, Feb 2004.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
JAVA PROGRAMMING PART II.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch and Neal Gafter Publisher:
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Indentation & Readability. What does this program do? public class Hello { public static void main ( String[] args ) { //display initial message System.out.println(
Java Gotcha's By Rick Mercer with help from the book:
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Java Producer-Consumer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Print Me Who’s Data? First Executed Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Unit Testing Part 2: Drivers and Stubs
Computer Science 320 A First Program in Parallel Java.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Output Programs These slides will present a variety of small programs. Each program has a compound condition which uses the Boolean Logic that was introduced.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Object Oriented Programming Lecture 2: BallWorld.
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
Staples are our staple Building upon our solution.
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
Examples of Classes & Objects
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
Exercise Java programming
using System; namespace Demo01 { class Program
Lecture 2: Data Types, Variables, Operators, and Expressions
Exceptions, Interfaces & Generics
Code Magnets problem for wk5
An Introduction to Java – Part I
null, true, and false are also reserved.
Introduction to Java Programming
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
Code Animation Examples
class PrintOnetoTen { public static void main(String args[]) {
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Agenda Types and identifiers Practice Assignment Keywords in Java
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter

Oddity The following method tests whether its argument is odd: public static boolean isOdd(int i) { return i % 2 == 1; } Does it work? It gives the correct answer for ¾ of the integers

Making change public class Change { public static void main(String[] args) { System.out.println( ); } }

Long Division public class LongDivision { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY); } } 5

Addition public class Addition { public static void main(String[] args) { System.out.println( l); } } 17777

Tweedledum Declare variables x and i such that x += i; is legal, but x = x + i; is not legal short x = 0; int i = ;

Tweedledee Declare variables x and i such that x = x + i; is legal, but x += i; is not legal Object x = "Hello "; String i = "world!";

+= public class PlusEquals { public static void main(String[] args) { int i = 2; i += 3.75; System.out.println(i); } } 5

Last Laugh public class LastLaugh { public static void main(String[] args) { System.out.print("H" + "a"); System.out.print('H' + 'a'); } } Ha169

Indecision public class Indecisive { public static void main(String[] args) { System.out.println(decision()); } static boolean decision() { try { return true; } finally { return false; } } } false

HelloGoodbye public class HelloGoodbye { public static void main(String[] args) { try { System.out.println("Hello world!"); System.exit(0); } finally { System.out.println("Goodbye world!"); } } } Hello world!

The reluctant constructor public class Reluctant { private Reluctant internalInstance = new Reluctant(); public Reluctant() throws Exception { throw new Exception("I'm not coming out!"); } public static void main(String[] args) { try { Reluctant b = new Reluctant(); System.out.println("Surprise!"); } catch (Exception e) { System.out.println("I told you so."); } } } Exception in thread "main" java.lang.StackOverflowError

Hello again public class Null { public static void main(String[] args) { ((Null)null).greet(); } public static void greet() { System.out.println("Hello world!"); } } Hello world!

The End