0% found this document useful (0 votes)
43 views15 pages

102 Lec03

This document provides notes from a lecture on Java static methods, packages, and expressions. It introduces static methods and how they can be called without creating an object. It discusses how packages organize classes and how import statements are used. It also covers access modifiers like public, private, and protected. Finally, it reviews Java expressions and arithmetic operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views15 pages

102 Lec03

This document provides notes from a lecture on Java static methods, packages, and expressions. It introduces static methods and how they can be called without creating an object. It discusses how packages organize classes and how import statements are used. It also covers access modifiers like public, private, and protected. Finally, it reviews Java expressions and arithmetic operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

CMPU-102-51 Spring 2020

Data Structures and Algorithms

Lecture #3: Java static, packages,


expressions

Rui Meireles
Peter Lemieszewski

1
Introduction to Java
(IPUJ 2.1-2.3)

2
When last we looked at lecture notes
class Person{ // class definition

int age; // class field

int getAge(){ // class method


return age;
}
}
• We're still missing a few things:
– how do we create Person objects?
• … an instance of the class!
– How do we initialize age?

3
Answer: Constructor methods
• Used to initialize fields of class instance (i.e. object)
• Method Signature:
• Always same name as class
• No return type (implicit return type is class where it’s defined)
• Keyword this used to distinguish between arguments and fields of
the same name
• It refers to the current object (currently being constructed)
• We can also use different names if we want to though
class Person{ class Person{
int age; int age;

Person(int page){ Person(int age){


age = page; this.age = age;
} }
} }
4
We’re getting there
• We now have a complete class:
class Person{ // class definition

int age; // class field

Person(int age){
this.age = age;
}

int getAge(){ // class method


return age;
}
}

• But how do we make use of it? We need to bootstrap the process!

5
The main method
• By convention, a Java program starts executing code at a method with
the following signature:
public static void main(String[] args){…}
• Interesting characteristics:
• Has public access: can be called from everywhere
• Is marked as static, which means it can be called without an object
• Has no return type, i.e. it is a void method
• Receives an array of Strings as its single argument
• This array represents the input provided from the command line, with the items
separated by spaces
• Don’t worry about details for now, as we'll cover arrays later
• Example method that creates a Person object and prints its age:
public static void main(String[] args){
Person p = new Person(23);
int a = p.getAge();
System.out.println(a);
}
6
Putting it all together
class Person{

int age;

public Person(int age){ this.age = age; }

int getAge(){ return age; }

public static void main(String[] args){


Person p = new Person(23);
int a = p.getAge();
System.out.println(a);
}
}
• Running from command line:
1. Save source to file Person.java
2. Compile with javac Person.java
3. Execute with java Person

7
Static class members
• We just saw the method used for bootstrapping execution:
public static void main(String[] args){…}
• Static class members are shared by all instances of a given class, and they can
be accessed without an object
• Valid for both field and method members
• Specified by optional static prefix after access prefix and before return type
• Accessed by applying dot operator to class name, e.g.:
- Math.sqrt(4);
- System.out.println("Hello world!");
- Explanation: we're accessing the println() method of the static field out, part of class System
• It's also a way to implement “global” constants/variables, e.g.:
- Declare, in class Math:
public static double PI = 3.141592653589…;
- Use as Math.PI;
- LAB: BREAK.
8
Java packages
• Used to group classes into functional or logical sets
• Defines namespace for classes
• Prevents naming conflicts (same name in different packages OK)
• Each source file belongs to a package, specified at the top
• Format: package <name>;
- E.g.: package edu.vassar.cs.102.f19;
• Naming convention
- All lowercase (prevents conflicts with class names)
- Dot separated, from more general to more specific, e.g.:
• edu.vassar.cs.102.f19
• Dots translate into folder separators in the file system, e.g.:
- edu/vassar/cs/102/f19
- A fully-qualified class name includes package as prefix
- E.g. java.lang.String, java.io.File

9
Import statements
• Must import out-of-package classes to use their shorthand names
• E.g. String vs java.lang.String
• Import statements placed at the top of the source file, after the
package declaration
• Format: import <packageName.className>;
• E.g. import java.lang.String;
• Imports class String from package java.lang
• Can use wildcards (*) to import entire packages
• E.g. import java.lang.*;
• import java.lang.*; is implicit in every file
• Contains the most commonly used classes, e.g.:
• System, String, Object (root of Java’s class hierarchy)
• Find info about all standard library classes at:
• https://docs.oracle.com/javase/10/docs/api/

10
Access prefixes in Java
• Specifies where the member can be accessed from
– Used for classes, fields and methods
• Optional prefix in member declaration
• E.g.: private int id;
public Person(int age);
• Summary table:
Modifier/Accessible from Class Package Subclass World
public ✓ ✓ ✓ ✓
protected ✓ ✓ ✓ ✗
no modifier (package priv) ✓ ✓ ✗ ✗
private ✓ ✗ ✗ ✗

• Note: In what concerns classes, private and protected modifiers can only be used
with inner classes (classes defined inside another class, ignore for now)

11
A Java program with access specifiers
package edu.vassar.cmpu.102;

import java.lang.String; // not really needed because


import java.lang.System; // import java.lang.*; is implicit

public class Person{

private String name;


private int age;

public Person(String name, int age){


this.name = name;
this.age = age;
}

public String getName(){ return name; }


public int getAge(){ return age; }

public static void main(String[] args){


Person mv = new Person("Matthew Vassar", 226);
System.out.println(mv.getName());
System.out.println(mv.getAge());
}
} 12
Java expressions
(IPUJ 2.5)

13
Java expressions
• An expression is a piece of code that evaluates to a value of a
certain type
• The simplest expressions are:
• A literal, e.g.: 3, 8.2, 'c', "a string literal", true
• A variable, e.g. (assuming we have String s = "a string";): s
• A method call, e.g.: s.length()
• Expressions can be combined into using operators
• Builds complexity
• Applicable operators depend on data type
• E.g. we can multiply two integers, but not two String objects

14
Arithmetic operators
• +, -, *, /, %
• They're all binary operators applicable to numeric types (e.g. int)
• Exception for the unary minus, which is equivalent to multiplying by -1
• E.g. int a = 3; -a; // evaluates to -3
• Java also has a unary plus, but it has no effect
• Note that char is considered a numeric type so we can do things like:
char a = 'a';
char b = 'b';
b - a // evaluates to 1
• a % b is the remainder of the integer division of a by b
• E.g. 5 % 2 // evaluates to 1 since 2*2+1=5
• Precedence rules
• Multiplication and division take precedence over addition and subtraction
• Unary minus and plus take precedence over all

15

You might also like