Java and C++: - Both Are "Object Oriented" - Java Is Interpreted, and Garbage Collected
Java and C++: - Both Are "Object Oriented" - Java Is Interpreted, and Garbage Collected
if (a.equals(b)) {
System.out.println("Variables are equal.");
} else {
System.out.println("Variables are NOT equal.");
}
if (a == b) {
System.out.println("Variables are equal.");
} else {
System.out.println("Variables are NOT equal.");
}
Why?
Explanation
• “abc” in each case is converted to a String
object by compiler
• Compiler is smart, and in some cases will
not create duplicate identical string
constants
– Saves a little memory
• Just uses reference for second instance of
constant
Converting from Strings to Other
Values
public class convert {
public static void main(String args[]) {
int x;
String s = "100";
// convert String to integer
x = Integer.parseInt(s);
x += 150;
System.out.println("x= " + x);
}
}
More Conversions
String s = “100.1”;
String m = “x”;
import java.io.*;
class X {
static int foo = 10;
}
public class TestStatic {
public static void main(String args[])
{
X blah = new X(); X blah2 = new X();
blah = null; blah2 = null;
System.gc(); // suggest that garbage collector be called
}
}
class X {
// keep track of number of active instances
static int numInstance = 0;
X() { // constructor
numInstance++;
}
class X {
static void print() {
System.out.println("This is a static method");
}
}
Inheritance
• Java does not support multiple inheritance
– I.e., a subclass cannot be derived from multiple
superclasses
– A class can implement interfaces, however
• Keyword is: extends
// assuming we have a predefined Shape class
public class Circle extends Shape {
// define Circle class
// inherits methods and variables from Shape class
}
Inheritance Exercise
• Write the beginnings of a Shape class, in Java
– Instance variables:
• x, y– giving the coordinates of the shape object
• Color (java.awt.Color)
– Constructors
• set coordinates
• No parameters; just give default coords
• Then, subclass (extend) the Shape class by making
a Circle class
– Add instance variable:
• radius
// can’t start this class using “java” program
import java.awt.Color;
public class Shape {
protected float x, y; // coords of object
protected Color c; // protected: visible within class, package, and package subclasses
public Shape() {
c = null;
x = 0.0f; // need to explicitly give type here!
y = 0.0f; // by default a real constant is a double
// compiler complains at possible loss of precision
}
public Shape(float x, float y) {
c = null;
this.x = x; // implicit this argument
this.y = y;
}
}
while (tok.hasMoreTokens()) {
String intString = tok.nextToken();
System.out.println("string= " + "'" + intString + "'");
}
}
}
OUTPUT:
string= '123'
string= '456'
string= '789'
import java.util.StringTokenizer; // Example 2
import java.io.*;
int x = Integer.parseInt(tok.nextToken());
int y = Integer.parseInt(tok.nextToken());
int z = Integer.parseInt(tok.nextToken());
if (myFile.exists()) {
if (myFile.isFile()) System.out.println("It is a file");
if (myFile.isDirectory()) System.out.println("It is a directory");
if (myFile.canWrite())
System.out.println("Can write the file/directory");
if (myFile.canRead()) System.out.println("Can read the file/directory");
myFile.delete();
}
}
}
java.io.FileInputStream
• Read a stream of bytes from a file
public FileInputStream(String name)
throws FileNotFoundException;
public FileInputStream(File file);
public int read(byte [] b, int off, int len)
throws IOException;
import java.io.*;