Program Statements Primitive Data Types and Strings.

Slides:



Advertisements
Similar presentations
CSC 1051 – Algorithms and Data Structures I
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Basic Syntax: Data & Expressions
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
INF 523Q Chapter 2: Objects and Primitive Data (Examples)
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
String Escape Sequences
© 2004 Pearson Addison-Wesley. All rights reserved1-1 Intermediate Java Programming Lory Al Moakar.
2.2 Information on Program Appearance and Printing.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Introduction to Java Chapters 1 and 2 The Java Language – Section 1.1 Data & Expressions – Sections 2.1 – 2.5 Instructor: Scott Kristjanson CMPT 125/125.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Chapter 2 Data and Expressions Part One. © 2004 Pearson Addison-Wesley. All rights reserved2-2/29 Data and Expressions Let's explore some other fundamental.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Chapter 2 Data and Expressions Java Software Solutions Foundations of Program Design 1.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: –character strings –primitive data –the declaration.
Chapter 2 print / println String Literals Escape Characters Variables / data types.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Chapter 2 1.What is the difference between print / println 2.What are String Literals 3.What are the Escape Characters for backslash, double quotataions,
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
1 Chapter 2: Objects and Primitive Data Chapter 2 focuses on: predefined objects primitive data the declaration and use of variables expressions and operator.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Object Oriented Programming Idea Computer program may be seen as comprising a collection of objects Object Fundamental entity in a JAVA program Used to.
Chapter 02 Data and Expressions.
Introduction to Java Programming
Web Programming Chapter 1 : Introduction to Java Programming
Intermediate Java Programming
hardware bits and bytes Java
CMPT 126 Java Basics.
Chapter 2 Data and Expressions
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
CSC 1051 – Data Structures and Algorithms I
Multiple variables can be created in one declaration
Chapter 2 Data and Expressions
Escape Sequences What if we wanted to print the quote character?
Data and Expressions Part One
Chapter 2 Data and Expressions
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
String Concatenation Objectives
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
Module 2 - Part 1 Variables, Assignment, and Data Types
Module 2 - Part 1 Variables, Assignment, and Data Types
Chapter 2 Data and Expressions
Chapter 2 Data and Expressions
CSC 1051 – Data Structures and Algorithms I
Instructor: Alexander Stoytchev
Java Programming Presented by Dr. K. SATISH KUMAR,
Module 2 - Part 1 Variables, Assignment, and Data Types
Presentation transcript:

Program Statements Primitive Data Types and Strings

ICS111 - Java Programming Blanca Polo 2 Strings  A string is a set of characters surrounded by quotes.  Examples: ”What a way to spend the summer!!!" ”I am learning UNIX and JAVA" ”N"  Strings are JAVA objects that is why you didn’t see the word String in the set of reserved words.

ICS111 - Java Programming Blanca Polo 3 String Declaration  You can declare a string the same way that you declared a primitive data type  Examples: String summer = ”What a way to spend the summer!!!”; String skills =”I am learning UNIX and JAVA”; String oneLetter= ”N”; Notice the uppercase Note: Class names begin with upper-case Variable names begin with lower-case Primitive data types begin with lower case

ICS111 - Java Programming Blanca Polo 4 The println Method  In the HelloWorld program used the println method to print a String of characters  The System.out object represents a destination (the monitor screen) to which we can send output System.out.println (”I love my JAVA class, it is fun."); object method name information provided to the method (parameters)

ICS111 - Java Programming Blanca Polo 5 The print Method  The print method is similar to the println method, except that it does not advance to the next line  Therefore anything printed after a print statement will appear on the same line See Countdown.java on page 61

ICS111 - Java Programming Blanca Polo 6 Countdown.java //******************************************************************** // Countdown.java Author: Lewis/Loftus // Demonstrates the difference between print and println. //******************************************************************** public class Countdown{ // // Prints two lines of output representing a rocket countdown. // public static void main (String[ ] args){ System.out.print ("Three... "); System.out.print ("Two... "); System.out.print ("One... "); System.out.print ("Zero... "); System.out.println ("Liftoff!"); // appears on first output line System.out.println ("Houston, we have a problem."); }

ICS111 - Java Programming Blanca Polo 7 String Concatenation  The string concatenation operator (+) is used to append one string to the end of another "Peanut butter " + "and jelly"  It can also be used to append a number to a string  A string literal cannot be broken across two lines in a program  See Facts.java (page 63)

ICS111 - Java Programming Blanca Polo 8 //******************************************************************** // Facts.java Author: Lewis/Loftus // // Demonstrates the use of the string concatenation operator and the // automatic conversion of an integer to a string. //******************************************************************** public class Facts{ // // Prints various facts. // public static void main (String[ ] args){ // Strings can be concatenated into one long string System.out.println ("We present the following facts for your " + "extracurricular edification:"); System.out.println ( ); // A string can contain numeric digits System.out.println ("Letters in the Hawaiian alphabet: 12"); // A numeric value can be concatenated to a string System.out.println ("Dialing code for Antarctica: " + 672); System.out.println ("Year in which Leonardo da Vinci invented “ + "the parachute: " ); System.out.println ("Speed of ketchup: " " km per year"); } Facts.java

ICS111 - Java Programming Blanca Polo 9 Concatenating or Adding?  The + operator is also used for arithmetic addition  The function that it performs depends on the type of the information on which it operates  If both operands are strings, or if one is a string and one is a number, it performs string concatenation  If both operands are numeric, it adds them  The + operator is evaluated left to right, but parentheses can be used to force the order  See Addition.java (page 64)

ICS111 - Java Programming Blanca Polo 10 Addition.java //******************************************************************** // Addition.java Author: Lewis/Loftus // // Demonstrates the difference between the addition and string // concatenation operators. //******************************************************************** public class Addition { // // Concatenates and adds two numbers and prints the results. // public static void main (String[ ] args) { System.out.println ("24 and 45 concatenated: " ); System.out.println ("24 and 45 added: " + ( )); }

ICS111 - Java Programming Blanca Polo 11 Printing Quotes  What if we wanted to print a the quote character? System.out.println ("I said "Hello" to you.");  The following line would confuse the compiler because it would interpret the second quote as the end of the string

ICS111 - Java Programming Blanca Polo 12 The Escape Character  An escape sequence is a series of characters that represents a special character  An escape sequence begins with a backslash character ( \ ) System.out.println ("I said \"Hello\" to you.");

ICS111 - Java Programming Blanca Polo 13 Escape Sequences  Some Java escape sequences: Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash

ICS111 - Java Programming Blanca Polo 14 Roses.java //******************************************************************** // Roses.java Author: Lewis/Loftus // // Demonstrates the use of escape sequences. //******************************************************************** public class Roses { // // Prints a poem (of sorts) on multiple lines. // public static void main (String[ ] args) { System.out.println ("Roses are red,\n\tViolets are blue,\n" + "Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t" + "So I'd rather just be friends\n\tAt this point in our " + "relationship."); }

ICS111 - Java Programming Blanca Polo 15