c) And last but not least, there are javadoc comments. See Weiss.

Similar documents
boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Full file at

Computational Expression

Introduction to Programming Using Java (98-388)

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Using Java Classes Fall 2018 Margaret Reid-Miller

Program Fundamentals

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

AP Computer Science A

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CS260 Intro to Java & Android 03.Java Language Basics

Programming with Java

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

1 Shyam sir JAVA Notes

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Full file at

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Points To Remember for SCJP

PIC 20A The Basics of Java

Outline. Why Java? (1/2) Why Java? (2/2) Java Compiler and Virtual Machine. Classes. COMP9024: Data Structures and Algorithms

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Array. Prepared By - Rifat Shahriyar

Decaf Language Reference Manual

A variable is a name for a location in memory A variable must be declared

Lecture Set 2: Starting Java

Getting started with Java

Index COPYRIGHTED MATERIAL

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Tools : The Java Compiler. The Java Interpreter. The Java Debugger


5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Lecture Set 2: Starting Java

Java Primer 1: Types, Classes and Operators

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Datatypes, Variables, and Operations

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

CSCI 355 Lab #2 Spring 2007

Simple Java Reference

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Outline. Java Compiler and Virtual Machine. Why Java? Naming Conventions. Classes. COMP9024: Data Structures and Algorithms

VARIABLES AND TYPES CITS1001

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

2 rd class Department of Programming. OOP with Java Programming

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Object-Oriented Programming

IC Language Specification

COMP 202 Java in one week

JAVA Programming Fundamentals

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

Operators and Expressions

CMPT 125: Lecture 3 Data and Expressions

Primitive Data Types: Intro

Chapter 2. Elementary Programming

Java Overview An introduction to the Java Programming Language

Visual C# Instructor s Manual Table of Contents

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CEN 414 Java Programming

DM550 Introduction to Programming part 2. Jan Baumbach.

Chapter 2: Basic Elements of Java

A Short Summary of Javali

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Chapter 2 ELEMENTARY PROGRAMMING

Java Bytecode (binary file)

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Basics of Java Programming

Java+- Language Reference Manual

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Java Object Oriented Design. CSC207 Fall 2014

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Zheng-Liang Lu Java Programming 45 / 79

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

4 Programming Fundamentals. Introduction to Programming 1 1

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

JAVA Ch. 4. Variables and Constants Lawrenceville Press

CSCI 355 LAB #2 Spring 2004

Introduction to Java & Fundamental Data Types

6.096 Introduction to C++ January (IAP) 2009

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Week 6: Review. Java is Case Sensitive

JAVA MOCK TEST JAVA MOCK TEST II

Computer Components. Software{ User Programs. Operating System. Hardware

Peer Instruction 1. Elementary Programming

Basic Computation. Chapter 2

Chapter. Let's explore some other fundamental programming concepts

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CHAPTER 2 Java Fundamentals

DM503 Programming B. Peter Schneider-Kamp.

PROGRAMMING FUNDAMENTALS

Chapter 2: Data and Expressions

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Transcription:

CSCI 151 Spring 2010 Java Bootcamp The following notes are meant to be a quick refresher on Java. It is not meant to be a means on its own to learn Java. For that you would need a lot more detail (for which the early textbook chapters is a good source). Furthermore, this is not an exhaustive listing of all of Java s capabilities, as we still have many a fundamental concept to cover in this course, and in other courses down the road. Enjoy! 1 Comments & Whitespace blanks and tabs ignored by Java compiler 3 types of comments: a) // This is a comment b) /* This is also a comment */ /* /* I can be nested */ */ /* And I can span multiple lines */ 2 Tokens c) And last but not least, there are javadoc comments. See Weiss. 2.1 Keywords These are words reserved in Java for special use, so you cannot use them as your own identifiers (variable, method, & class names). A sampling is: boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while 2.2 Identifiers is a name for a variable/class/method, i.e. myvar, booya, IHateArtichokesAndOlives,... must begin with a letter, underscore (_), or currency symbol ($) may contain any number of digits, letters, underscores, or currency symbols after the first character, i.e. _83yy$z Java is case-sensitive, i.e. IHateArtichokes is different from ihateartichokes must not use the keywords as they already have special meaning convention: class names should begin with an uppercase letter, as in MyClass. Method names should begin with a lowercase letter, as in main.

CSCI 151 Java Bootcamp 2 2.3 Primitive Data Types there are eight primitive types: boolean, char, byte, short, int, long, float, double we typically use boolean, int, double, char boolean: true, false (no 0s and 1s allowed) char: a, b, c,... (any 16-bit unicode character) can convert chars to ints, and vice-versa ex. char whatami = 97; // This is the character s a! Ooooo use actual character, i.e. char = X ; or, use escape characters: \t (tab), \n (newline), \" ( ), \ ( ), \\ ( \) int: 32 bits. Ranges from -2147483648 to 2147483647 double 64 bits. 2.4 Operators use decimal point i.e.,.10, 1.0, 1.0 use scientific notation, i.e. 1e-6 1.23E02 increment / decrement ex. x = x+1 could be written as x++ or ++x ex. artichoke = 1; baloney = artichoke++; baloney gets the current value of artichoke (i.e. 1), then artichoke increments to 2 modulus (%) (the remainder operator) ex. 24 % 5 gives 4 before of = (assign) vs == (equals)! ex. int a = 5; while( a = 5 ) {a = 4; } // This will loop forever object creation (new) instanceof (see Weiss) object access (.) ex. Foobar fb = new Foobar(); fb.fixme(); // Creates new object // Accesses a method from the object array element access( [] ) ex. int[] myarray = {5, 4, 3, 2, 1}; int temp = myarray[1]; // temp will have the value 4

CSCI 151 Java Bootcamp 3 2.5 Boolean Expressions true, false are the simplest boolean expressions can use relational operators to construct boolean expressions ex. i < 5, s.charat(0) == s.charat(6), s.equals( "trogdor" ) can use boolean operators to combine boolean expressions ex. ( (n==1) (n==2) ) 2.6 Punctuation use ( ) for expressions and methods use ; for ending statements use { } for blocks of statements 3 Statements empty statement: ; block: any collection of statements inside { } expression: assignments, increment / decrement, method calls, object creation declaration: must tell Java about a variable before using it ex. int a; // Declaring the variable a to be an int a = 5; // Using the variable a assignment: to store a value in a variable method call object creation selection: if-else, if-else if relations: <, >, <=, >=, ==,! = logic: && (and), (or),! (not) values: true, false ex. if( b ) // Or, for example if( x < 5 ) System.out.println( "b is true!" ); ex. if( b ) System.out.println( "b is still true!" );

CSCI 151 Java Bootcamp 4 else System.out.println( "b is false. Figures." ); repetition: while, do-while, for ex. while( ImCrazy ) ex. do{ Hospital.checkIn( "Alexa" ); a--; } while( a > 10 ) ex. for( int i=1; i < 10; i ++ ) { } 4 Methods System.out.println( i ); syntax: modifiers <return-type> <method-name>( arguments ) [throws exceptions] { <stmts> } ex. public int squareme( int i ) { return (i * i); } modifiers are public: allows method to be inherited and accessed from outside the class protected: allows method to be inherited, but prohibits it from being accessed from outside the class private: prohibits method from being inherited or accessed from outside the class static: limits the number of instances of this method to one and allows access without an object final: prohibits method from being overridden (inherited) return type can be void (returns no value), or any type. arguments are of the form <type> <varname>, <type> <varname>,..., or no arguments at all before the body of the method, may possibly throw exceptions ex. public static readfile( String fname ) throws FileNotFoundException {...} if the method parameters are primitive types then the value of the actual parameter is copied into the formal parameter, that is, the method cannot change the value of an actual parameter if the method parameters are object/complex/reference types, then the pointer to the actual parameter is copied into the formal parameter, and you can change the value of an actual paramter

CSCI 151 Java Bootcamp 5 overloading: writing multiple methods with the same name but different signature (i.e. different order of arguments, types of arguments, number of arguments, or any combination of the three) 5 Creating Objects & References to create an object (which is an instance of a class), use a constructor call: ex. Movie m = new Movie( "Revenge of the Nerds" ); null is the value that represents no object the keyword this represents a reference to the current object ex. class Alien { private String planet; private Alien friend; // name of home planet // friend of current planet public Alien( String planet ) { this.planet = planet; } Java does not allow you to store the actual object in a variable; instead, you need to use a variable of the object s type that stores the address of this object. This kind of variable is called a reference variable. ex. Aardvark a = new Aardvark(); a is a variable of type Aardvark a stores the address of a newly created Aardvark if you print a, you ll see the address value (Aardvark@...), not anything actually *useful*, God forbid. changing the contents of a reference variable means storing the address f a different object: ex. Alien bossalien; Alien a1 = new Alien(); Alien a2 = new Alien(); bossalien = a1; // boss now contains the address of a1 passing an object to a method really means passing a reference to that object returning an object from a method really means returning a reference to that object

CSCI 151 Java Bootcamp 6 6 Arrays arrays are objects must use new! all elements of array must have same type indexed from 0. Indices must be integers, or be expressions that evaluate to integers syntax to declare: <type>[] <varname>; or <type> <varname>[]; ex. Aliens[] starsinmeninblackii; // An array of Aliens syntax to assign: <varname> = new <type>[size]; ex. starsinmeninblackii = new Aliens[ 3 ]; int[] somearray = new int[ 8 ]; can find length easily using <varname>.length ex. starsinmeninblackii.length is 3. arrays are 0-indexed: ex. somearray[ 0 ] is the first element of somearray. ex. somearray[ somearray.length -1 ] is the last element of somearray. can make arrays of objects (i.e. arrays of references), and multidimensional arrays 7 Strings and Characters strings are reference types (use String class) string literal: "yodelayheehoo" is an instance of class String empty string: "" concatenation is easy: ex. "you make me complete" + "ly miserable" "you make me completely miserable" even concatenating non-strings is easy: ex. "mumbo number " + 5 "mumbo number 5" must put String on one line. One! multiple String constructors: String s0 = "yo!"; String s1 = new String(); // creates a string literal // creates an empty string

CSCI 151 Java Bootcamp 7 String s2 = new String( "whassaaaap" ); // creates string of "whassaaaap" Strings are immutable once created, they cannot change! But you can copy them... strings indexed from 0 ex. String s = new String( "Ra ra Rhasputin" ); char c = s.charat( 1 ); String t = s.substring( 0, 5 ); // c will hold the character a // t holds "Ra ra" Do not use == to compare strings, use s1.equals(s2). Yes, sometimes == will work, but not al the time, so just stick with the equals, okay!?! 8 Classes blueprint / mold for creating objects syntax: <modifiers> class <classname> { <fields>; <constructors>; <methods>; } modifiers same as for methods fields and methods are called members fields represent properties of a class, methods are for accessing and modifying these properties fields get default values of their respective types every method can see any other method in the same class in any order constructors return a reference to the newly created object they do not have a return type syntax: <modifiers> <classname>( <arguments> ) { <body> } every class has at least one constructor, even if you don t write one. The default is classname() {}, a.k.a. the empty constructor. if you want to call another class constructor or the super s constructor in your constructor, you must do it in the first line. otherwise, the super s constructor is automatically called (the empty one) ex. class Movie { } private String name; public Movie( String s ) public tostring() { name = s; } \\ This is the constructor { return name;} public addstudiotoname( String studio ) { name = studio + " s " + name }

CSCI 151 Java Bootcamp 8 9 Inheritance all classes inherit from class Object use the keyword extends to inherit from a class subclass extends a superclass to take advantage of superclass s existing functionality subclass can extend from at most one superclass public and protected methods and fields are inherited unless overridden private fields and methods do technically do not inherit, but may be indirectly accessed using a public/protected member which does inherit private fields and methods are accessed from the same class that they are called from use super to access a superclass member, unless that member is not visible (i.e. private) 10 Useful Classes 10.1 The Math Class need to add import java.lang.*; at top of file. contains functions like abs, sqrt, pow,... 10.2 The Random Class need to add import java.util.*; at top of file methods functions like nextint(n) 10.3 The Scanner Class need to add import java.util.*; at top off file can construct for the console or from a File, or even from a String ex. Scanner input = new Scanner( new File( "input.txt" ) ); Scanner console = new Scanner(System.in); Scanner console = new Scanner( "it s just string" ); method nextline(), nextint() 10.4 The File Class need to add import java.io.*; at top of file