Review of Java … or maybe not.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Java 1.5 & Effective Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Packages and Libraries M Taimoor Khan
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
10-Nov-15 Java Object Oriented Programming What is it?
Java Generics Compiled from Core Java Technologies Tech Tips By Billy B. L. Lim.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Review and Java Questions 13 June Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
28 Formatted Output.
Chapter 2 Basic Computation
JAVA MULTIPLE CHOICE QUESTION.
Formatting Output & Enumerated Types & Wrapper Classes
Introduction to Java part 2
Objectives You should be able to describe: Interactive Keyboard Input
Introduction to Computer Science / Procedural – 67130
University of Central Florida COP 3330 Object Oriented Programming
Multiple variables can be created in one declaration
Exercise on Java Basics
Chapter 3 Assignment Statement
Classes, Libraries & Packages
Java Programming: From Problem Analysis to Program Design, 4e
An Introduction to Java – Part I
Static and non-Static Chapter 5.
CMSC 202 Static Methods.
Introduction to Programming in Java
Defining Classes II.
Exercise on Java Basics
5 Variables, Data Types.
Unit-2 Objects and Classes
© A+ Computer Science - VARIABLES © A+ Computer Science -
CS Week 9 Jim Williams, PhD.
Introduction to Java part 2
Chapter 2: Basic Elements of Java
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Arrays and Collections
Chapter 2: Java Fundamentals
Java for IOI.
Programs and Classes A program is made up from classes
Classes and Objects Static Methods
Introduction to Java part 2
Java Basics Data Types in Java.
Java’s Central Casting
Creating and Modifying Text part 3
Using java libraries CGS3416 spring 2019.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Review of Java … or maybe not

Complete this class public _____ Foo _____ Bar _____ Comparable { /** name of this Foo */ private String name; /** initialize a new Foo */ public ____ Foo(String name) { ______________ = ________ } public String toString() { return name; } // what other method is required?

Make the name immutable public _____ Foo _____ Bar _____ Comparable { /** name of this Foo */ private _________ String name; ... immutable means you cannot change the value after it is set the first time.

Write an access & mutator for size public class TShirt { /** the size of the t-shirt (inches) */ private int size; /** Get the size of t-shirt (in inches) */ public ______ _________________ { } /** Set the size of this t-shirt. */ public ______ _______________ {

What is the difference between left & right? boolean int double char String Integer Date Scanner System

What are the other 3 primitive types? Name these Primitives ________ 32-bit whole numbers -9999 …. 0, 1, 0x10 ________ 64-bit whole numbers, written like 4L ________ 'ก', 'ด', \u0420 ________ 2.98E+08 ________ 8-bit values 0, 1, …, 255 (not int values) What are the other 3 primitive types? ________ ________ ________

Wrapper Classes Primitive Wrapper boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double double root = Math.sqrt( 2.0 ); Double d1 = new Double( root ); // same thing: automatic boxing Double d2 = root; // print as a string out.println( d2.toString( ) ); // static method to make a string out.println( Integer.toString( 2 ) );

Numeric Constants 1. What is the largest possible "int" value? int max = ___________________________; 2. What happens if you go beyond that? int tooBig = max + 1; a) exception thrown b) error c) converts to "long" d) wrap around to negative value 3. What are the largest and smallest (magnitude) double values?

What is the result? > char c = 'A'; > c+1 > ++c > (short) c > (byte) c > (char) 66 (int) c means "convert the value to an int". This is called a cast. You can use class name in a cast, too: (Character) c

What is the result? > c = 'ก' ; (gau gai) > (int) c > ++c > (char) c Java uses Unicode for char and Strings, but the output may not be readable if the output device doesn't use Unicode

How are these different? // 1 Billion + 2 Billion > 1000000000 + 2000000000 > 1000000000L + 2000000000L > 1E9 + 2E9 // in Java 7 you can write _ in numbers > 1_000_000_000L + 2_000_000_000L

Bizarre Numbers System.out.println( 12 ); System.out.println( 012 ); System.out.println( 0x12 ); System.out.println( 012 + 0x12 );

Which data type should you use for ... // the day of the month ________ day = 13; // 13th of Jan // population of the world ________ worldPop = (7 billion) ; // Bank account number // example: 001230055555 ________ accountNumber = . . .

How to Convert Primitive to Object? A List (like ArrayList) can only contain objects. How can we add primitive values (like int) to a List? List mylist = new ArrayList( ); int n = 51651111; mylist.add( n ); // Wait? How is this possible? Try in BlueJ Codepad: > List list = new ArrayList( ); > list.add( "hello" ) > list.add( 10 ) // what is being put in the List? > list.get( 0 ) "apple" (String) > list.get( 1 ) <object reference> (Integer)

Methods to convert to/from String int n = 29*31; // convert n to a String String product = Integer.toString(n); // parse integer value of a String String s = "123"; int m = Integer.__________ (s); // parse double value of String s double d = ______________________;

parseInt(string) versus valueOf(string) What is the difference? String s = "123"; Integer.parseInt( s ) Integer.valueOf( s )

Useful Constants in Numeric Wrapper classes What is the largest "int" value? What is the smallest "long" value? What is the range (smallest, biggest) of double? int maximum = long minimum = double minsize = double maxsize =

What value is after the biggest value? int n = Integer.MAX_VALUE; n = n + 1; System.out.println( n ); double d = Double.MAX_VALUE; d = d + 1; System.out.println( d ); d = d * 1.000001;

Packages Java uses packages to organize classes. Packages reduce size of name space and avoid name collisions (like Date in java.util and java.sql). Q: Which package contain these classes? Java language core classes (Object, String, System, ...). You never have to import this. Classes for input and output, like InputStream, FileReader Date classes and collections (List, ArrayList) Utilities Scanner, Arrays, Java Graphics frameworks (2 packages)

Name these Packages Java language core classes Object, String, System, Integer, Double, Math, Thread. Classes for input and output InputStream, BufferedReader, File Date/time classes, collections, utilities Calendar, Date, List, ArrayList, Set, Arrays, Formatter, Scanner java compiler always "import"s this package, so you don't need to.

Useful Packages added in Java 8 java.datetime New date and time classes. java.util.stream Classes and interfaces for "streams" style programming. A very cool and handy feature, found in other languages.

Packages What package are these classes in? String … System ... Scanner … Date … InputStream and FileReader …

What is the output? System.out.println( 3 + 4 );

Bit Operations > int a = 7; > int b = 10; > a & b > a | b

Arguments are passed by value public static void swap(int a, int b) { int temp = a; a = b; b = temp; } // elsewhere in the code... int m = 10; int n = 20; swap( m, n ); What is m?

Person class Person - name: String Person( name: String ) <<constructor>> Person( name: String ) getName( ): String setName( newname: String ): void toString(): String Person p = new Person( "Pee" ); p.setName( "Nong" ); System.out.println( p.toString() ); // prints "Nong"

Passing arguments, again public void swap(Person a, Person b) { Person temp = a; a = b; b = temp; } // elsewhere in the code... Person m = new Person( "Meaw" ); Person n = new Person( "Nok" ); swap( m, n ); What is m.toString() ?

How about this? public void swapName(Person a, Person b) { String temp = a.getName(); a.setName( b.getName() ); b.setName( temp ); } // elsewhere in the code... Person m = new Person( "Meaw" ); Person n = new Person( "Nok" ); swapName( m, n ); What is m.toString() ?

Difference between "==" and .equals? > Double x = new Double(10); > Double y = new Double(10); > x == y > String s = "yes"; > String t = "yes"; > s == t > String u = new String("yes"); > s == u > s.equals(u)

How to write equals( ) You should usually define equals( ) like this: public class Person { public boolean equals( Object other ) { … } Not like this: public boolean equals( Person other ) { ... }

WHERE ARE THE JDK CLASSES? JAR files What is a JAR file? Why use them? How to create one? WHERE ARE THE JDK CLASSES? Classes in the Java SE API: 4,024 in java 7 3,793 in java 6 3,279 in java 5.0 Actually there are MORE classes that this – some classes are not documented in the API. And this number does not include interfaces. These classes are on your computer (in the JDK). Where are they?

BlueJ IDE Layout Current Project Object Bench Code Pad