CSCB 07 TUTORIAL 2 Java Basics SVN For

CSCB 07 TUTORIAL # 2 Java Basics

SVN For Assignments (Review) � To checkout, use: svn co svn+ssh: //(UTORid)@mathlab. utsc. utoronto. ca/svn/c scb 07 f 12/UTORid � Before starting coding always use: svn update � After finished some part of code, use: svn commit � If added a file use: svn add

Variables and Pointers (Aliases. java) � Primitive data types: int, boolean, double, long, char -Variable assignment creates a new variable! (new memory location) � Objects: String, Array. List, Integer -Variable assignment passes a reference to the same object! (same memory location use ‘new’ to create a new modified object)

Comparisons and Equality (Equality. java) � For Objects, use: . equals() (== will check the address in memory) � For Primitive Types, use: ==

Basic Objects (More. Basics. java) � String a = "Joshua"; Strings are immutable! a+a is still a! � String. Builder c = new String. Builder("Baby"); String builder is like a mutable String � Arrays: int[][] table = new int[50][30] Can’t change the size once created! (recreate) � Array. List: Array. List<String> names = new Array. List<String>(); Size changes as needed + lots of functions!

Loops � int num = 5 while (num > 0) { num --; } � for (int i = 0; i < 10; i++) { } � String word = ”icantcomeupwithone” for (char letter : word){ System. out. println(letter); }

Casting and Wrappers � Wrapper is an Object class for a primitive type Eg: Integer, Double, … They provide extra functions like conversion: Integer. parse. Int(“ 56”); � Casting is explicit conversion to some Object obj = “Stringular. Thing”; String str = (String) obj; Help avoid compiler errors but may cause runtime. Careful!

Scope (Parameters. java Locals. java) � Scope is method’s or variable’s visibility � Passing variables to functions: For primitive data types new variable is created in the function’s scope For Objects – a reference to the same Object is passed along to the function’s scope � Refer to Variables and Pointers slide!
- Slides: 8