102 Lec03
102 Lec03
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
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 age){
this.age = age;
}
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;
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;
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