Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Similar documents
Getting started with Java

CS 11 java track: lecture 1

An overview of Java, Data types and variables

Programming Language Concepts: Lecture 2

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

CS2141 Software Development using C/C++ C++ Basics

Java Bytecode (binary file)

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Lec 3. Compilers, Debugging, Hello World, and Variables

Chapter Two Bonus Lesson: JavaDoc

Java Basic Programming Constructs

CS 106 Introduction to Computer Science I

Unit 14. Passing Arrays & C++ Strings

Selected Questions from by Nageshwara Rao

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

C and C++ I. Spring 2014 Carola Wenk

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Code Ninjas: Introduction to Computer Science. Macomb Science Olympiad Presented by Swati Dharia

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Assumptions. History

Programming Language Concepts: Lecture 2

CE221 Programming in C++ Part 1 Introduction

Array. Prepared By - Rifat Shahriyar

Section 2.2 Your First Program in Java: Printing a Line of Text

Getting started with Java

Lecture 2, September 4

Kickstart Intro to Java Part I

Object Reference and Memory Allocation. Questions:

INFO Object-Oriented Programming

CS212 Midterm. 1. Read the following code fragments and answer the questions.

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

1. Download the JDK 6, from

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

Functions and Methods. Questions:

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CS 376b Computer Vision

Building Java Programs

Full file at

String is a collection of alphanumeric & special characters and escape sequences enclosed in (Double quotes).

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Introduction to Programming Using Java (98-388)

Building Java Programs

Software and Programming 1

CS 231 Data Structures and Algorithms, Fall 2016

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Introduction to Java

13 th Windsor Regional Secondary School Computer Programming Competition

Computer Science II Lecture 1 Introduction and Background

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

1007 Imperative Programming Part II

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Software and Programming 1

DM550 Introduction to Programming part 2. Jan Baumbach.

Question: Total Points: Score:

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

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

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

1 Unit 8 'for' Loops

Lab 1: First Steps in C++ - Eclipse

CA341 - Comparative Programming Languages

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Introduction to Java

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Place your name tag here

the gamedesigninitiative at cornell university Lecture 6 C++: Basics

JThreads/C++ Version 2.0.0b1. IONA Technologies PLC

BTE2313. Chapter 2: Introduction to C++ Programming

Dynamic arrays / C Strings

Chapters 1-4 Summary. Syntax - Java or C? Syntax - Java or C?

Manual for Basic Java

CS11 Java. Fall Lecture 1

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Linked List using a Sentinel

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

C++ basics Getting started with, and Data Types.

Parallele Numerik. Blatt 1

Program Fundamentals

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Notes from the Boards Set BN19 Page

CSE 333 Lecture 9 - intro to C++

CS111: PROGRAMMING LANGUAGE II

Computer Science II (20082) Week 1: Review and Inheritance

Expression: Expression: Statement: Output: Expression: Expression:

Introduction to Software Development (ISD) Week 3

Java Foundations: Unit 3. Parts of a Java Program

C++_ MARKS 40 MIN

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Programs, Statements, Variables

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

BM214E Object Oriented Programming Lecture 8

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Transcription:

Today... Java basics S. Bowers 1 of 8

Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How is the above different than in C++? main is defined within a class main takes an array of Strings (command-line args) the [] s are in the wrong place! main s return type is void main is declared to be public and static Java methods declare their visibility (instead of sections in C++) static is the same here as in C++ (method lives with the class) the class is declared public classes in Java can have public or package (default) access instead of cout we use System.out.println(...) method S. Bowers 2 of 8

In C++: #include <iostream> using namespace std; int main(int argc, char **argv) { cout << "Hello World!" << endl; // or: char *argv[] In this statement... System.out.println("Hello World!"); System is a standard Java class out is a public, static variable (field) of System println is a method of the object out print is similar, but doesn t include a newline Memorize this! Public static void main string bracket bracket args S. Bowers 3 of 8

Working with String objects The String class is a lot like string in C++ String s = "Hello"; // create a new string object s System.out.println(s); // print "Hello" s.length(); // returns 5 char e = s.charat(1); // returns char e s.indexof(e); // returns 1 here (-1 if not found) String h = s + " " + "World!"; // string concatenation System.out.println(h); // prints "Hello World!" Note: these are hints for HW 1 To find out more about String, see the Java API! http://docs.oracle.com/javase/6/docs/api/ http://docs.oracle.com/javase/6/docs/api/java/lang/string.html Compiling and running To compile and run on ada: $ javac HelloWorld.java $ java HelloWorld Hello World! S. Bowers 4 of 8

Java Data Types Like C++, all variables must have a declared type Java distinguishes primitive from non-primitive types The primitives are the atomic values (numbers, characters, etc.) largely the same as C++, e.g.: int, char, double, etc. some differences though e.g., boolean s are true or false (instead of 0 and not 0, as in C++) Q: How would we define a public class method that always returns true? Note: class methods are static, instance methods are non-static public static boolean alwaystrue() { return true; Array and reference types (think objects) are the non-primitives more on these later S. Bowers 5 of 8

Exercise Write a public class method that: takes two String values (str1 and str2) and returns true if they start with the same character and returns false otherwise public static boolean samefirstchar(string str1, String str2) { if(str1.charat(0) == str2.charat(0)) return true; else return false; Now write a public class method that checks if all characters match public static boolean samestrings(string str1, String str2) { if(str1.length()!= str2.length()) return false; for(int i = 0; i < str1.length(); i++) if(str1.charat(i)!= str2.charat(i)) return false; return true; Note that these are missing the case when args are null (more later) S. Bowers 6 of 8

Javadoc Comments Java has three types of comments: one-line comment: // one-liner int v = 100; // v is velocity in mph standard multi-line comment: /* latte mocha dopio usually like this in Java: /* * latte * mocha * dopio a javadoc (multi-line) documentation comment /** * The velocity in mph *... note the double ** s on the first line S. Bowers 7 of 8

Doc comments used for class, interface, field, constructor, and method comments Doc comments have special syntax! The first paragraph is the description The following lines are block tags (or just tags) tags are denoted by a leading @ character the first one ends the description An example: /** * The <code>helloworld</code> class implements * the Java Hello World program. * @author Shawn Bowers * @version Assignment 0, 1/24/2012 public class HelloWorld { /** * Prints the hello world string * @param args the command line arguments (not used) public static void main(string[] args) { System.out.println("Hello World!"); S. Bowers 8 of 8