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

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

Computational Expression

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

Full file at

Introduction to Programming Using Java (98-388)

Using Java Classes Fall 2018 Margaret Reid-Miller

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

AP Computer Science A

Program Fundamentals

Programming with Java

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

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

PIC 20A The Basics of Java

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

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

Full file at

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

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

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

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

Object-Oriented Programming

Array. Prepared By - Rifat Shahriyar

Peer Instruction 1. Elementary Programming

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

Chapter 2: Basic Elements of Java

Lecture Set 2: Starting Java

Basics of Java Programming

Java+- Language Reference Manual

Zheng-Liang Lu Java Programming 45 / 79

JAVA Programming Fundamentals

Lecture Set 2: Starting Java

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

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

CMPT 125: Lecture 3 Data and Expressions

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

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

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

Chapter. Let's explore some other fundamental programming concepts

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

Java Primer 1: Types, Classes and Operators

Chapter 2: Data and Expressions

Getting started with Java

Simple Java Reference

CS313D: ADVANCED PROGRAMMING LANGUAGE

PROGRAMMING FUNDAMENTALS

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

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

Introduction to Java & Fundamental Data Types

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

Primitive Data Types: Intro

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

Chapter 02: Using Data

VARIABLES AND TYPES CITS1001

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

ECE 122 Engineering Problem Solving with Java

Decaf Language Reference Manual

COMP 202 Java in one week

We now start exploring some key elements of the Java programming language and ways of performing I/O

QUIZ: What value is stored in a after this

6.096 Introduction to C++ January (IAP) 2009

Basic Computation. Chapter 2

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Visual C# Instructor s Manual Table of Contents

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Typescript on LLVM Language Reference Manual

Index COPYRIGHTED MATERIAL

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

IT 374 C# and Applications/ IT695 C# Data Structures

Java Notes. 10th ICSE. Saravanan Ganesh

Language Reference Manual

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

Chapter 2 ELEMENTARY PROGRAMMING

Chapter 3: Operators, Expressions and Type Conversion

Fall 2017 CISC124 9/16/2017

Reading Input from Text File

Chapter 2. Elementary Programming

What did we talk about last time? Examples switch statements

Datatypes, Variables, and Operations

2 rd class Department of Programming. OOP with Java Programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

CSCE 110 PROGRAMMING FUNDAMENTALS


Chapter 2: Basic Elements of C++

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

VLC : Language Reference Manual

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Methods CSC 121 Fall 2016 Howard Rosenthal

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Creating a C++ Program

1 Shyam sir JAVA Notes

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

CS260 Intro to Java & Android 03.Java Language Basics

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Transcription:

CSCI 150 Fall 2007 Java Syntax The following notes are meant to be a quick cheat sheet for Java. It is not meant to be a means on its own to learn Java or this course. For that you should look at your textbook and the sample code from class. 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. These notes will be updated as we learn more syntax. Enjoy! 1 Comments & Whitespace blanks and tabs ignored by Java compiler 2 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 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). They are listed on p17 of your text, but 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 150 Java Syntax 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 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) 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 150 Java Syntax 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 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!" ); else System.out.println( "b is false. Figures." ); repetition: while, for

CSCI 150 Java Syntax 4 ex. while( ImCrazy ) Hospital.checkIn( "Alexa" ); ex. for( int i=1; i < 10; i ++ ) { } 4 Methods System.out.println( i ); syntax: public static <return-type> <method-name>( arguments ) { <statements> } ex. public int squareme( int i ) { return (i * i); } 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 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 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

CSCI 150 Java Syntax 5 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 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

CSCI 150 Java Syntax 6 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 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: public or private 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

CSCI 150 Java Syntax 7 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. ex. class Movie { } private String name; public Movie( String s ) public tostring() 9 Useful Classes 9.1 The Math Class { name = s; } \\ This is the constructor { return name;} public addstudiotoname( String studio ) { name = studio + " s " + name } need to add import java.lang.*; at top of file. contains functions like abs, sqrt, pow,... 9.2 The Random Class need to add import java.util.*; at top of file methods functions like nextint(n) 9.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() 9.4 The File Class need to add import java.io.*; at top of file