Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined.

Slides:



Advertisements
Similar presentations
Chapter 5 Implementing a simple class. This chapter discusses n Implementing class definitions. n How to store data in an object and how to write method.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
VBA Modules, Functions, Variables, and Constants
CS102--Object Oriented Programming
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
CS102--Object Oriented Programming Lecture 3: – Defining classes (10 questions) – The StringTokenizer class – The Math class Copyright © 2008 Xiaoyan Li.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
CS102--Object Oriented Programming Lecture 2: – the String class – Console Input/Output – Flow of control Copyright © 2008 Xiaoyan Li.
String Escape Sequences
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Java Programming Language
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Chapter 2: Java Fundamentals
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Copyright Curt Hill Variables What are they? Why do we need them?
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Sahar Mosleh California State University San MarcosPage 1 Character String.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
CMSC 202 Java Primer. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java-02 Basic Concepts Review concepts and examine how java handles them.
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
Static Methods Slides 2 to 15 only Static Methods A static method is one that can be used without a calling object A static method still belongs.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Introduction to Modular Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
String class.
Java Primer 1: Types, Classes and Operators
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Primitive Types Vs. Reference Types, Strings, Enumerations
Escape Sequences What if we wanted to print the quote character?
Type Conversion, Constants, and the String Object
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Examples of Primitive Values
Chapter 2 Edited by JJ Shepherd
Java Classes and Objects 3rd Lecture
CMSC 202 Java Primer 2.
Implementing a simple class
Primitive Types and Expressions
Classes, Objects and Methods
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined class in Java that is used to store and process strings Objects of type String are made up of strings of characters that are written within double quotes Any quoted string is a constant of type String "Live long and prosper." A variable of type String can be given the value of a String object String blessing = "Live long and prosper.";

Sahar Mosleh California State University San MarcosPage 2 Concatenation: Using the + operator on two strings in order to connect them to form one longer string If greeting is equal to "Hello ", and javaClass is equal to "class", then greeting + javaClass is equal to "Hello class" Any number of strings can be concatenated together When a string is combined with almost any other type of item, the result is a string "The answer is " + 42 evaluates to "The answer is 42" Concatenation of Strings

Sahar Mosleh California State University San MarcosPage 3 String Methods The String class contains many useful methods for string-processing applications A String method is called by writing a String object, a dot, the name of the method, and a pair of parentheses to enclose any arguments If a String method returns a value, then it can be placed anywhere that a value of its type can be used String greeting = "Hello"; int count = greeting.length(); System.out.println("Length is " + greeting.length()); Always count from zero when referring to the position or index of a character in a string

Sahar Mosleh California State University San MarcosPage 4 Some Methods in the Class String

Sahar Mosleh California State University San MarcosPage 5

Sahar Mosleh California State University San MarcosPage 6

Sahar Mosleh California State University San MarcosPage 7

Sahar Mosleh California State University San MarcosPage 8

Sahar Mosleh California State University San MarcosPage 9

Sahar Mosleh California State University San MarcosPage 10 String Indexes

Sahar Mosleh California State University San MarcosPage 11 Escape Sequences A backslash (\) immediately preceding a character (i.e., without any space) denotes an escape sequence or an escape character The character following the backslash does not have its usual meaning Although it is formed using two symbols, it is regarded as a single character

Sahar Mosleh California State University San MarcosPage 12

Sahar Mosleh California State University San MarcosPage 13 Static Methods A static method is one that can be used without a calling object A static method still belongs to a class, and its definition is given inside the class definition When a static method is defined, the keyword static is placed in the method header public static returnedType myMethod(parameters) {... } Static methods are invoked using the class name in place of a calling object returnedValue = MyClass.myMethod(arguments);

Sahar Mosleh California State University San MarcosPage 14 Example: Public static int maximum (intn1,intn2) { if (n1>n2) return n1; else return n2; } If the maximum method were in a classs name SomeClass, then the following is a sample invocation of maximum: Int budget = SomeClass.maximum(yourmoney, mymoney); yourmoney and mymoney are variables of type int that contain some value.

Sahar Mosleh California State University San MarcosPage 15 The Math Class The Math class provides a number of standard mathematical methods It is found in the java.lang package, so it does not require an import statement All of its methods and data are static, therefore they are invoked with the class name Math instead of a calling object The Math class has two predefined constants, E (e, the base of the natural logarithm system) and PI ( , ) area = Math.PI * radius * radius;

Sahar Mosleh California State University San MarcosPage 16 Some Methods in the Class Math

Sahar Mosleh California State University San MarcosPage 17

Sahar Mosleh California State University San MarcosPage 18

Sahar Mosleh California State University San MarcosPage 19

Sahar Mosleh California State University San MarcosPage 20

Sahar Mosleh California State University San MarcosPage 21 Variables and Memory A computer has two forms of memory Secondary memory is used to hold files for "permanent" storage Main memory is used by a computer when it is running a program Values stored in a program's variables are kept in main memory

Sahar Mosleh California State University San MarcosPage 22 Main memory consists of a long list of numbered locations called bytes Each byte contains eight bits: eight 0 or 1 digits The number that identifies a byte is called its address A data item can be stored in one (or more) of these bytes The address of the byte is used to find the data item when needed

Sahar Mosleh California State University San MarcosPage 23 Values of most data types require more than one byte of storage Several adjacent bytes are then used to hold the data item The entire chunk of memory that holds the data is called its memory location The address of the first byte of this memory location is used as the address for the data item A computer's main memory can be thought of as a long list of memory locations of varying sizes

Sahar Mosleh California State University San MarcosPage 24 Variables in Memory

Sahar Mosleh California State University San MarcosPage 25 Every variable is implemented as a location in computer memory When the variable is a primitive type, the value of the variable is stored in the memory location assigned to the variable Each primitive type always require the same amount of memory to store its values References

Sahar Mosleh California State University San MarcosPage 26 When the variable is a class type, only the memory address (or reference) where its object is located is stored in the memory location assigned to the variable The object named by the variable is stored in some other location in memory Like primitives, the value of a class variable is a fixed size Unlike primitives, the value of a class variable is a memory address or reference The object, whose address is stored in the variable, can be of any size

Sahar Mosleh California State University San MarcosPage 27 Two reference variables can contain the same reference, and therefore name the same object The assignment operator sets the reference (memory address) of one class type variable equal to that of another Any change to the object named by one of theses variables will produce a change to the object named by the other variable, since they are the same object variable2 = variable1;

Sahar Mosleh California State University San MarcosPage 28 Public class ToyClass { private String name; private int number; public ToyClass (String initiaName, int initiaNumber) { name = initiaName; number = initiaNumber; } public ToyClass () { name = “No name yet” number = 0; } public void set (String newName, int newNumber) { name = newName; number = newNumber; } : }

Sahar Mosleh California State University San MarcosPage 29 Class Type Variables Store a Reference

Sahar Mosleh California State University San MarcosPage 30

Sahar Mosleh California State University San MarcosPage 31 Assignment Operator with Class Type Variables

Sahar Mosleh California State University San MarcosPage 32

Sahar Mosleh California State University San MarcosPage 33