
String range = "1..32";
String[] bounds = range.split("..");
String range = "1..32";
String[] bounds = range.split("\\.\\.");

String range = "1..32";
String[] bounds = range.split("..");
String range = "1..32";
String[] bounds = range.split("\\.\\.");

Well, I must admit that it's been some time since the last article but I was really overwhelmed recently – I had to accommodate in a new apartment, settle down all the things at university and finally I have started to work as a Java Software Developer. But anyway, now all the issues are resolved and I'm back to blogging again!
So today I would like to make a summary of OCPJP certification. First of all, I'm really glad that I've taken upon this endeavor. I have learnt many new things about Java that I didn't use before. Actually I'm not sure if I will ever use it but it's good to know it anyway ;-). And as luck would have it, I already have noticed how has my marked value increased. I did mention that I got a job, didn't I? ;-) Alright but you might probably want to know what I did in preparation for this exam. So obviously on the first place there is a book, the only one, don't even think about getting any other - SCJP Sun Certified Programmer for Java 6 Exam. I read it twice. The first time was a quick review of the book and getting familiar with some completely new areas. The second time though was much more careful and full of experiments with the code. And that is really vital! Experiment with the code – without this you will score much lower than you could if you did it. I know, it takes some time but trust me on that, it's really worth it. Ok, so let's assume that you already read the book, as many times as you wanted and you are wondering what next... Well, in my case it was a series of mock tests. Mostly by Whizlab (http://www.whizlabs.com/scjp/certification-exam.html). But in this place I have one more important tip. After solving a mock test also do some experiments with the code in exercises, especially the ones that were not correct. And I guess that's all! If you do more or less what I've described I'm sure you will do well on the exam!
As for me, my next goal on the Oracle Certification path is Oracle Certified Professional, Java EE 5 Web Component Developer! Soon, you will be able to follow my preparation's activities to this exam!
Integer i1 = Integer.valueOf("3");
Integer i2 = Integer.valueOf(123);
Float f = Float.valueOf("2.2");
Float f = Float.valueOf("5.5");
byte b = f.byteValue();
short s = f.shortValue();
double d = f.doubleValue();
double d = Double.parseDouble("123.456");
int i = Integer.parseInt("345");
byte b = Byte.parseByte("127");
int a = 5; (fits well)
long b = 6; (fits well since long is bigger than int)
but what if we try to assign the integer literal (so an int) to the smaller type? Let's have a look!byte c = 7;
And yeah, there is still no problem. That's of course good for us but basing on the above that shouldn't work. Well, that happens because the compiler makes an implicit cast so in fact, it looks like this:byte c = (byte)7;
Alright. That's very convenient for us. But this implicit cast is applied only to literals! So this is not going to work:int a = 5;
byte b = a;
To compile the code consisting of the above assignments you need to do an explicit cast:int a = 5;
byte b = (byte)a;
Also keep in mind the following rule. The result of any expression involving an int or anything smaller, like byte and short will result in an int! So the following will... not compile!byte a = 5;
byte b = 1;
byte c = a + b;
You need to put an explicit cast! One more thing about the floating points literals. They are always doubles. And here the compiler won't make an implicit cast, the following code will not compile:float d = 2.5;
Instead you should write:float d = (float)2.5;
Or mark that what you mean is really a float by adding and f or F at the end of the literal:\float d = 2.5f;
Ok, that's it for the literals! Now let's move on to the primitive casting.int a = 5;
byte b = (byte)a;
long c = 'a' + 'b';
a = b;
c = a;
c = b;
These are all legal assignments. But the followings are not:b = a;
b = c;
a = c;
And what do we need? An explicit cast!b = (byte)a;
b = (byte)c;
a = (int)c;
Et voila monsieur! Also the same thing happens with floating points. All the following are legal:double d = 2.5;
float f = 3.4f;
int i = 12;
long l = 45;
d = l; // assigning long to double
d = f; // assigning float to double
f = i; // assigning integer to float
But in case when a truncation may occur we have to make an explicit cast otherwise our code will produce an error.double d = 2.5;
float f = 3.4f;
int i = 12;
long l = 45;
f = (float)d;
i = (int)f;
l = (long)d;
That's a piece of cake but I'm sure that you've found here something new and interesting for you!
class Vehicle {}
class Car extends Vehicle {}
public class Sfield {
public static void main(String[] args) {
Vehicle v = new Vehicle();
Car c = (Car)v;
}
}
class Vehicle {}
class Car extends Vehicle {}
public class Sfield {
public static void main(String[] args) {
String v = new String("Vehicle");
Car c = (Car)v;
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:But in the first example it might happen that the v references to the Car object. But it was not possible to verify this at the compilation time.
Cannot cast from String to Car
class U1 {
private U1() { }
public U1(int x) { }
}
class U2 extends U1 {
U2() {
System.out.println("U2 construction...");
}
}
class U1 {
private U1() { }
public U1(int x) { }
}
class U2 extends U1 {
U2() {
super(5);
System.out.println("U2 construction...");
}
}
class T1 {
static int a = 5;
}
class T2 extends T1 { }
public class Sfield {
public static void main(String[] args) {
T2.a = 6;
System.out.println(T1.a);
System.out.println(T2.a);
}
}
6What would happen though, if in T2 we define again the static int a? Let's have a look:
6
(Luckily there is no third 6 since it could have got scary)
class T1 {
static int a = 5;
}
class T2 extends T1 {
static int a = 6;
}
public class Sfield {
public static void main(String[] args) {
System.out.println(T1.a);
System.out.println(T2.a);
}
}
5So now apparently we have different static fields for each class and its instances. What does matter here, is the type of the reference variable that we try to access the member.
6
class R1 {
static void say() {
System.out.println("R1");
}
}
class R2 extends R1 {
static void say() {
System.out.println("R2");
}
}
public class Sfield {
public static void main(String[] args) {
R1.say();
R2.say();
}
}
R1But still it's not an override but a redefinition!
R2
enum Colour {RED, YELLOW, BLUE}
enum Colour {
RED("bloody"), YELLOW("shiny"), BLUE("sea");
String info;
Colour(String info) {
this.info = info;
}
}
public class Woo {
public static void main(String[] args) {
for(Colour c: Colour.values()) {
System.out.println(c.info + " " + c);
}
}
}
enum Colour {
RED("bloody"),
YELLOW("shiny"),
BLUE("sea") {
public String moreInfo() {
return "extreamly";
}
};
String info;
Colour(String info) {
this.info = info;
}
public String moreInfo() {
return "mmm...";
}
}
public class Woo {
public static void main(String[] args) {
for(Colour c: Colour.values()) {
System.out.println(c.moreInfo() + " " + c.info + " " + c);
}
}
}
package example;
class DefaultAccess {
int a;
}package protectedExample;
public class Loo {
protected int a;
}
package protectedExampleDifferent;
import protectedExample.Loo;
public class Woo extends Loo{
void callWoo() {
System.out.println(a);
}
}
package protectedExampleDifferent;
import protectedExample.Loo;
public class Woo extends Loo{
void callWoo() {
System.out.println(a);
}
}
class Woo2 {
void test() {
Woo woo = new Woo();
System.out.println(woo.a);
}
}And here it's not gonna work. Even though the member 'a' is inherited and can be used within the Woo class, it can't be accessed by the dot operator by another class.package protectedExampleDifferent;
import protectedExample.Loo;
public class Woo extends Loo{
void callWoo() {
System.out.println(a);
}
}
class Woo2 extends Woo {
void test() {
System.out.println(a);
}
}
-classpath /com/foo/bar:/com/foo2/bar:bar/foo
-classpath /com/foo/bar:/com/foo2/bar:bar/foo
-classpath /com/foo2/bar:/com/foo/bar:bar/foo
-classpath /com/foo/bar:/com/foo2/bar:bar/foo:. -classpath /com/foo/bar:/com/foo2/bar:bar/foo:.:/com/foo/bar/my.JAR
public static void sleep(long milliseconds) throws InterruptedException
try {
Thread.sleep(1000);
catch(InterruptedException ie) {}
Thread t = new Thread();
try {
t.sleep(1000);
catch(InterruptedException ie) {}
public static void yeld()
public final void join() throws InterruptedException
public class MyThreadClass implements Runnable {
static int sum = 0;
public void run() {
try {
Thread.sleep(1000);
} catch(Exception e) {}
for(int i = 0; i < 100; i++) {
sum += 1;
}
}
public static void main(String[] args) throws Exception {
MyThreadClass r = new MyThreadClass();
Thread t = new Thread(r);
t.start();
System.out.println("Started!");
t.join();
System.out.println("The result is: " + sum);
}
}
public class MyThreadClass extends Thread {
public void run() {
System.out.println("This is how we do it!");
}
}
public class MyThreadClass2 implements Runnable {
public void run() {
System.out.println("This is how we do it better!");
}
}
MyThreadClass myThreadClass = new MyThreadClass();
Duh... MyThreadClass2 r = MyThreadClass2();
Thread t = new Thread(r);
t.start();
public class MyClass implements Runnable {
private static int a = 5;
public synchronized static void play() {
for(int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " work with " + a + " and is counting: " + i);
try {
Thread.sleep(1);
} catch(Exception e) {}
}
}
public synchronized void play2() {
for(int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " work with " + a + " and is counting: " + i);
}
}
public void run() {
if(Thread.currentThread().getName().equals("first")) {
play();
}
else {
play2();
}
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
Thread first = new Thread(myClass);
Thread second = new Thread(myClass);
first.setName("first");
second.setName("second");
first.start();
second.start();
}
}
first work with 5 and is counting: 0
first work with 5 and is counting: 1
second work with 5 and is counting: 0
second work with 5 and is counting: 1
first work with 5 and is counting: 2
second work with 5 and is counting: 2
second work with 5 and is counting: 3
second work with 5 and is counting: 4
second work with 5 and is counting: 5
first work with 5 and is counting: 3
second work with 5 and is counting: 6
second work with 5 and is counting: 7
second work with 5 and is counting: 8
first work with 5 and is counting: 4
second work with 5 and is counting: 9
first work with 5 and is counting: 5
first work with 5 and is counting: 6
first work with 5 and is counting: 7
first work with 5 and is counting: 8
first work with 5 and is counting: 9