UNIT 1 - Java Boot Camp
COE312
Imran A. Zualkernan
Source: Marvin Soloman, University of Wisconsin
1
C 2008-17 Imran A. Zualkernan
Java
C 2008-17 Imran A. Zualkernan 2
Java
• The Java language is actually rather small and simple -
an order of magnitude smaller and simpler than C++,
and in some ways, even smaller and simpler than C.
• However, it comes with a very large and constantly
growing library of utility classes.
C 2008-17 Imran A. Zualkernan 3
Common Libraries
• [Link] contains things like character strings, that are
essentially "built in" to the language.
• [Link] contains support for input and output, and
• [Link] contains some handy data structures such as lists and hash
tables.
C 2008-17 Imran A. Zualkernan 4
Classes
• Simple class definitions in Java look rather like class definitions in
C++.
class Pair { int x, y; }
C 2008-17 Imran A. Zualkernan 5
Classes and Files
• Each class definition should go in a separate file, and the
name of the source file must be exactly the same
(including case) as the name of the class, with ".java"
appended.
• For example, the definition of Pair must go in file
[Link].
• You can compile this class with the command
javac [Link]
• Assuming there are no errors, you will get a file named
[Link].
• You can run the program by
java [Link]
C 2008-17 Imran A. Zualkernan 6
Java Compilation Process
[Link] [Link] [Link]
[Link] [Link]
JVM
C 2008-17 Imran A. Zualkernan 7
Example – .java to .class
0: iconst_2
1: istore_1
2: iload_1
3: sipush 1000
6: if_icmpge 44
9: iconst_2
10: istore_2
11: iload_2
12: iload_1
outer: 13: if_icmpge 31
for (int i = 2; i < 1000; i++) { 16: iload_1
for (int j = 2; j < i; j++) { 17: iload_2
if (i % j == 0) continue outer; 18: irem
} [Link] (i);
compiler
19: ifne 25
} 22: goto 38
25: iinc 2, 1
[Link] 28: goto 11
31: getstatic #84; //Field
java/lang/[Link]:Ljava/io/PrintStream;
34: iload_1
35: invokevirtual #85; //Method java/io/[Link]:(I)V
38: iinc 1, 1
41: goto 2
44: return
Source: [Link] [Link]
8
C 2008-17 Imran A. Zualkernan
Compile once – run anywhere
[Link]
[Link]
Modified from: [Link]
C 2008-17 Imran A. Zualkernan 9
Packages
• There is a large set of predefined classes,
grouped into packages.
• The full name of one of these predefined classes
includes the name of the package as prefix.
• For example, the library class [Link]
is in package [Link], and a program may use
the class with code like this:
[Link] r = new [Link]();
C 2008-17 Imran A. Zualkernan 10
import statement
• The import statement allows you to omit the
package name from one of these classes.
(similar to #include in C++)
• A Java program that includes the line import
[Link];
can abbreviate the use of Random to
Random r = new Random();
C 2008-17 Imran A. Zualkernan 11
import
• You can import all the classes in a package at once with
a notation like
import [Link].*;
• The package [Link] is special; every program
behaves as if it started with import [Link].*;
C 2008-17 Imran A. Zualkernan 12
Creates a reference (similar Need at least one
to a pointer). Note capital H. Example class with main()
public class HelloTest {
public static void main(String[] args) {
Hello greeter = new Hello();
[Link]();
}
}
In C++
Like C++, creates an
Hello * greeter = new Hello(); instance of the object
on the heap.
C 2008-17 Imran A. Zualkernan 13
Example
public class Hello {
void speak() {
[Link]("Hello World!");
}
}
In C++
Similar to cout
cout << “Hello World!”;
C 2008-17 Imran A. Zualkernan 14
Primitives and non-primitives
• Java makes a big distinction between values
(integers, characters, etc.) and objects.
Primitive – small i
int i;
Object – capital I
Integer ii;
ii = 3; // Same as ii = new Integer(3);
i = ii; // Same as i = [Link]();
C 2008-17 Imran A. Zualkernan 15
Primitive Values
• There are exactly eight primitive types in
Java, boolean, char, byte, short, int, long,
float, and double.
• A boolean value is either true or false.
• Unlike C++ You cannot use an integer
where a boolean is required (e.g. in an if
or while statement) nor is there any
automatic conversion between boolean
and integer.
C 2008-17 Imran A. Zualkernan 16
Java Primitives
boolean: 32 bits on stack and JVM dependent on Heap (1 byte in Oracle’s JVM)
C 2008-17 Imran A. Zualkernan 17
Pointers
• Almost everything in a pointer in Java.
• Objects can only be referenced with pointers (called references in
java)
• Variables can hold primitive values (such as integers or floating-
point numbers) or references (pointers) to objects.
• A variable cannot hold an object, (e.g., Flower f; in C++)
• You cannot make a pointer to a primitive value. (e.g., can not do
something like int * x; in C++)
C 2008-17 Imran A. Zualkernan 18
Pointers (References)
C 2008-17 Imran A. Zualkernan 19
Classes
• Objects are instances of classes.
• They are always created by the new
operator.
• Each object has a set of
– fields (data members in C++) and
– methods (function members in C++)
C 2008-17 Imran A. Zualkernan 20
Classes
• Like variables, each field can hold either a
primitive value (a boolean, int, etc.) or a
reference, which is either null or points to
another object.
• When a new object is created, its fields are
initialized to zero, null or false as appropriate.
• By contrast, variables are not automatically
initialized. It is a compile-time error to use a
variable that has not been initialized.
C 2008-17 Imran A. Zualkernan 21
Access Levels
• Each field and method has an access level:
– private: accessible only in this class
– (package): accessible only in this package
– protected: accessible only in this package and in all subclasses of this
class
– public: accessible everywhere this class is available
• Similarly, each class has one of two possible access levels:
– (package): class objects can only be declared and manipulated by code
in this package
– public: class objects can be declared and manipulated by code in any
package
Note: for both fields and classes, package access is the default, and is
used when no access is specified.
C 2008-17 Imran A. Zualkernan 22
Argument Passing to Functions
• Arguments to a Java functions are passed
"by value".
• Arguments to a Java functions are passed
"by value".
• ..
C 2008-17 Imran A. Zualkernan 23
Argument Passing to Functions
C 2008-17 Imran A. Zualkernan 24
Pass by value - Example
void f() {
int n = 1;
Pair p = new Pair();
p.x = 2; p.y = 3;
[Link](n); // prints 1
[Link](p.x); // prints 2
g(n,p);
[Link](n); // still prints 1
[Link](p.x); // prints 100
}
void g(int num, Pair ptr) {
[Link](num); // prints 1
num = 17; // changes only the local copy
[Link](num); // prints 17
[Link](ptr.x); // prints 2
ptr.x = 100; // changes the x field of caller's Pair
ptr = null; // changes only the local ptr
}
C 2008-17 Imran A. Zualkernan 25
Java and C++
• Unlike C++, Java has no way of declaring reference parameters
• Unlike C++ or C, Java has no way of creating a pointer to a (non-
object) value, so you can't do something like this
/* C or C++ */
void swap1(int *xp, int *yp) {
int tmp;
tmp = *xp;
*xp = *yp;
*yp = tmp;
}
int foo = 10, bar = 20;
swap1(&foo, &bar); /* now foo==20 and bar==10 */
C 2008-17 Imran A. Zualkernan 26
Garbage Collection
C 2008-17 Imran A. Zualkernan 27
Garbage Collection
• New objects are create by the new operator in
Java just like C++ (except that an argument list
is required after the class name, even if the
constructor for the class doesn't take any
arguments so the list is empty).
• However, there is no delete operator.
• The Java system automatically deletes objects
when no references to them remain.
C 2008-17 Imran A. Zualkernan 28
Useful How-to and references
• [Link]
c++.html
• [Link]
• [Link]
C 2008-17 Imran A. Zualkernan 60