Java Foundations: Unit 3. Parts of a Java Program

Similar documents
The While Statement. If boolean expression is true execute statement; continue to execute statement so long as boolean expression is true.

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

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

String. Other languages that implement strings as character arrays

An overview of Java, Data types and variables

STRINGS AND STRINGBUILDERS. Spring 2019

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

Packages. 1. Every Java object resides in a package. 2. An object s package is explicitly declared on the first line of code in a.

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

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

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

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

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

Software and Programming 1

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

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

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

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

- Thus there is a String class (a large class)

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Full file at

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

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

Welcome to the Using Objects lab!

Program Fundamentals

Java Bytecode (binary file)

CS 177 Recitation. Week 1 Intro to Java

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

CS251L REVIEW Derek Trumbo UNM

Index COPYRIGHTED MATERIAL

Crash Course Review Only. Please use online Jasmit Singh 2

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Software and Programming 1

COMP-202: Foundations of Programming

C02: Overview of Software Development and Java

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Java Fall 2018 Margaret Reid-Miller

Java Basic Programming Constructs

Mobile App:IT. Methods & Classes

Programs, Statements, Variables

A couple of decent C++ web resources you might want to bookmark:

Welcome to the Using Objects lab!

CHAPTER 7 OBJECTS AND CLASSES

Getting started with Java

Pace University. Fundamental Concepts of CS121 1

Programming with Java

Introduction to Java

Introduction Basic elements of Java

Testing Driven Development. Advanced Programming

Table of Contents Date(s) Title/Topic Page #s. Abstraction

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

Introduction to Programming Using Java (98-388)

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

CS 11 java track: lecture 1

INFO Object-Oriented Programming

Lecture 2: Intro to Java

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

Getting started with Java

Lab 14 & 15: String Handling

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

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Introduction to Programming (Java) 4/12

More non-primitive types Lesson 06

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

1. Download the JDK 6, from

Primitive Data and Objects


Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

13 th Windsor Regional Secondary School Computer Programming Competition

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

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

Programming Language Concepts: Lecture 2

Notes from the Boards Set BN19 Page

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

CST242 Strings and Characters Page 1

1.1 Your First Program

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

CHAPTER 7 OBJECTS AND CLASSES

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

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

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

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Chapter 1 Introduction to Java

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS 1301 Ch 8, Part A

Important Java terminology

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Using Java Classes Fall 2018 Margaret Reid-Miller

Variables of class Type. Week 8. Variables of class Type, Cont. A simple class:

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

1.1 Your First Program

Transcription:

Java Foundations: Unit 3 Parts of a Java Program

class + name public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); A class creates a new type, something which can contain data and perform actions. In Java nothing can happen outside a class. Every class has a name, such as Turtle.

public + class public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); Declaring a class public makes it useable by any other class. Classes can also be private.

Class-level Braces public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); Java uses braces to group things together. In this case they delimit the start and end of the class declaration.

main public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); When you start a program, Java has to know where to begina execution. Given the command: java HelloWorld Java looks for a class file named HelloWorld; within the class file it looks for a main method, and starts the program there.

public main public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); Declaring a method public means it can be used by anyone. Methods may also be private.

static main public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); Declaring a method static means it can be used at any time. If you don t declare a method static it can only be invoked on a specific object. Static methods are called class methods.

void main public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); In Java every method has a type, and many are associated with values of that type. Math.log is type double, and computes the logarithm of a number: double exponent = Math.log( datapoint ); Void means that a method cannot be associated with a value.

String[] args public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); When executing a program you can supply it with input via command line arguments. The Windows command > notepad HelloWorld.java has one command line argument. The Java command > java MyApp dick jane sally has three command line arguments which can be accessed through args.

Indentation public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); In Java you can (usually) put white space and line breaks wherever you like. Use indentation to delimit groupings of code in your program.

Brace Alignment public class HelloWorld public static void main( String[] args ) System.out.println( Hello world! ); Aligning matching braces makes it easier to decipher.

Exercises 1. Go into your HelloWorld source code and change main to sam. Compile it with javac. What happens when you try to execute it with java? 2. In your HelloWorld program, add the code for GetGreeting (below), and change the println as follows: public class HelloWorld static public void main( String[] args ) System.out.println( getgreeting() ); static public String getgreeting() return "Hi there, world"; Explain: What happened when you substituted getgreeting() for "HelloWorld"? What is the purpose of String in the declaration of getgreeting? What is the purpose of static?

Strings

String Objects: Immutability A String object stores a sequence of Unicode characters. Strings are immutable; once created they cannot be changed. String bob = abra cada bra ; System.out.println( bob.trim() ); System.out.println( bob ); The trim method creates a new String object that contains the characters from the original String without the leading and trailing whitespace. The original String is not changed.

String Concatenation Java has special support for String objects. In particular, two strings can be concatenated by adding them together: String manny = the time has come, ; String moe = the walrus said, ; String jack = manny + moe; // ^^^^ // The time has come, the walrus said,

String Equality (1) Two different String objects can contain the same string of characters. String str1 = "Red dog, blue dog"; String str2 = "Red dog, blue dog"; String str3 = "Red dog, ";... 2526 Red dog, blue dog str3 = str3 + "blue dog";... 2734 Red dog, blue dog str1 and str2 refer to the same object str1 and str3 refer to the different object; however, both objects contain the same value

String Equality (2) To test two strings for equality, use the equals method in the String class. if ( str1.equals( str3 ) ) System.out.println( "equal" );

String Order To test the order of two Strings use compareto compareto evaluates to an integer: > 0, if the first string is greater than the second; 0 if the two strings are equal; or < 0 if the first string is less than the second: String onestr; String twostr;... int order = onestr.compareto( twostr ) if ( order > 0 ) System.out.println( onestr greater ); else if ( order == 0 ) System.out.println( strings equal ); else if ( order < 0 ) System.out.println( twostr greater );

String Collation String one is less than String two if one comes in the dictionary before two. Java uses a dictionary in which order is determined by unicode value: All integers are less than all letters. All upper case letters are less than all lower case letters. 42 is less than elephant elephant is greater than Zulu

Common String Methods Method charat Action Return the unicode character at a specified index. comparetoignorecase Perform a case-insensitive comparison of two strings. endswith indexof substring tolowercase trim Returns true if one string ends with another. Returns the index of a character within a string. Returns a substring of a string. Evaluates to a new string in which all characters in the old string are changed to lower case. Removes whitespace from the beginning and end of a string.

String Class Documentation See the complete documentation for the String class in the Java API.

Exercises 1. Given the following pairs of string literals, write a program to determine which member of the pair is greater than the other. 200 and 30 400 and 40 Zulu and Albatross Market and market If you don t know how to form an if statement, just print the value of the comparison and eye-ball it to see what the relative order is: System.out.println( 200.compareTo( 30 ));

StringBuilder (1) Because strings are immutable, dynamically building a string can be a very expensive operation: String prefix = "Rev4.2\t"; String author = "\tbob"; String body = "Dow Jones down 2%"; String message = prefix + body + author;

StringBuilder (2) StringBuilder provides a mutable class for dynamically assembling strings: String prefix = "Rev4.2\t"; String author = "\tbob"; String body = "Dow Jones down 2%"; StringBuilder bldr = new StringBuilder(); bldr.append( prefix ); bldr.append( body ); bldr.append( author ); String message = bldr.tostring();

StringBuilder (3) See the complete StringBuilder class documentation.