[Release] Java tutorial part two Java 

Junior Spellweaver
Joined
Jul 8, 2008
Messages
164
Reaction score
0
This part is for people who already have a reasonable understanding of java. If you are NOT one of them, I suggest you read this. Here, we will begin to explore the classes and interfaces of the Java-API. I'll make this in a new thread because this is more advanced.

Exploring java.lang

java.lang.System - This describes information about the system.

Important properties

System.out - Reference to the PrintWriter associated with the console.
System.in - Reference to the Reader associated with the console. This is connected to the keyboard.
System.err - Reference to the Error console.

Important functions

System.exit(int status) - Exits this JVM (Java Virtual Machine). If status is not zero, it means an error occurred.
System.getProperty(String what) - Gets a property from the users computer. what can be "user.dir", "user.home", "os.name", "os.arch" and a lot more. Search on google for more.
System.getSecurityManager() - Returns the security manager that controls what you can and can't do to the users computer.

Generics

J2SE 5 added a new dimension of programming. This dimension is called "Generics". Generics allow you to define type parameters for a class. This is extremely powerful. Just look at the examples:

Code:
class <T> {
}

Here, T must be declared as a data type when this class is instantiated. For example:

Code:
class <T> {
}

class use {
 T<Integer> = new T<Integer>();
}

What the above code does, is set the type parameter, that is T, to Integer. You can use any class you like for the parameter, eg T<Double>, T<InputStream> etc.

More Generics

What if you wanted to declare a generic class which needed its parameter to have a function such as doubleValue? All subclasses of Number have this method, so we can make something like this:

Code:
class <T extends Number> {
}

That will ensure all type parameters, that is T has to be Number or a subclass of Number.

Things you must be aware of when using generics

These are some things you must avoid when using generics:

It is illegal it instantiate a type parameter, eg:
Code:
 class <T> {
  public class() {
   T t = new T(); //ILLEGAL!
  }
 }

The reason should be easy to understand, it is because T is unknown at run time, so the compiler doesn't know what class it should Instantiate.

It is illegal it make a subclass of java.lang.Throwable generic.

This means you cannot make generic exceptions.

The introduction of generics sometimes causes a new error, called Ambiguity. This is when two or more things are the same, which means the compiler cannot decide what to call.
This is an example of ambiguity:

Code:
public class fail<T, V> {
  T t;
  V v;
  void set(T t) {
    this.t = t;
  }
  
 void set(V v) {
    this.v = v;
 }
}

public class fail2 {
  public static void main(String[] args) {
   fail<String, String> = new fail<String, String>();
   fail.set("LOL");
 }
}

This code has an error, because both sets matches String, so it cannot decide what one to call. A way around this is just to use different names, eg : setT, setV.

Autoboxing

Autoboxing means you can declare code like this:
Code:
Integer i = 3;

But Integer is an object and 3 is an int, how can this work? The JVM automatically boxes 3 into an Integer, and assigns it to i. This makes coding so much more efficient.

Interfaces

Interfaces are one of the strengths of java, because they allow you to make one interface refer to all sorts of classes which implement it.

Here is an example of this power:

Code:
interface ValueHolder {
  Object get();
}

public class StringHolder implements ValueHolder {
 String val;

 public StringHolder(String i) {
  val = i;
 }
 public Object get() {
 return val;
 }
}

public class IntegerHolder implements ValueHolder {
 Integer val;

 public IntegerHolder(Integer i) {
  val = i;
 }
 public Object get() {
 return val
 }
}


public class uses {
 public static void main(String[] args) {
  ValueHolder vh;
  vh = new StringHolder(s);
  System.out.println(vh.get());
  vh = new IntegerHolder(1);
  System.out.println(vh.get());
 }
}

Now, you might think that this code won't work, because vh is assigned StringHolder then IntegerHolder, but through the power of interfaces, it does work. This is one of Java's major strengths.

Abstract Classes

Abstract Classes are classes which have methods that need to be filled in by a subclass. This is an example:

Code:
public abstract class Abstract {
 protected int i = 0;

 public int getI() { //Concrete methods still allowed
  return i;
 }

 abstract void setI(int i);
}

public class Filler extends Abstract {
 public void set(int i) {
  this.i = i;
 }
}

Here, Filler implemented the function set. This is also a powerful feature, because it allows you to make classes which leave it up to the subclasses to define the specifics.

Casting

Casting means converting one data type to another.

For example, you can find the ASCII value of 'A' by doing this:

Code:
 public int getValueOfA() {
  int val = (int)('A');
  return val;
 }

What this code does, is converts 'A' into a integer format. It yields the value of 65.

You may also cast classes to another class IF they are compatiable. If they are not, a java.lang.ClassCastException is thrown.

Multithreading and synchronization

Multithreading is the process of multitasking. A program with multiple threads can have two or more parts that execute concurrently. This is very good for complex computations which take a long time.

To create a thread, you can either extend Thread or implement Runnable. Personally, I like to implement runnable, but the choice is yours ^_^

Code:
public class CanRun implements Runnable {
 public void run() {
 }
}

run must be implemented by any class which creates a thread. Run is just like main([]) except when run returns, the thread dies. This is how you can start a new thread:

Code:
public class CanRun implements Runnable {
 public void run() {
  System.out.println("Inside run!");
 }
}

public class Main {
 public static void main(String[] args) {
  Thread thread = new Thread(new CanRun());
  thread.start();
}

Here, start() is the method called to start the thread. This thread dies when run() returns remember...

Synchronization

Java has awesome built in synchronization, all you need to do, is preceede a method declaration with the
'synchronized' keyword. Java does the rest for you. ;) What this does, is only allows one thread to access the method at a time.
 
Last edited:
ty ^^ i been trying to find this method for ages...
Code:
System.out - Reference to the PrintWriter associated with the console.
System.in - Reference to the Reader associated with the console. This is connected to the keyboard.
System.err - Reference to the Error console.

Important functions

System.exit(int status) - Exits this JVM (Java Virtual Machine). If status is not zero, it means an error occurred.
System.getProperty(String what) - Gets a property from the users computer. what can be "user.dir", "user.home", "os.name", "os.arch" and a lot more. Search on google for more.
System.getSecurityManager() - Returns the security manager that controls what you can and can't do to the users computer.
 
Tell me about it... We learn 4 hours a week with him and never open the computer.
No wonder why all the class fails.
Luckily, I know a bit of C++ and Visual Basic so I got B+ in this subject.
All the rest of the class got about C-...
 
Tell me about it... We learn 4 hours a week with him and never open the computer.
No wonder why all the class fails.
Luckily, I know a bit of C++ and Visual Basic so I got B+ in this subject.
All the rest of the class got about C-...

wow.... this **** is part of a subject or optional ? where you live in o.o
 
I live in Israel :P
It is half optional - you have to choose a subject for at least 1 year.
I am good with computers so I took this subject but seriously, this teacher makes this subject sucks!

EDIT: *Hope that my teacher doesn't play Maple Story xD*
 
I live in Israel :P
It is half optional - you have to choose a subject for at least 1 year.
I am good with computers so I took this subject but seriously, this teacher makes this subject sucks!

EDIT: *Hope that my teacher doesn't play Maple Story xD*

aww... i only learn that in Singapore during CCA [Co-Curricular activity] it's not part of a subject ;(
*jealous* :blushing:
 
Back