java basics. to checkout, use: svn co svn+ssh://(utorid)@mathlab.utsc.utoronto.ca/svn/c...

8
CSCB07 TUTORIAL # 2 Java Basics

Upload: percival-horton

Post on 04-Jan-2016

243 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Basics.  To checkout, use: svn co svn+ssh://(UTORid)@mathlab.utsc.utoronto.ca/svn/c scb07f12/UTORid  Before starting coding always use: svn update

CSCB07 TUTORIAL # 2

Java Basics

Page 2: Java Basics.  To checkout, use: svn co svn+ssh://(UTORid)@mathlab.utsc.utoronto.ca/svn/c scb07f12/UTORid  Before starting coding always use: svn update

To checkout, use: svn co svn+ssh://(UTORid)@mathlab.utsc.utoronto.ca/svn/cscb07f12/UTORid

Before starting coding always use: svn update

After finished some part of code, use: svn commit

If added a file use:svn add

SVN For Assignments (Review)

Page 3: Java Basics.  To checkout, use: svn co svn+ssh://(UTORid)@mathlab.utsc.utoronto.ca/svn/c scb07f12/UTORid  Before starting coding always use: svn update

Primitive data types: int, boolean, double, long, char

-Variable assignment creates a new variable! (new memory location)

Objects: String, ArrayList, Integer

-Variable assignment passes a reference to the same object! (same memory location - use ‘new’ to create a new modified object)

Variables and Pointers (Aliases.java)

Page 4: Java Basics.  To checkout, use: svn co svn+ssh://(UTORid)@mathlab.utsc.utoronto.ca/svn/c scb07f12/UTORid  Before starting coding always use: svn update

For Objects, use: .equals()(== will check the address in memory)

For Primitive Types, use: ==

Comparisons and Equality(Equality.java)

Page 5: Java Basics.  To checkout, use: svn co svn+ssh://(UTORid)@mathlab.utsc.utoronto.ca/svn/c scb07f12/UTORid  Before starting coding always use: svn update

String a = "Joshua";Strings are immutable! a+a is still a!

StringBuilder c = new StringBuilder("Baby");String builder is like a mutable String

Arrays: int[][] table = new int[50][30]Can’t change the size once created! (recreate)

ArrayList: ArrayList<String> names = new ArrayList<String>();Size changes as needed + lots of functions!

Basic Objects (MoreBasics.java)

Page 6: Java Basics.  To checkout, use: svn co svn+ssh://(UTORid)@mathlab.utsc.utoronto.ca/svn/c scb07f12/UTORid  Before starting coding always use: svn update

int num = 5while (num > 0) {

num --;}

for (int i = 0; i < 10; i++) {}

String word = ”icantcomeupwithone”for (char letter : word){

System.out.println(letter);}

Loops

Page 7: Java Basics.  To checkout, use: svn co svn+ssh://(UTORid)@mathlab.utsc.utoronto.ca/svn/c scb07f12/UTORid  Before starting coding always use: svn update

Wrapper is an Object class for a primitive type

Eg: Integer, Double, …They provide extra functions like conversion:

Integer.parseInt(“56”);

Casting is explicit conversion to some Object

Object obj = “StringularThing”;String str = (String) obj;

Help avoid compiler errors but may cause runtime. Careful!

Casting and Wrappers

Page 8: Java Basics.  To checkout, use: svn co svn+ssh://(UTORid)@mathlab.utsc.utoronto.ca/svn/c scb07f12/UTORid  Before starting coding always use: svn update

Scope is method’s or variable’s visibility Passing variables to functions:

For primitive data types new variable is created in the function’s scopeFor Objects – a reference to the same Object is passed along to the function’s scope

Refer to Variables and Pointers slide!

Scope(Parameters.java Locals.java)