On Java
On Java
History of Java
OO is ubiquitous!
Lots of commercially produced code in C++ (and Java,
Smalltalk, etc.)
Focus on data, not procedures
Encapsulates data and functions
More “natural”
Exploits inheritance
(Almost) Everything in Java is an object!
First Java Program: Hello, World!
// This is a comment
// The file is named HelloWorld.java
public class HelloWorld {
public static void main(String args[]) {
System.out.println(“Hello World!”);
}
}
Note: If the class is public, the name of the class
(i.e., HelloWorld) must be the same as the name of
the file plus the .java extension (i.e.,
HelloWorld.java)
println explained
System is a class.
It has a class variable named out.
out refers to an object of type PrintStream.
PrintStream objects have an instance method
named println.
Java: The Programming Language
How to Compile and Run
Comments
// comment on one line
/* comment on one or more lines */
/** documenting comments */
Statements
int x; // statement ends with a semicolon (;)
x = 1 + 2;
Primitive Data Types
Integers Floats
byte (8 bits)
float (32 bits)
short (16 bits)
double (64 bits)
Boolean
int (32 bits)
long (64 bits) boolean (1 bit)
Characters
char (16 bits Unicode) Strings
String (an example of class
data type)
Primitive data types
capital case (because it is a class, not a primitive)
Identifiers and Literals
Identifiers
case sensitive
have no maximum length
start with a letter, underscore, or dollar sign
e.g., user_name, _file, $money
Literals
numeric: 2 (int), 3L (long), 2.0f (float) 3.14 (double), 077
(octal), 0xDC (hex)
character: ‘a’, ‘t’
boolean: true, false
string: “hello”
// Student.java
public class Student {
private String name; // name is a field, attribute, or an instance variable
public Student(String n) { // This is a constructor. Note that it has the
name = n; // same name as the class name. More on this later.
}
public void setName (String n) {
name = n; The getter and setter methods. Typically, get
} and set methods are defined for each field.
public String getName () { If a field is named, say, workAddr, then a get
return name; method named getWorkAddr() and a set method
} named setWorkAddr(…) are created.
public void print () {
System.out.println(“Name is “+name);
}
}
Classes (3)
Example:
Student s1; // s1 has the value null
s1 = new Student(“A”); s1
s1 = new Student(“B”); A
s1
A
A will be garbage collected
Methods
defined the behaviors of a class (called member
functions in C++)
can only be implemented in classes (no standalone
code)
must have a return type unless they are constructors
(which have no return type)
must have a comma separated list of pairs of parameter
types and names (if takes no parameter, the list is
empty)
Method Definition
Syntax: optional
[accessType] [modifier(s)] returnType methodName (parameter list) {
… // method body
}
Examples:
public static void main(String[] args) {
…
}
private int myMethod(int j, String s) {
…
}
String myMethod2( ) {
…
}
Constructors
Default constructor
A default constructor is automatically provided for you in every
class
Format: public MyClass() { // assume class name is MyClass
}
Allows you to do:
MyClass mc = new MyClass( );
Will be invalidated if you add a constructor declaration with
arguments
Access Specifiers
Accessibility Criteria
Fields: Methods:
Superclass All the fields and
f1, f2, and f3 m1,m2, m3, m4, m5, and m6 methods are inherited
by the subclass
// Person.java // Student2.java
public class Person { public class Student2 extends Person {
private String name; private float gpa;
public Person (String n) { public Student2 (String n, float g) {
name = n; super (n); // calls superclass constructor
} gpa = g;
public void setName (String n) { }
name = n; public void setGpa (float g) {
} gpa = g;
public String getName () { }
return name; public float getGpa () {
} return gpa;
}
public void print () {
public void print () { // overriding
System.out.println(“Name is super.print(); // calls superclass print()
“+name); System.out.println("gpa is” +gpa);
} }
} }
Overriding vs. Overloading
Overriding
Subclass can specialize the methods of the superclass by changing
the operation of a method declared by the superclass without
changing the interface.
Overridden method can be invoked using super
Same method name, same parameter list, same return type,
different body
Overloading
A class can enhance its functionality by providing a means for the
user to call a method with a different number of arguments or type.
Same method name, same or different return type, different
parameter list, different body
Overloading: Example