Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.

Slides:



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

Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Chapter 3 Fundamental Data Types Goals: To understand integer and floating-point numbers To recognize the limitations of the int and double types and the.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
Strings In Java, strings are contained in an object that is an instance of the String class. String in java is the name of a class. That’s why it starts.
Fundamental Programming Structures in Java: Strings.
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.
Strings Reading for this Lecture, L&L, 3.2. Strings String is basically just a collection of characters. Thus, the string “Martyn” could be thought of.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
28-Jun-15 String and StringBuilder Part I: String.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Recitation 2 Main Method, API & Packages, Java Basics.
Chapter 3 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the int and double types.
Unit 3: Java Data Types Math class and String class.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
String Manipulation. Java String class  The String class represents character strings “Tammy Bailey”  All strings (arrays of characters) in Java programs.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
String and Scanner CS 21a: Introduction to Computing I First Semester,
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
Unit 2 Test: Tues 11/3. ASCII / Unicode –Each letter, symbol, etc has a # value –See ascii table (in folder) –To convert a char into its ascii value,
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
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.
17-Feb-16 String and StringBuilder Part I: String.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
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.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
The Methods and What You Need to Know for the AP Exam
Strings A String is a sequence of letters
Java: Base Types All information has a type or class designation
Chapter 2 Basic Computation
Java: Base Types All information has a type or class designation
Strings, StringBuilder, and Character
String class.
String and String Buffers
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Primitive Types Vs. Reference Types, Strings, Enumerations
String and StringBuilder
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
String and StringBuilder
Lecture 10 Strings CSE /26/2018.
String and StringBuilder
String.
CS2011 Introduction to Programming I Strings
Java Programming Language
Dr. Sampath Jayarathna Cal Poly Pomona
Outline Creating Objects The String Class The Random and Math Classes
Object Oriented Programming
What is a String? String s = "compsci"; s c o m p s i
Strings in Java Strings in Java are also a reference type.
Unit-2 Objects and Classes
What We Want To Do User enters: Mary Smith
Presentation transcript:

Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores any value assigned to it. A variable which is declared as a class type can store the reference to an object of that type.

chars The char type can store one character value 'A', '\n', '\u00E9‘ char represented by a 16 bit unsigned integer * A total of 2 16 different Unicode chars can be represented int value; char letter; value = 5; letter = ‘5’; value = letter; //stores a 53 letter = value; //error ……. Information can be lost // when converting 32 bits to 16

A String is NOT a primitive type.. String is a class provided by the Java API (A object of this type stores a ordered group of chars) Strings ARE OBJECTS!! Look at Java API specs………….

The String class provides constructors String word; word = new String(“hello”); //constructing a //String object from a String literal Seems redundant …. String is the ONLY class that lets you create a String object by assigning a value…. word = “hello”;

String constructors are useful, however String word, word2; word = JOptionPane.showInputDialog(“Enter info:”); The showInputDIalog method calls one of the String constructors to create a String object. This String object is then returned to the caller. word2 = new String(word); You would call a constructor to make a copy of an existing String.

int length() length() method returns the number of chars in a String object System.out.println(“size” + “hello”.length()) ; The length of a String is not always obvious… String in = JOptionPane.showInputDialog(“Enter”); int size = in.length();

String characters are indexed………… The first character in a String is at index 0. Note: last char is at index: length()-1

char charAt(int) charAt method returns character from a string the String object is unchanged by this call String in = JOptionPane.showInputDialog(“Enter”); JOptionPane.showMessageDialog(null,”first char is” + in.charAt(0) ); JOptionPane.showMessageDialog(null,”last char is” + in.charAt(in.length()-1 ) );

String substring(int,int) This method returns a new String object, which contains a portion of the characters of the invoking object Parameters supply start and “past the end” position String in, apiece; in = JOptionPane.showInputDialog(“Enter”); //user types hello world apiece = in.substring(1,8); // String in is unchanged // String apiece contains characters “ello wo”

A String is an IMMUTABLE object………… Note that we changed the String object that in was referring to by creating a new object and assigning the new object to in. It is not possible to change the data (chars) stored in a String object. If you wish to do this, you must create a new String object. This is because the String class has no mutator methods (methods which change its instance data). A class without mutator methods is called IMMUTABLE.

int indexOf(char) indexOf method returns the position of the first occurrence of parameter (-1 if parameter does not occur) String in = JOptionPane.showInputDialog(“Enter”); int blk = in.indexOf(“ “); //where is first blank? If user had entered hello world, blk now contains 5 in = in.substring(blk+1, in.length() ); //in is now referencing an object without the first word

String substring(int) Many String methods are OVERLOADED … This call has an implied 2 nd parameter – which is the length of the String String in = JOptionPane.showInputDialog(“Enter”); int blk = in.indexOf(“ “); //where is first blank? If user had entered hello world, blk now contains 5 in = in.substring(blk+1 ); //in is now referencing an object without the first word

+ is the String concatenation operator + is an overloaded operator in Java String fname = "Harry"; String lname = "Hacker"; String name = fname + lname; name is "HarryHacker" If one operand of + is a string, the other is converted to a string: String a = "Agent"; String name = a + 7; name is "Agent7" You commonly use this for output: System.out.println(“The value is “ + 7 );

Converting between Strings and Numbers Convert to number: int n = Integer.parseInt(str); double x = Double.parseDouble(x); Convert to string: String str = Integer.toString(n); More efficient, less readable str = "" + n; System.out.println(“” + n ); //explicit conversion needed here

Practice Convert String word to pig latin (assume word stores 1 word) Ask user for a a sentence If the word COW is contained in the sentence, replace the first occurrence of it with MOOSE Replace ALL occurrances of COW with MOOSE