STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.

Slides:



Advertisements
Similar presentations
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Advertisements

Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.
23-Apr-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
Objects vs. Primitives Primitive Primitive byte, short, int, long byte, short, int, long float, double float, double char char boolean boolean Object (instance.
Primitive Types CSE 115 Spring 2006 April 3 &
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Primitive Variables.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32),
In Java.... Variables contain the values of primitive data types Value Types.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
A: A: double “4” A: “34” 4.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Object-Oriented Programming Fundamentals. Comp 241, Fall Example Colored points on the screen What data goes into making one? Coordinates Color.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
100e Hosted by vhs © Don Link, Indian Creek School, 2004 Jeopardy.
1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Object Oriented Programming Lecture 2: BallWorld.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Interface Definition Language
Computer Science 3 Hobart College
Review Operation Bingo
Explicit and Implicit Type Changes
Principles of Computer Programming (using Java) Chapter 2, Part 1
Unit-2 Objects and Classes
null, true, and false are also reserved.
الكلية الجامعية للعلوم التطبيقية
نوع داده هاي انتزاعي Abstract Data Types
Computers & Programming Languages
An Introduction to Java – Part I, language basics
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Value Types and Reference Types
Object-Oriented Programming
Chapter 2: Java Fundamentals
Programs and Classes A program is made up from classes
Java Programming Review 1
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Names of variables, functions, classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Subtype Substitution Principle
2018 Arithmetic.
Presentation transcript:

STRING AN EXAMPLE OF REFERENCE DATA TYPE

2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double  char  boolean  We can use the following operators with primitive data types:  arithmetic operators: *, /, %, +, -  relational operators: >, >=,, >=, <, <=, ==, !=  logical operators: !, &&, ||

3 Primitive vs Reference Data Types  A Primitive type is one that holds a simple, indecomposable value, such as:  a single number  a single character  A Reference type is a type for a class:  it can hold objects that have data and methods  Reference data types are stored in memory differently from primitive data types.

4 Memory Allocation  Declaration of a primitive data type prepares memory to store a value of that type. int num1; num1 0x0010 memory address memory allocated to store an integer (4 bytes) variable name int num1 = 12; When a value is assigned to the variable num1, the value is stored at that memory location. 12

5 Memory Allocation  Declaration of a reference data type prepares memory to store a memory address. String s1; s1 0x0010 memory address memory allocated to store an memory address variable name The new operator allocates memory to store the actual object. s1 = new String("Hello"); 0x4h12 "Hello" The memory address of the object will be stored in the space allocated for s1. 0x4h12 memory address

6 Comparing Strings  A variable that is declared for a reference data type only refers to a memory location for an object. String s1 = new String("hi"); s1s2 0x4h120x3121 "hi" 0x3121 0x4h12 returns false because it is comparing the memory addresses 0x4h12 and 0x3121 String s3 = s2; s3 0x3121 returns true because it is comparing the memory addresses 0x3121 and 0x3121 (both s2 and s3 refer to the same object) String s2 = new String("hi"); System.out.println(s1 == s2); System.out.println(s2 == s3) System.out.println(s1.equals(s2) returns true because the String method equals is used to compare Strings.

7 The String Data Type  A String type is an example of a reference data type.  A string is defined as a sequence of characters.  Examples of String literals:  " " (space, not the character ' ')  "" (empty String)  "a"  "HELLO"  "This is a String"  "\tThis is also a String\n"

8 Declaring a String  Strings can be used to store names, titles, etc.  We can declare a String data type by giving it a variable name:  String name;  We can also initialize the variable upon declaration:  String subjectCode = “ JSI1026";  Because String is a class type, the correct way to declare it is to use the new operator:  String subjectCode = new String( “ JSI1026");

9 String Objects  When you declare a String using: String subjectCode = new String(“JSI1026"); The String subjectCode refers to a String object.  String objects have  data : they hold a sequence of characters  methods : the data can be manipulated in a certain way.  We will look at some of the more common methods associated with Strings.

10 String Methods  Assume that we have declared the String object: String subjectCode = new String(“JSI1026");  public int length()  returns the length of the String:  subjectCode.length() returns the value 6  public boolean equals(AnotherString)  checks if the calling string is equal to AnotherString  subjectCode.equals(“JSI1029") returns false  public boolean equalsIgnoreCase(AnotherString)  checks if it is equal to AnotherString, considering lowercase and uppercase letters to be equal.  subjectCode.equalsIgnoreCase(“jsi1026") returns true

11 String Methods  public String toLowerCase()  returns a String that is the calling String converted to lowercase.  subjectCode.toLowerCase() returns the String “jsi1026"  Similarly for the method toUpperCase()  public String trim() removes leading and trailing whitespace: String whiteString = new String(" Lots of WhiteSpace "); whiteString.trim() returns the String "Lots of WhiteSpace"

12  public char charAt(int Position)  returns the character at Position.  whiteString.charAt(6) returns 's'  public String substring(int Start)  returns a String beginning from Start  whiteString.substring(11) returns "WhiteSpace "  public String substring(int Start, int End)  returns a String beginning at Start up until just before End  whiteString(3,7) returns "Lots"  public int indexOf(String A_String)  returns the first position of A_String if found, -1 if not found.  indexOf("it") returns 13 LotsofWhiteSpace positions of characters whiteString

13 (More) String Methods  public int compareTo(A_String)  compares the string to A_String lexicographically and returns a – ve number if the calling string comes first, 0 if they are equal and a +ve number if A_String comes first.  public boolean endsWith(A_String)  returns true if the string ends with A_String, false otherwise.  public boolean startsWith(A_String)  returns true if the string starts with A_String.  see example: StringExamples.java

14 Example – the String class  Let's consider some of the String methods we have considered previously:  public boolean equals(String anotherString)  public boolean equalsIgnoreCase(String otherString) String s1 = new String("Hello");// String objects s1 String s2 = new String("hello");// and s2 created System.out.println("Two strings equal " + s1.equals(s2)); System.out.print("Ignoring case: "+ s1.equalsIgnoreCase(s2)); the method is invoked by the String object s1.

15 Exercise  Consider the methods:  public int length()  Returns the length of this string.  public char charAt(int pos)  Returns the character at the specified index.  Write a program that displays the String "Happy New Year", one character per line. HappyNewYearHappyNewYear see NewYearGreeting.java

16 Exercise  Write a program that asks for the user's name and gender. If the gender is ‘l' or ‘L' then display "Halo Tuan XXX" or "Halo Nona XXX“  Sample run 1: Please enter name: Lina Please enter gender: P Halo Nona Lina  Sample run 2: Please enter name: Joko Please enter gender: L Halo Tuan Joko