1 of 31

COMP 210 -- Welcome!

Lecture 0

Thanks to Kris Jordan, Ketan Mayer-Patel and Dave Stotts for their help and large parts of the course material!

1

2 of 31

Welcome!

2

3 of 31

What are we learning in this class?

  • Data Structures
    • Central to everything we do as computer scientists
    • All around us!

…and..

  • Analysis
    • Motivate the need for efficiency
    • Make decisions quantitatively about the best ways to organize and store data in a system

But first… a crash course on Java.

3

4 of 31

Warm-up!

4

5 of 31

Time to dive in! :)

https://www.cs.unc.edu/~kakiryan/teaching/summer-210/exercises/ex01.html

5

6 of 31

Let’s Begin!

6

7 of 31

Writing Programs in Java! (1/2)

  • All code in Java is organized into classes (and interfaces)
    • Classes (and interfaces) organized into packages
    • Class and interface names should start with a capital letter. Other variables should not!

  • Java compiler (javac) produces “bytecode

  • Java interpreter (java) executes the program

7

8 of 31

Writing Programs in Java! (2/2)

  • Java interpreter (java) executes the program
    • Specify a specific class name as the main class for the program
    • Execution begins with the method “main” in the specified class.

  • The main method has the following declaration:

public static void main(String[] args)

8

9 of 31

Compiling Java to Bytecode

  • Java’s Intermediate representation (IR) is called bytecode
  • The Java Compiler takes .java files to .class files
  • The Java Virtual Machine (JVM) reads this bytecode and compiles it to machine code Just-in-Time (JIT)

9

10 of 31

Java’s Intermediate Representation Model

10

Main.java

Main.class

  1. Compile

2. Run

Main.class

Java Virtual Machine (JVM) Program

Just-in-Time Machine Code

11 of 31

Zooming out for a moment: Execution Models

  1. Compilation to native machine code
    1. Examples: C, C++, Rust

  • Compilation to an Intermediate Representation (IR):
    • IR is then interpreted and/or compiled to native machine code by another program, like a Virtual Machine
    • Examples: Java, Scala, C#, Virtual Basic

  • Interpretation via another program
    • Examples: Python, Bash, JavaScript

11

12 of 31

The “Main” Method

public static void main(String[] args)

  • public: The method can be accessed from
  • static: The method is a class method (will discuss later)
  • void: The method does not return anything
  • main: The name of the method is main
  • String[] args: The method has one parameter, and it is an array of Strings, named args

12

13 of 31

Declaring Variables in Java

  • Names must be declared with a type. This type does not change.
    • type name;
    • type name1, name2, name3;
    • type name = value;
    • These names must all begin with a lowercase value. Use camelCase!

13

14 of 31

Value Data Types

  • Value Types: defined entirely by their value
    • int
    • boolean
    • char
    • float
    • double
    • byte
    • short
    • long

14

15 of 31

Reference Data Types

  • Reference Types: are structures in memory (aka: objects)
  • Uniquely identified by an address in memory
    • The “value” of a variable that holds a reference type is this address
  • Examples:
    • String
    • Arrays (Of any type)
    • Instances of classes (Objects created with the keyword new)

15

16 of 31

Strings in Java

  • As a literal
    • Enclosed in double quotes
    • Escape sequences for common non-printable characters
  • As the result of String concatenation
    • Note that + operator used with String and non-String will convert non-String to its String representation.
  • Useful String methods:
    • length()
    • charAt(index)
    • equals(otherString)
    • subString(beginIndex, endIndex)
    • trim()
    • indexOf(substring)
    • split()

16

17 of 31

Scopes

  • Scope: where a name is valid

  • Local variables: are valid within the block of statements where the declaration occurs
    • The declaration must occur before the variable is used
    • Parameter names are local variables, method body is the scope

17

18 of 31

Arrays

  • Arrays: hold an indexed sequence of values
    • Indices start at 0
    • Can’t mix types

  • Arrays are fixed length
    • Must be specified when created
    • Once created, cannot be resized
    • BUT, individual elements can be changed/mutated!

18

19 of 31

Control Flow

if (boolean_expression) {

// Block executes if expression is true

} else if (boolean_expression) {

// Can chain multiple ”else if” blocks as needed.

} else {

// Trailing optional else block executes if none above do.

}

  • Boolean expression: an expression that evaluate to either true or false
    • Reminder: an expression is a sequence of symbols that can be evaluated to produce a value. Can be used wherever a value is expected.
    • Don’t test booleans against literals

19

20 of 31

For Loops

for (init; test; update) {

// Loop body

}

20

21 of 31

While Loops

while (boolean_expression) {

// Loop body

}

21

22 of 31

Defining Functions and Procedures in Java (1/6)

static access_modifier return_type name (param list) {

...

// method body

...

}

22

If present, indicates that method is a “class” method.

If not present, indicates that method is an “instance” method.

  • Class, or static, methods can be called by using the class name.
  • Instance methods can only be called using an object.
  • For now, we will only use class methods.

23 of 31

Defining Functions and Procedures in Java (2/6)

static access_modifier return_type name (param list) {

...

// method body

...

}

23

One of the following:

  • public
  • private
  • protected
  • No modifier

  • public: Can be used anywhere within the class, in a different class of same package, or in a different class in a different package
  • private: Can only be used in this class.
  • protected: Can be used within the class, in a different class of the same package, or in a subclass (we will look at this later)
  • no modifier: Can be used in any class in the same package.

24 of 31

Defining Functions and Procedures in Java (3/6)

static access_modifier return_type name (param list) {

...

// method body

...

}

24

Type of value returned or void if method does not have a return value

  • Return statement: ends execution of a method and returns the value of the expression as the result of the method.
  • Syntax for non-void:

return expression;

  • Syntax for void:

return;

Examples:

  • int - Method returns an integer
  • String - Method returns a String object reference
  • SomeClass - Method returns a SomeClass object reference
  • double[] - Method returns a reference to an array of double values.

25 of 31

Defining Functions and Procedures in Java (4/6)

static access_modifier return_type name (param list) {

...

// method body

...

}

25

Name of the method… named using the camelCaseConvention!

26 of 31

Defining Functions and Procedures in Java (5/6)

static access_modifier return_type name (param list) {

...

// method body

...

}

26

Comma separated list of parameter names preceded with their type.

If absent, then method needs no parameters.

Examples:

  • (int a, int b, int c) - 3 integer parameters
  • (String aString)- 1 String parameter
  • (boolean[] barray, double val) - 1 boolean array and 1 double
  • () - No parameters

27 of 31

Defining Functions and Procedures in Java (6/6)

static access_modifier return_type name (param list) {

...

// method body

...

}

27

A sequence of statements… each ends in a semicolon!

28 of 31

Calling (Static) Methods

  • Calling a static method from within the same class:
    • methodName(arguments);
  • Calling a static method from a different class (same package)
    • ClassName.methodName(arguments);
  • Calling a static method defined in a different package:
    • PackageName.ClassName.methodName(arguments)
  • Arguments: comma separated list of values
    • Must match in number and type of parameters according to method’s signature.

  • A method call that returns a value (i.e., not a “void” method) can be part of an expression.
    • int max_times_min = max(a, b, c) * min(a, b, c);

28

29 of 31

Objects

  • Objects: allow you to glue together several pieces of data (!) into a structure (!) that you can then treat as a unit
  • Create objects via the new keyword
  • Dereference operator . for field access

29

30 of 31

The Scanner Class

  • Used to read input
  • … and you must anticipate the type of input!
  • Being able to read documentation is also an important skill
    • Google: “Scanner class Java 18”
    • https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/Scanner.html

30

31 of 31

Reminders

  • Software setup
  • By tomorrow night: EX00 on Gradescope
  • Office hours! Swing by just to say hi :)

31