introduction to java
TRANSCRIPT
![Page 1: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/1.jpg)
Introduction to Java
CST200 – Week 1: Introduction to Java
Instructor: Andreea Molnar
![Page 2: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/2.jpg)
Outline
•Aims
•Basic Concepts
•Strings
•Arrays
![Page 3: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/3.jpg)
Aim
•The aim of this presentation is to emphasis on some of the material covered by the pluralsigh videos.
![Page 4: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/4.jpg)
Basic Concepts
•Expression: see more about them here: http://www.d.umn.edu/~gshute/java/statements.html
•Algorithm: see a definition here: http://en.wikipedia.org/wiki/Algorithm
![Page 5: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/5.jpg)
Strings and Arrays
The first letter of a String is at the position 0 and the first value of an array is at the position 0.
![Page 6: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/6.jpg)
Strings
Any sequence of characters, for example:
String s = “Arizona”;
String s = “1457 Mesa”;
String s = “@u”;
![Page 7: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/7.jpg)
Strings - Methods
You can find all the String methods at: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
![Page 8: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/8.jpg)
Strings - Methods
public int length() – returns the length of the string
String s = “CST 200”;
s.length() – will return 7 as there are 7 characters in the string
![Page 9: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/9.jpg)
Strings - Methods
public int length() – returns the length of the string
String s = “CST 200”;
s.length() – will return 7 as there are 7 characters in the string (space is count as a separate character)
![Page 10: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/10.jpg)
Strings - Methods
public char charAt(int index)– returns the character value at the specified index
String s = “CST 200”;
C S T 2 0 0
00 1 2 3 4 5 6
index
![Page 11: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/11.jpg)
Strings - Methods
public char charAt(int index)– returns the character value at the specified index
String s = “CST 200”;
C S T 2 0 0
00 1 2 3 4 5 6
The first character of a String is at the position 0!!!
![Page 12: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/12.jpg)
Strings - Methods
String s = “CST 200”;C S T 2 0 0
00 1 2 3 4 5 6
System.out.println(s.charAt(0));//will print C
System.out.println(s.charAt(6));//will print 0
![Page 13: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/13.jpg)
Strings - Methods
public int indexOf(String str) - returns the index within this string of the first occurrence of the specified substring
String s = “CST 200”;
C S T 2 0 0
00 1 2 3 4 5 6
System.out.println(s.indexOf(“T”));
//will print 3
![Page 14: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/14.jpg)
Arrays
Array is a container that holds a fixed number of values.
int[] array = {10, 20, 30, 50};
10 20 30 50
0 2 3 4
index
![Page 15: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/15.jpg)
Arraysint[] array = {10, 20, 30, 50};
10 20 30 50
0 1 2 3
To print the values of this array:
System.out.println(array[0]);//will print 10
System.out.println(array[1]);//will print 20
System.out.println(array[2]);//will print 30
System.out.println(array[3]);//will print 50
![Page 16: Introduction to java](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f53dbb4c905423f8b53d5/html5/thumbnails/16.jpg)
Summary
•Expression definition
•Algorithm definition
•The first character of an array starts at position 0
•The first element of an array starts at the position 0