SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Size: px
Start display at page:

Download "SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)"

Transcription

1 SOFTWARE DEVELOPMENT 1 Strings and Enumerations 2018W (Institute of Pervasive Computing, JKU Linz)

2 CHARACTER ENCODING On digital systems, each character is represented by a specific number. The character set (short charset) determines, which number represents a character. There are numerous different character sets in use: US-ASCII: 128 characters (7 bit) one of the oldest standards; the first 128 characters of most character sets are identical to ASCII Windows ANSI: 256 characters (8 bit) commonly encountered on Windows systems ISO : 256 characters (8 bit) common for western websites UTF-8, UTF-16,... (Unicode): variable character width (e.g. UTF-8 has 8-32 bit). Supports pretty much any language on the planet. Recommended by most international institutions. Java works internally always with characters (char) in UTF-16 format. During input / output, characters need to be converted. Software Development 1 // 2018W // 2

3 CHARACTER SEQUENCES :: STRINGS In Java, character sequences are contained in the class String. String Overview (see API documentation for more details): class String { // Constructors public String() { /* Create a new empty String */ public String(char value[]) { /* String from character array */ public String(byte[] bytes, String charsetname) { /* String from byte array, with specified character set */ // Methods public int length() { /* number of characters */ public char charat(int index) { /* single char at position */ public int indexof(int ch) { /* first position of a char */ public String substring(int beginindex) { /* part of the String */ public String concat(string str) { /* join 2 Strings */ public int compareto(string anotherstring) { /* compare Strings */ public static String valueof(double d) { /* convert a double to String */ Software Development 1 // 2018W // 3

4 CONSTRUCTORS OF CLASS STRING public String(char value[]) Convert array of characters to a String. public String(byte[] bytes, String charsetname) Convert array of bytes to a String. The specified character encoding is used for conversion. public String(byte[] bytes) Convert with system default character set. public String(StringBuffer buffer) Convert a StringBuffer to String. Beispiele: char[] c = {'F', 'r', 'o', 'g', ' ', 's'; String sc = new String(c); byte[] b = {0x20, 0x6c, 0x65, 0x67, 0x73; String sb = new String(b, "US-ASCII"); System.out.println(sc + sb); // Output: Frog s legs Software Development 1 // 2018W // 4

5 CREATING STRINGS String from a literal: String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Convert from arrays of characters, bytes... String s1 = new String( new char[] {'A', 'B', 'C', 'D', 'E' ); String s2 = new String(s1); // copy of s1 Convert primitive data types: // int, double, long, float, boolean, short... String s3 = String.valueOf( x == 2.4 ); String pi = String.valueOf( ); Concatenate 2 Strings with the + operator: String firstname = new String("Walter"); String lastname = "Wazinger"; // short form (literal) String name = firstname + lastname; // new String object System.out.println("Age: " + age + " years"); Software Development 1 // 2018W // 5

6 String rector = "Hagelauer"; rector.length(); // 9 "Hagelauer".length(); // 9 length() is a method, not a field as it is with arrays! rector.charat(2); // 'g' rector.indexof("a"); // 1 first index of "a" rector.indexof("a", 6); // -1 next index of "a", starting at index 6 rector.indexof("x"); // -1 "x" is not found rector.indexof('g'); // 2 'g' is character, not String String s4 = rector.substring(0, 2); // "Ha" substring [0..2[ String s5 = rector.substring(3); // "elauer" substring [3...] String s = s4 + s5; // "Haelauer" boolean error = s.startswith("hagel"); Software Development 1 // 2018W // 6

7 EXAMPLE :: CHARACTER INDEX class Alphabet { protected static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; protected static int numberinalphabet(char c) { return alphabet.indexof(c) + 1; Get index of character in alphabet (1-26) public static void main(string[] args) { for (int i = 0; i < args.length; i++) { char c; c = args[i].charat(0); System.out.println("Character " + c + " is number " + numberinalphabet(c)); SWE1.10 / Alphabet.java Software Development 1 // 2018W // 7

8 CONVERTING TO/ FROM STRINGS Converting to primitive data types int i = Integer.parseInt("666"); int j = new Integer("666").intValue(); double pi = Double.valueOf("3.1415").doubleValue(); Converting from primitive data types int three = 3; String s = Integer.toString(three); String number = String.valueOf(666); Converting to character arrays char[] alphabet; alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); Converting from characters or character arrays String t = String.valueOf('i'); String lettersequence = new String(alphabet); Software Development 1 // 2018W // 8

9 EXAMPLE :: CONVERTING STRINGS TO INT //Display sum of all application arguments public class Convert { public static void main(string[] args) { //convert String arguments to int int[] a = new int[args.length]; for (int i = 0; i < args.length; i++) { // convert with Integer object Integer number = new Integer(args[i]); a[i] = number.intvalue(); // convert directly to int a[i] = Integer.parseInt(args[i]); // build sum and display int sum = 0; for (int i = 0; i < a.length; i++) sum += a[i]; System.out.println("Sum: " + sum); SWE1.10 / Convert.java Software Development 1 // 2018W // 9

10 COMMON METHODS public String tolowercase() public String touppercase() Transforms letters to lower / upper case. Also works with local letters (e.g. ö, ü, ß) touppercase converts "ß" to "SS"! It can not be converted back with tolowercase. Example: String a = "Schoßhündchen"; System.out.println(a.toLowerCase()); // Output: schoßhündchen System.out.println(a.toUpperCase()); // Output: SCHOSSHÜNDCHEN System.out.println(a.toUpperCase().toLowerCase()); // Output: schosshündchen Software Development 1 // 2018W // 10

11 COMMON METHODS public boolean equals(object anobject) Overwrites equals method from class Object. Checks if String content is identical. Example: String name1 = "Max"; String name2 = "Max"; String name3 = new String("Max"); String name4 = "max"; System.out.println(name1 == name2); System.out.println(name1 == name3); name1 and name2 do not only have the same contents, they are referencing the same object! Compare the references, not the content! // true // false! System.out.println(name1.equals(name3)); // true System.out.println(name1.equalsIgnoreCase(name4)); // true Compare the String content. Software Development 1 // 2018W // 11

12 COMMON METHODS public int compareto(string anotherstring) Performs a lexical comparison of 2 Strings. Returns 0, in case the Strings are identical, a value lower than 0, in case the String lexicographically precedes the argument, and a value greater than 0, in case the String lexicographically follows the argument. Example: String name = "Max"; System.out.println(name.compareTo("Franz")); // result > 0 System.out.println(name.compareTo("Max")); // result = 0 System.out.println(name.compareTo("Peter")); // result < 0 Software Development 1 // 2018W // 12

13 CLASS STRINGBUFFER AND STRINGBUILDER String objects can not be changed once they are created. To edit character sequences, create an instance of either StringBuffer or StringBuilder. Both have identical interface and functionality, but StringBuilder is not thread safe. If only a single thread is accessing the character sequence, StringBuilder should be used since it is slightly faster. Constructors: public StringBuffer() Create a new (empty) character sequence. public StringBuffer(int length) Create a new (empty) character sequence with an initial capacity. The capacity it automatically expanded when necessary. public StringBuffer(String str) Create a new character sequence and initialize it with a String. Software Development 1 // 2018W // 13

14 STRINGBUFFER :: METHODS StringBuffer a = new StringBuffer("Bazinga!"); int i = a.length(); // Number of characters in a a.append("!!!"); a.append(3); a.append(3.14); // a = "Bazinga!!!!" // append is overloaded for // all primitive data types a.delete(8, a.length()); // a = "Bazinga!" // Delete from index 11 to the end a.insert(0, "3 x "); // a = "3 x Bazinga!" // Insert at index 0 Software Development 1 // 2018W // 14

15 STRINGBUFFER :: METHODS a.replace(0,1,"100"); // a = "100 x Bazinga!" // Replace index [0..1[ a.deletecharat(2); // a = "10 x Bazinga!" // Delete char at index 2 a.setlength(4); // a = "10 x" // Set to new length; cut off rest a.reverse(); // a = "x 01" // Reverse sequence String s1 = a.tostring(); // Convert buffer to String String s2 = a.substring(0,1); // Only take part of the buffer Software Development 1 // 2018W // 15

16 STRINGBUFFER :: EXAMPLE // Read int numbers from standard input public class ReadInt { // read a line from standard input and convert to number private static int readint() throws IOException { // read line StringBuilder buffer = new StringBuilder(); // new StringBuilder int c = System.in.read(); // read first char while (c >= 0 && c!= '\n') { // while not end of line buffer.append((char)c); // add char to buffer c = System.in.read(); // parse number in line and return int return Integer.parseInt(buffer.toString()); // test our method public static void main(string[] args) throws IOException { int i = readint(); System.out.println("Your number: " + i); // read next char It is much more efficient to use a StringBuilder than concatenating Strings ( s = s + (char)c; ) Exceptions are not handled SWE1.10 / ReadInt.java Software Development 1 // 2018W // 16

17 ENUMERATIONS One way to define enumerations is a list of constants: public static final int DAY_MONDAY = 0; public static final int DAY_TUESDAY = 1; public static final int DAY_WEDNESDAY = 2; public static final int DAY_THURSDAY = 3; public static final int DAY_FRIDAY = 4; public static final int DAY_SATURDAY = 5; public static final int DAY_SUNDAY = 6; int dayofevent = DAY_WEDNESDAY; Define all weekdays as constant integers. Assign a weekday to a variable. This approach has several disadvantages: That a constant is part of the enumeration has to be indicated with a name prefix. No type safety: Any int value may be assigned to variables that should hold a weekday. The compiler can only check the type int. Outputting a weekday only shows a numeric value. Complicated and long-winded declaration. Software Development 1 // 2018W // 17

18 ENUMERATIONS Declaration: enum Name { CONSTANT_1, CONSTANT_2,... All enumerations are subclasses of java.lang.enum Coding conventions: Since enumerations are classes, their name should be formatted like a class name: first letter of each word in uppercase. The values are constants (static final) and therefore should be in all uppercase, with underscores to separate words. Example: enum DaysOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY Declaration of enumeration references: DaysOfWeek dayofevent = DaysOfWeek.WEDNESDAY; Software Development 1 // 2018W // 18

19 ENUMERATIONS Internally Java translates enumerations in classes, that look like this: enum DaysOfWeek { MONDAY, TUESDAY,... class DaysOfWeek extends Enum { public static final DaysOfWeek MONDAY = new DaysOfWeek(); public static final DaysOfWeek TUESDAY = new DaysOfWeek();... The enumeration values are static final references to a new instance of the class DaysOfWeek This should also explain the following code pieces: DaysOfWeek dayofevent = DaysOfWeek.WEDNESDAY; import static DaysOfWeek.*;... DaysOfWeek dayofevent = WEDNESDAY; The object WEDNESDAY was instantiated once at program start. No further new required! import static allows us to reference enumeration values directly. Software Development 1 // 2018W // 19

20 ENUMERATIONS Since enumerations are actually classes, they can also contain fields, methods and constructors: enum DaysOfWeek { MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6), SUNDAY(7); This would translate to something like this: class DaysOfWeek extends Enum { public static final DaysOfWeek MONDAY = new DaysOfWeek(1); public static final DaysOfWeek TUESDAY = new DaysOfWeek(2);... private int number; DaysOfWeek(int number) { this.number = number; Private object field. Constructor public String tostring() { return super.tostring() + "(" + number + ")"; Overwrite the default tostring method. Software Development 1 // 2018W // 20

21 ENUMERATIONS :: EXAMPLE public enum IntSize { BYTE, SHORT, INT, LONG; public static IntSize getsize(long i) { // find min. size for number if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) return BYTE; if (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) return SHORT; if (i >= Integer.MIN_VALUE && i <= Integer.MAX_VALUE) return INT; return LONG; // try with some numbers public static void main(string[] args) { long[] nums = {34567, 280, -10, L; for (long n : nums) { IntSize minsize = IntSize.getSize(n); System.out.print(n + ": "); switch (minsize) { case BYTE: System.out.println("min size is 8 Bit"); break; case SHORT: System.out.println("min size is 16 Bit"); break; case INT: System.out.println("min size is 32 Bit"); break; case LONG: System.out.println("min size is 64 Bit"); Method to find the smallest primitive numeric data type that can store the number. Enumerations can also be used in switch-statements Software Development 1 // 2018W // 21

22 SOFTWARE DEVELOPMENT 1 Strings and Enumerations 2018W (Institute of Pervasive Computing, JKU Linz)

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University Enumerated Types CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Enumerated Types An enumerated type defines a list of enumerated values Each value is an identifier

More information

Lab 14 & 15: String Handling

Lab 14 & 15: String Handling Lab 14 & 15: String Handling Prof. Navrati Saxena TA: Rochak Sachan String Handling 9/11/2012 22 String Handling Java implements strings as objects of type String. Once a String object has been created,

More information

Object-Oriented Programming

Object-Oriented Programming Data structures Object-Oriented Programming Outline Primitive data types String Math class Array Container classes Readings: HFJ: Ch. 13, 6. GT: Ch. 13, 6. Đại học Công nghệ - ĐHQG HN Data structures 2

More information

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing Java Programming MSc Induction Tutorials 2011 Stefan Stafrace PhD Student Department of Computing s.stafrace@surrey.ac.uk 1 Tutorial Objectives This is an example based tutorial for students who want to

More information

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters Overloaded Methods Sending Messages Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Initialization & Cleanup 2 Overloaded Constructors Sending Parameters accessor method 3 4 Sending Parameters

More information

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All Advanced Java This chapter discusses class String, class StringBuilder and class Character from the java.lang package. These classes provide

More information

Class Library java.lang Package. Bok, Jong Soon

Class Library java.lang Package. Bok, Jong Soon Class Library java.lang Package Bok, Jong Soon javaexpert@nate.com www.javaexpert.co.kr Object class Is the root of the class hierarchy. Every class has Object as a superclass. If no inheritance is specified

More information

Chapter 8: A Second Look at Classes and Objects

Chapter 8: A Second Look at Classes and Objects Chapter 8: A Second Look at Classes and Objects Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 8 discusses the following main topics:

More information

09 Objects III: String and StringBuffer, Singly (Doubly) Linked Lists

09 Objects III: String and StringBuffer, Singly (Doubly) Linked Lists Exercises Software Development I 09 Objects III: String and StringBuffer, Singly (Doubly) Linked Lists December 10th, 2014 Software Development I Winter term 2014/2015 Priv.-Doz. Dipl.-Ing. Dr. Andreas

More information

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended JAVA Classes Class definition complete definition [public] [abstract] [final] class Name [extends Parent] [impelements ListOfInterfaces] {... // class body public public class abstract no instance can

More information

Java Strings Java, winter semester

Java Strings Java, winter semester Java Strings 1 String instances of java.lang.string compiler works with them almost with primitive types String constants = instances of the String class immutable!!! for changes clases StringBuffer, StringBuilder

More information

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All This chapter discusses class String, from the java.lang package. These classes provide the foundation for string and character manipulation

More information

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

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method Appendix 3 Java - String charat() Method This method returns the character located at the String's specified index. The string indexes start from zero. public char charat(int index) index -- Index of the

More information

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

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics Strings Strings and their methods Produced by: Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list Primitive Types: char Object Types: String Primitive vs Object Types

More information

About Codefrux While the current trends around the world are based on the internet, mobile and its applications, we try to make the most out of it. As for us, we are a well established IT professionals

More information

Objectives. Describe ways to create constants const readonly enum

Objectives. Describe ways to create constants const readonly enum Constants Objectives Describe ways to create constants const readonly enum 2 Motivation Idea of constant is useful makes programs more readable allows more compile time error checking 3 Const Keyword const

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Creating Strings. String Length

Creating Strings. String Length Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

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

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs. Java SE11 Development Java is the most widely-used development language in the world today. It allows programmers to create objects that can interact with other objects to solve a problem. Explore Java

More information

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix PGJC4_JSE8_OCA.book Page ix Monday, June 20, 2016 2:31 PM Contents Figures Tables Examples Foreword Preface xix xxi xxiii xxvii xxix 1 Basics of Java Programming 1 1.1 Introduction 2 1.2 Classes 2 Declaring

More information

More non-primitive types Lesson 06

More non-primitive types Lesson 06 CSC110 2.0 Object Oriented Programming Ms. Gnanakanthi Makalanda Dept. of Computer Science University of Sri Jayewardenepura More non-primitive types Lesson 06 1 2 Outline 1. Two-dimensional arrays 2.

More information

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

2. All the strings gets collected in a special memory are for Strings called  String constant pool. Basics about Strings in Java 1. You can create Strings in various ways:- a) By Creating a String Object String s=new String("abcdef"); b) By just creating object and then referring to string String a=new

More information

Building Strings and Exploring String Class:

Building Strings and Exploring String Class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Lecture Notes K.Yellaswamy Assistant Professor CMR College of Engineering & Technology Building Strings and Exploring

More information

Lecture Notes K.Yellaswamy Assistant Professor K L University

Lecture Notes K.Yellaswamy Assistant Professor K L University Lecture Notes K.Yellaswamy Assistant Professor K L University Building Strings and Exploring String Class: -------------------------------------------- The String class ------------------- String: A String

More information

Topic 5: Enumerated Types and Switch Statements

Topic 5: Enumerated Types and Switch Statements Topic 5: Enumerated Types and Switch Statements Reading: JBD Sections 6.1, 6.2, 3.9 1 What's wrong with this code? if (pressure > 85.0) excesspressure = pressure - 85.0; else safetymargin = 85.0 - pressure;!

More information

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Language Features. 1. The primitive types int, double, and boolean are part of the AP Language Features 1. The primitive types int, double, and boolean are part of the AP short, long, byte, char, and float are not in the subset. In particular, students need not be aware that strings are

More information

Cloning Enums. Cloning and Enums BIU OOP

Cloning Enums. Cloning and Enums BIU OOP Table of contents 1 Cloning 2 Integer representation Object representation Java Enum Cloning Objective We have an object and we need to make a copy of it. We need to choose if we want a shallow copy or

More information

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Java enum, casts, and others (Select portions of Chapters 4 & 5) Enum or enumerates types Java enum, casts, and others (Select portions of Chapters 4 & 5) Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The

More information

"Hello" " This " + "is String " + "concatenation"

Hello  This  + is String  + concatenation Strings About Strings Strings are objects, but there is a special syntax for writing String literals: "Hello" Strings, unlike most other objects, have a defined operation (as opposed to a method): " This

More information

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

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and

More information

String. Other languages that implement strings as character arrays

String. Other languages that implement strings as character arrays 1. length() 2. tostring() 3. charat() 4. getchars() 5. getbytes() 6. tochararray() 7. equals() 8. equalsignorecase() 9. regionmatches() 10. startswith() 11. endswith() 12. compareto() 13. indexof() 14.

More information

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

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

RTL Reference 1. JVM. 2. Lexical Conventions

RTL Reference 1. JVM. 2. Lexical Conventions RTL Reference 1. JVM Record Transformation Language (RTL) runs on the JVM. Runtime support for operations on data types are all implemented in Java. This constrains the data types to be compatible to Java's

More information

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

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics Strings Strings and their methods Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list Primitive Types: char Object Types: String Primitive

More information

More on variables and methods

More on variables and methods More on variables and methods Robots Learning to Program with Java Byron Weber Becker chapter 7 Announcements (Oct 12) Reading for Monday Ch 7.4-7.5 Program#5 out Character Data String is a java class

More information

Selected Questions from by Nageshwara Rao

Selected Questions from  by Nageshwara Rao Selected Questions from http://way2java.com by Nageshwara Rao Swaminathan J Amrita University swaminathanj@am.amrita.edu November 24, 2016 Swaminathan J (Amrita University) way2java.com (Nageshwara Rao)

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

Index COPYRIGHTED MATERIAL

Index COPYRIGHTED MATERIAL Index COPYRIGHTED MATERIAL Note to the Reader: Throughout this index boldfaced page numbers indicate primary discussions of a topic. Italicized page numbers indicate illustrations. A abstract classes

More information

Letterkenny Institute of Technology

Letterkenny Institute of Technology Letterkenny Institute of Technology Course Code: OOPR K6A02 BACHELOR OF SCIENCE IN COMPUTING WITH BUSINESS APPLICATIONS MULTIMEDIA AND DIGITAL ENTERTAINMENT COMPUTER SECURITY & DIGITAL FORENSICS COMPUTER

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

TEST (MODULE:- 1 and 2)

TEST (MODULE:- 1 and 2) TEST (MODULE:- 1 and 2) What are command line arguments? Write a program in JAVA to print Fibonacci series using command line arguments? [10] Create a class employee with data members empid, empname, designation

More information

Java Identifiers, Data Types & Variables

Java Identifiers, Data Types & Variables Java Identifiers, Data Types & Variables 1. Java Identifiers: Identifiers are name given to a class, variable or a method. public class TestingShastra { //TestingShastra is an identifier for class char

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

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

Java s String Class. in simplest form, just quoted text. used as parameters to. This is a string So is this hi 1 Java s String Class in simplest form, just quoted text "This is a string" "So is this" "hi" used as parameters to Text constructor System.out.println 2 The Empty String smallest possible string made

More information

Lesson:9 Working with Array and String

Lesson:9 Working with Array and String Introduction to Array: Lesson:9 Working with Array and String An Array is a variable representing a collection of homogeneous type of elements. Arrays are useful to represent vector, matrix and other multi-dimensional

More information

Class. Chapter 6: Data Abstraction. Example. Class

Class. Chapter 6: Data Abstraction. Example. Class Chapter 6: Data Abstraction In Java, there are three types of data values primitives arrays objects actually, arrays are a special type of object Class In Java, objects are used to represent data values

More information

Lecture 12. Data Types and Strings

Lecture 12. Data Types and Strings Lecture 12 Data Types and Strings Class v. Object A Class represents the generic description of a type. An Object represents a specific instance of the type. Video Game=>Class, WoW=>Instance Members of

More information

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing. CISC-124 20180115 20180116 20180118 We continued our introductory exploration of Java and object-oriented programming by looking at a program that uses two classes. We created a Java file Dog.java and

More information

String related classes

String related classes Java Strings String related classes Java provides three String related classes java.lang package String class: Storing and processing Strings but Strings created using the String class cannot be modified

More information

Chapter 10: Text Processing and More about Wrapper Classes

Chapter 10: Text Processing and More about Wrapper Classes Chapter 10: Text Processing and More about Wrapper Classes Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley.

More information

Timing for Interfaces and Abstract Classes

Timing for Interfaces and Abstract Classes Timing for Interfaces and Abstract Classes Consider using abstract classes if you want to: share code among several closely related classes declare non-static or non-final fields Consider using interfaces

More information

15CS45 : OBJECT ORIENTED CONCEPTS

15CS45 : OBJECT ORIENTED CONCEPTS 15CS45 : OBJECT ORIENTED CONCEPTS QUESTION BANK: What do you know about Java? What are the supported platforms by Java Programming Language? List any five features of Java? Why is Java Architectural Neutral?

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

CS18000: Problem Solving And Object-Oriented Programming

CS18000: Problem Solving And Object-Oriented Programming CS18000: Problem Solving And Object-Oriented Programming Class (and Program) Structure 31 January 2011 Prof. Chris Clifton Classes and Objects Set of real or virtual objects Represent Template in Java

More information

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness. Methods There s a method in my madness. Sect. 3.3, 8.2 1 Example Class: Car How Cars are Described Make Model Year Color Owner Location Mileage Actions that can be applied to cars Create a new car Transfer

More information

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a Spring 2017 Name: TUID: Page Points Score 1 28 2 18 3 12 4 12 5 15 6 15 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc. i Some API Reminders

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

CS112 Lecture: Characters and Strings

CS112 Lecture: Characters and Strings CS112 Lecture: Characters and Strings Objectives: Last Revised 3/21/06 1. To introduce the data type char and related basic information (escape sequences, Unicode). 2. To introduce the library classes

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double(3.14); 4... Zheng-Liang Lu Java Programming 290 / 321 Wrapper Classes To treat values as objects, Java supplies standard wrapper classes for each primitive type. For example, you can construct a wrapper object from a primitive value or from a string representation

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 2

Darshan Institute of Engineering & Technology for Diploma Studies Unit 2 Primitive Data Types Primitive data types can be classified in four groups: 1) Integers : o This group includes byte, short, int, and long. o All of these are signed, positive and negative values. DataType

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

TEXT-BASED APPLICATIONS

TEXT-BASED APPLICATIONS Objectives 9 TEXT-BASED APPLICATIONS Write a program that uses command-line arguments and system properties Write a program that reads from standard input Write a program that can create, read, and write

More information

Datatypes, Variables, and Operations

Datatypes, Variables, and Operations Datatypes, Variables, and Operations 1 Primitive Type Classification 2 Numerical Data Types Name Range Storage Size byte 2 7 to 2 7 1 (-128 to 127) 8-bit signed short 2 15 to 2 15 1 (-32768 to 32767) 16-bit

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination Thursday, December 11, 2008 Examiners: Mathieu Petitpas [Section 1] 14:00

More information

Java is an objet-oriented programming language providing features that support

Java is an objet-oriented programming language providing features that support Java Essentials CSCI 136: Spring 2018 Handout 2 February 2 Language Basics Java is an objet-oriented programming language providing features that support Data abstraction Code reuse Modular development

More information

Computer Science II Data Structures

Computer Science II Data Structures Computer Science II Data Structures Instructor Sukumar Ghosh 201P Maclean Hall Office hours: 10:30 AM 12:00 PM Mondays and Fridays Course Webpage homepage.cs.uiowa.edu/~ghosh/2116.html Course Syllabus

More information

Java Module Lesson 2A Practice Exercise

Java Module Lesson 2A Practice Exercise Java Module Lesson 2A Practice Exercise Name Completion Complete each sentence or statement. 1. The three main data types used in a typical Java program are:,, and. 2. In general, data types that are simple

More information

Getting started with Java

Getting started with Java Getting started with Java by Vlad Costel Ungureanu for Learn Stuff Programming Languages A programming language is a formal constructed language designed to communicate instructions to a machine, particularly

More information

Outline. Why Java? (1/2) Why Java? (2/2) Java Compiler and Virtual Machine. Classes. COMP9024: Data Structures and Algorithms

Outline. Why Java? (1/2) Why Java? (2/2) Java Compiler and Virtual Machine. Classes. COMP9024: Data Structures and Algorithms COMP9024: Data Structures and Algorithms Week One: Java Programming Language (I) Hui Wu Session 2, 2016 http://www.cse.unsw.edu.au/~cs9024 Outline Classes and objects Methods Primitive data types and operators

More information

CSCI312 Principles of Programming Languages!

CSCI312 Principles of Programming Languages! CSCI312 Principles of Programming Languages! Chapter 5 Types Xu Liu! ! 5.1!Type Errors! 5.2!Static and Dynamic Typing! 5.3!Basic Types! 5.4!NonBasic Types! 5.5!Recursive Data Types! 5.6!Functions as Types!

More information

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name: CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : In mathematics,

More information

Agenda: Discussion Week 7. May 11, 2009

Agenda: Discussion Week 7. May 11, 2009 Agenda: Discussion Week 7 Method signatures Static vs. instance compareto Exceptions Interfaces 2 D arrays Recursion varargs enum Suggestions? May 11, 2009 Method signatures [protection] [static] [return

More information

5/23/2015. Core Java Syllabus. VikRam ShaRma

5/23/2015. Core Java Syllabus. VikRam ShaRma 5/23/2015 Core Java Syllabus VikRam ShaRma Basic Concepts of Core Java 1 Introduction to Java 1.1 Need of java i.e. History 1.2 What is java? 1.3 Java Buzzwords 1.4 JDK JRE JVM JIT - Java Compiler 1.5

More information

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

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008) Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008) In previous chapters, methods were called with parameters that were primitive data types. This chapter discusses how to use object

More information

STUDENT LESSON A10 The String Class

STUDENT LESSON A10 The String Class STUDENT LESSON A10 The String Class Java Curriculum for AP Computer Science, Student Lesson A10 1 STUDENT LESSON A10 The String Class INTRODUCTION: Strings are needed in many programming tasks. Much of

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

[TAP:AXETF] Inheritance

[TAP:AXETF] Inheritance [TAP:AXETF] Inheritance What makes the latter better? A. Less room for error B. Easier to understand the global structure C. All of the above D. None of the above E. Whatever 1 Administrative Details Lab

More information

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Tools : The Java Compiler. The Java Interpreter. The Java Debugger Tools : The Java Compiler javac [ options ] filename.java... -depend: Causes recompilation of class files on which the source files given as command line arguments recursively depend. -O: Optimizes code,

More information

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS ARRAYS A Java array is an Object that holds an ordered collection of elements. Components of an array can be primitive types or may reference objects, including other arrays. Arrays can be declared, allocated,

More information

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture. CSE1710 Click to edit Master Week text 12, styles Lecture 23 Second level Third level Fourth level Fifth level Fall 2014! Thursday, Nov 27, 2014 1 Big Picture Today is last day covering Ch 6 Tuesday, Dec

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept F1 A Java program Ch 1 in PPIJ Introduction to the course The computer and its workings The algorithm concept The structure of a Java program Classes and methods Variables Program statements Comments Naming

More information

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question) CS/B.TECH/CSE(New)/SEM-5/CS-504D/2013-14 2013 OBJECT ORIENTED PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates are required to give their answers

More information

Fall 2017 CISC124 9/16/2017

Fall 2017 CISC124 9/16/2017 CISC124 Labs start this week in JEFF 155: Meet your TA. Check out the course web site, if you have not already done so. Watch lecture videos if you need to review anything we have already done. Problems

More information

Java Classes & Primitive Types

Java Classes & Primitive Types Java Classes & Primitive Types Rui Moreira Classes Ponto (from figgeom) x : int = 0 y : int = 0 n Attributes q Characteristics/properties of classes q Primitive types (e.g., char, byte, int, float, etc.)

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

COE318 Lecture Notes Week 5 (Oct 3, 2011)

COE318 Lecture Notes Week 5 (Oct 3, 2011) COE318 Software Systems Lecture Notes: Week 5 1 of 6 COE318 Lecture Notes Week 5 (Oct 3, 2011) Topics Announcements Strings static and final qualifiers Stack and Heap details Announcements Quiz: Today!

More information

Java Classes & Primitive Types

Java Classes & Primitive Types Java Classes & Primitive Types Rui Moreira Classes Ponto (from figgeom) x : int = 0 y : int = 0 n Attributes q Characteristics/properties of classes q Primitive types (e.g., char, byte, int, float, etc.)

More information

COE318 Lecture Notes Week 4 (Sept 26, 2011)

COE318 Lecture Notes Week 4 (Sept 26, 2011) COE318 Software Systems Lecture Notes: Week 4 1 of 11 COE318 Lecture Notes Week 4 (Sept 26, 2011) Topics Announcements Data types (cont.) Pass by value Arrays The + operator Strings Stack and Heap details

More information

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen Other conditional and loop constructs Fundamentals of Computer Science Keith Vertanen Overview Current loop constructs: for, while, do-while New loop constructs Get out of loop early: break Skip rest of

More information

MODULE 02: BASIC COMPUTATION IN JAVA

MODULE 02: BASIC COMPUTATION IN JAVA MODULE 02: BASIC COMPUTATION IN JAVA Outline Variables Naming Conventions Data Types Primitive Data Types Review: int, double New: boolean, char The String Class Type Conversion Expressions Assignment

More information

JAVA Programming Fundamentals

JAVA Programming Fundamentals Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

More information

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Michele Van Dyne Museum 204B  CSCI 136: Fundamentals of Computer Science II, Spring Michele Van Dyne Museum 204B mvandyne@mtech.edu http://katie.mtech.edu/classes/csci136 CSCI 136: Fundamentals of Computer Science II, Spring 2016 1 Review of Java Basics Data Types Arrays NEW: multidimensional

More information

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

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming Exam 1 Prep Dr. Demetrios Glinos University of Central Florida COP3330 Object Oriented Programming Progress Exam 1 is a Timed Webcourses Quiz You can find it from the "Assignments" link on Webcourses choose

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information