More advanced topics for Java
Object oriented programming
- Classes are blue prints and objects are instances. Real-world objects have states and behaviors; software objects are similar. A software object's state is stored in fields (variables) and behavior is shown via methods (functions).
Variables, local/instance/class variables
- A variable provides named storage that programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable's memory. Variables need to be declared before they can be used.
int a; // Declare an integer named a int a = 10, b = 10; // Declare & initialize double pi = 3.14159; // declare and assign PI String s = "ABC"; //declare a string
- Class/instance/local variables
keyword. Instance variables are often initialized when the class is instantiated, and accessing instance variables are done via created objects. Static variables are rarely used other than being declared as constants.- Local variables are defined inside methods, constructors or blocks
- Both instance & class variables are variables within a class but outside any method and can be accessed from inside any method, constructor or blocks of that particular class. The difference is that class variables are declared with the
static
- Class/instance/local variables
Modifiers
- Java has many modifiers (public, private, protected, static, final etc), which can be included
in the definition of a class, method, or variable. The modifier keyword precedes the rest of the statement. Some modifiers (public, protected, private) control access; others are non access modifiers, including static for defining class methods (e.g., public static void main()) & variables, final modifier for finalizing the implementations of classes/methods/variables, abstract for creating abstract classes & methods, and synchronized and volatile are used for threads.
private String name = "Unk"; static final double num = 10.0; protected static final int MAXSTEP = 10; public static void main(String[] argv) { }
Abstraction/Interface
- The interface keyword is used to declare an interface. An interface contains methods, all are abstract. And an instance cannot contain instance variables--all must be declared both static and final. A class uses the implements keyword to implement an interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.
- Abstract methods are methods without body. A class which contains the abstract keyword in its declaration is known as abstract class. If a class has at least one abstract method, then the class must be declared abstract. An abstract class cannot be instantiated. A (normal) class can inherit from an abstract class, providing implementations to all the abstract methods in it.
Examples of using class/object
//define a class named Student public class Student { //name, department are instance variables public String name = "unk"; private String department = "unk"; public Student(String name) { this.name = name; } public void setName(String name) { this.name = name; } public void setDepartment(String department) { this.department = department; } public String getDepartment() { return department; } public void display() { System.out.println("Student Information"); System.out.println("Name: " + name); System.out.println("Department: " + department); } public static void main(String[] argv) { //name is a local variable String name = "Ray"; //create an instance from the Student class Student stu1 = new Student(name); stu1.setDepartment("CS"); //create an array of students Student[] stuList = new Student[2]; stuList[0] = stu1; stuList[1] = new Student("Ben"); stuList[1].setDepartment("Biology"); stuList[0].display(); stuList[1].display(); } }
Getting data from System.in, a local file, or a URL (using Scanner)
Need to import java.io.*; & import java.net.*;
See a sample code (note the throws IOException declaration for handling exceptions)
Three steps for reading from a URL: 1) create a scanner; 2) read data; and 3) close the scanner
//Step 1: create a scanner //From a URL URL url = new URL("http://homes.soic.indiana.edu/classes/spring2016/csci/c343-yye/tweet-data-Jan16.txt"); Scanner in = new Scanner(url.openStream()); //From System.in (user's inputs) //Scanner in = new Scanner(System.in); //From a local file (e.g., tweet-data-Jan16.txt on your local machine) //Scanner in = new Scanner(new FileReader("tweet-data-Jan16.txt")); //Step 2: read data while (in.hasNext()) { //nextLine() reads a line; //Scanner class has other methods to allow the user to read values of various types, eg.., nextInt() String str = in.nextLine(); } //Step 3: close the scanner in.close();