1. If directory in which the file should be created does not exist, method createNewFile() throws IOException.
try {
File f = new File("test", "test.txt");
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
----------------------------------------------------------------------------------------------------------
2. Method renameTo accepts only File objects not String!
Prototype of the method:
public boolean renameTo(File dest)
----------------------------------------------------------------------------------------------------------
3. If there is no directory structure, PrintWriter throws FileNotFoundException.
try {
PrintWriter pw = new PrintWriter("test/test/test.xml");
}catch(FileNotFoundException e) {
e.printStackTrace();
}
when the directory structure exists, the physical file is created by PrintWriter
try {
PrintWriter pw = new PrintWriter("test.xml");
}catch(FileNotFoundException e) {
e.printStackTrace();
}
----------------------------------------------------------------------------------------------------------
4. flush() method is only available in writers: FileWriter, BufferedFileWriter, PrintWriter.
flush() method is not available in readers.
czwartek, 15 września 2011
poniedziałek, 12 września 2011
INITIALIZATION BLOCKS
1. It is legal to create instance of outer class within both: static and instance initialization blocks.
E.g.:
class X {
{ X x = new X();}
static { X x = new X();}
}
2. It is legal to use this in initialization (non-static) block:
class X {
int a;
{this.a = 4;}
}
3.
class X{
{x = 4}
int x;
public X() {
System.out.println(x); //prints 4
}
}
class X{
{x = 4}
int x = 1;
public X() {
System.out.println(x); //prints 1
}
}
class X{
int x=1;
{x = 4}
public X() {
System.out.println(x); //prints 4
}
}
exactly the same is in case of static initialization blocks
E.g.:
class X {
{ X x = new X();}
static { X x = new X();}
}
2. It is legal to use this in initialization (non-static) block:
class X {
int a;
{this.a = 4;}
}
3.
class X{
{x = 4}
int x;
public X() {
System.out.println(x); //prints 4
}
}
class X{
{x = 4}
int x = 1;
public X() {
System.out.println(x); //prints 1
}
}
class X{
int x=1;
{x = 4}
public X() {
System.out.println(x); //prints 4
}
}
exactly the same is in case of static initialization blocks
niedziela, 11 września 2011
COLLECTIONS
1. Collections methods such as: sort(), binarySerach() or reverse() accepts only Lists not Sets.
Notice also that sort() and binarySerach() methods accept only Lists containing Comparable elements of the same type. If you try to sort lists containing non-comparable elements compilation fails. If the elements are of different types ClassCastException is thrown at runtime (unless you use your own Comparator).
To reverse order you do need have list of Comparable objects.
Notice also that sort() and binarySerach() methods accept only Lists containing Comparable elements of the same type. If you try to sort lists containing non-comparable elements compilation fails. If the elements are of different types ClassCastException is thrown at runtime (unless you use your own Comparator).
To reverse order you do need have list of Comparable objects.
sobota, 10 września 2011
INNER CLASSES
1. Inner non-static class must not have static fields or methods.
2. Inner static class may have static fields and methods
3. It is illegal to add/hide static methods in anonymous inner class
eg.
class X {
public static void do() {}
}
class Y{
public void method() {
X x = new X() {
public static void do() {} //compile error
};
}
2. Inner static class may have static fields and methods
3. It is illegal to add/hide static methods in anonymous inner class
eg.
class X {
public static void do() {}
}
class Y{
public void method() {
X x = new X() {
public static void do() {} //compile error
};
}
GENERICS
1.
It is incorrect:
-using wildcards in type variable declaration, eg.
public <?> void methods() {
....
}
public <? extends T> void methods() {
....
}
public <? super Comparable> void method() {
....
}
- using super:
public <T super Comparable> T get() {
....
}
It is correct:
public <T> T get() {
....
}
public <T extends Comparable> T get() {
....
}
------------------------------------------------------------------------------------------------------------
2. Return types
Notice also that the last example method can return only T - type object.
public <T extends Comparable> T get() {
return (T) new String(""); //correct
}
public <T extends Comparable> T get() {
return new String(""); //incorrect
}
public <T extends Comparable> T get(T t) {
return t; //correct
}
------------------------------------------------------------------------------------------------------------
3.
It is correct, int[] is a Object!
List<?> l = new ArrayList<int[]>();
This one is incorrect:
List<?> l = new ArrayList<int>();
------------------------------------------------------------------------------------------------------------
4. Compiler warning is thrown every time one tries to assign non-generic element to generic one.
public static void main(String... s) {
List<Object> l = null;
ArrayList l2 = null;
l = l2; //warning
}
the exception is ? :
public static void main(String... s) {
List<?> l = null;
ArrayList l2 = null;
l = l2; //no warning
}
------------------------------------------------------------------------------------------------------------
5. Cannot create objects using wildcards, eg.:
new ArrayList<?>();
new ArrayList<? extends Number>();
new ArrayList<? super Number>();
new HashMap<String, ? extends Number>();
------------------------------------------------------------------------------------------------------------
6. One has to declare the type before return type and after method modifiers, e.g.:
public static <E> void add(E e) {}
public <E> static void add(E e) {} //incorrect
public static void <E> add(E e) {} //incorrect
It is incorrect:
-using wildcards in type variable declaration, eg.
public <?> void methods() {
....
}
public <? extends T> void methods() {
....
}
public <? super Comparable> void method() {
....
}
- using super:
public <T super Comparable> T get() {
....
}
It is correct:
public <T> T get() {
....
}
public <T extends Comparable> T get() {
....
}
------------------------------------------------------------------------------------------------------------
2. Return types
Notice also that the last example method can return only T - type object.
public <T extends Comparable> T get() {
return (T) new String(""); //correct
}
public <T extends Comparable> T get() {
return new String(""); //incorrect
}
public <T extends Comparable> T get(T t) {
return t; //correct
}
3.
It is correct, int[] is a Object!
List<?> l = new ArrayList<int[]>();
This one is incorrect:
List<?> l = new ArrayList<int>();
------------------------------------------------------------------------------------------------------------
4. Compiler warning is thrown every time one tries to assign non-generic element to generic one.
public static void main(String... s) {
List<Object> l = null;
ArrayList l2 = null;
l = l2; //warning
}
the exception is ? :
public static void main(String... s) {
List<?> l = null;
ArrayList l2 = null;
l = l2; //no warning
}
------------------------------------------------------------------------------------------------------------
5. Cannot create objects using wildcards, eg.:
new ArrayList<?>();
new ArrayList<? extends Number>();
new ArrayList<? super Number>();
new HashMap<String, ? extends Number>();
------------------------------------------------------------------------------------------------------------
6. One has to declare the type before return type and after method modifiers, e.g.:
public static <E> void add(E e) {}
public <E> static void add(E e) {} //incorrect
public static void <E> add(E e) {} //incorrect
REGEX
1.
[a-f] search for a, b, c, d, e or f
[af] search for a or f
\w search for [a-zA-Z_0-9]
\s search for space
\d search for [0-9]
. search for any character
\. search for dot
? - 0 or 1 time
* - 0 or more times
+ - 1 or more times
{n} - exactly n times
{n, } - minimum n times
{n, m} - minimum n times and maximum m times
------------------------------------------------------------------------------------------------------------
2. If one tries to use group/start/end method of Matcher class and the searched string is not found, the IllegalStateException is thrown, e.g.:
Pattern p = Pattern.compile("test");
Matcher m = p.matcher("my text");
m.find();
System.out.println(m.group() + " " + m.start() + " " + m.end());
Keep in mind that you need use find method first and then group/start/stop, eg.
Pattern p = Pattern.compile("test");
Matcher m = p.matcher("test");
System.out.println(m.group() + " " + m.start() + " " + m.end());
In both cases IllegalStateException is trhown.
The proper useage may be as follows:
Pattern p = Pattern.compile("test");
Matcher m = p.matcher("my text");
while(m.find()){
System.out.println(m.group() + " " + m.start() + " " + m.end());
}
[a-f] search for a, b, c, d, e or f
[af] search for a or f
\w search for [a-zA-Z_0-9]
\s search for space
\d search for [0-9]
. search for any character
\. search for dot
? - 0 or 1 time
* - 0 or more times
+ - 1 or more times
{n} - exactly n times
{n, } - minimum n times
{n, m} - minimum n times and maximum m times
------------------------------------------------------------------------------------------------------------
2. If one tries to use group/start/end method of Matcher class and the searched string is not found, the IllegalStateException is thrown, e.g.:
Pattern p = Pattern.compile("test");
Matcher m = p.matcher("my text");
m.find();
System.out.println(m.group() + " " + m.start() + " " + m.end());
Keep in mind that you need use find method first and then group/start/stop, eg.
Pattern p = Pattern.compile("test");
Matcher m = p.matcher("test");
System.out.println(m.group() + " " + m.start() + " " + m.end());
In both cases IllegalStateException is trhown.
The proper useage may be as follows:
Pattern p = Pattern.compile("test");
Matcher m = p.matcher("my text");
while(m.find()){
System.out.println(m.group() + " " + m.start() + " " + m.end());
}
EXCEPTIONS
1. When Exception is thrown, the finally block is executed always before the exception is propagated.
See the following example:
try {
System.out.println("TRY-START");
method_that_throws_runtime_exception();
System.out.println("TRY-STOP");
} finally {
System.out.println("FINALLY");
}
the result is: TRY-START FINALLY and then the information about the runtime exception.
------------------------------------------------------------------------------------------------------------
2. One more thing regarding above scenario. Please, look at this example:
try {
System.out.println("TRY-START");
method_that_throws_runtime_exception(); /e.g. NullPointerException
System.out.println("TRY-STOP");
} finally {
System.out.println("FINALLY");
throws new Exception();
}
The propagated exception is Exception not NullPointerException!
And the result is: TRY-START FINALLY information about Exception.
When the catch block handles exception:
try {
System.out.println("TRY-START");
method_that_throws_runtime_exception(); /e.g. NullPointerException
System.out.println("TRY-STOP");
} catch (Exception e) {
throws new Exception();
} finally {
System.out.println("FINALLY");
}
The propagated exception is Exception not NullPointerException!
And the result is: TRY-START FINALLY information about Exception.
------------------------------------------------------------------------------------------------------------
3. It is correct:
void method() throws FileNotFoundException {}
but this one is incorrect:
try {
} catch (FileNotFoundexception e) {
}
See the following example:
try {
System.out.println("TRY-START");
method_that_throws_runtime_exception();
System.out.println("TRY-STOP");
} finally {
System.out.println("FINALLY");
}
the result is: TRY-START FINALLY and then the information about the runtime exception.
------------------------------------------------------------------------------------------------------------
2. One more thing regarding above scenario. Please, look at this example:
try {
System.out.println("TRY-START");
method_that_throws_runtime_exception(); /e.g. NullPointerException
System.out.println("TRY-STOP");
} finally {
System.out.println("FINALLY");
throws new Exception();
}
The propagated exception is Exception not NullPointerException!
And the result is: TRY-START FINALLY information about Exception.
When the catch block handles exception:
try {
System.out.println("TRY-START");
method_that_throws_runtime_exception(); /e.g. NullPointerException
System.out.println("TRY-STOP");
} catch (Exception e) {
throws new Exception();
} finally {
System.out.println("FINALLY");
}
The propagated exception is Exception not NullPointerException!
And the result is: TRY-START FINALLY information about Exception.
------------------------------------------------------------------------------------------------------------
3. It is correct:
void method() throws FileNotFoundException {}
but this one is incorrect:
try {
} catch (FileNotFoundexception e) {
}
piątek, 9 września 2011
ASSIGNMENTS
1.
class X {}
class Y{}
X x = new X();
System.out.println((x instanceof Y)); - does not compile. There is no possibility x to be instance of Y class
The instanceof compile time error exists only in case of class.
---------------------------------------------------------------------------------------------------------------
2.
class X {}
interface Y{};
X x = new X();
System.out.println((x instanceof Y)); - compile. It is possible that at some point X can implement Y interface.
Exactly the same behavior (as in 1 and 2) is in case of == operator.
---------------------------------------------------------------------------------------------------------------
3.
int i = 3, n = -4;
System.out.println(-i + " " + -i); it is correct, the output: -3 4
so: -i is the same as: i = -1*i
---------------------------------------------------------------------------------------------------------------
4.
The result of Integer.parseInt("011") is 11 not 9! The reason is that the radix is decimal.
The result of Integer.parseInt("011", 8) is 9.
---------------------------------------------------------------------------------------------------------------
5.
float f = 10; //correct
float f = 10.0 //incorrect, it must be float f = 10.0f
floating point numbers are by default double. If you want to assing double to float the explicit cast is required, e.g.: float f = (float)10.0
---------------------------------------------------------------------------------------------------------------
6. Local variables must be initialized before use, look at the below example:
void method(){
String s;
if (s == null) { //compile time error
s = "s";
}
}
---------------------------------------------------------------------------------------------------------------
7. It is not allowed to compare (using ==) primitive and null;
e.g.
int i = 3;
if (i == null) { /compile-time error
...
}
class X {}
class Y{}
X x = new X();
System.out.println((x instanceof Y)); - does not compile. There is no possibility x to be instance of Y class
The instanceof compile time error exists only in case of class.
---------------------------------------------------------------------------------------------------------------
2.
class X {}
interface Y{};
X x = new X();
System.out.println((x instanceof Y)); - compile. It is possible that at some point X can implement Y interface.
Exactly the same behavior (as in 1 and 2) is in case of == operator.
---------------------------------------------------------------------------------------------------------------
3.
int i = 3, n = -4;
System.out.println(-i + " " + -i); it is correct, the output: -3 4
so: -i is the same as: i = -1*i
---------------------------------------------------------------------------------------------------------------
4.
The result of Integer.parseInt("011") is 11 not 9! The reason is that the radix is decimal.
The result of Integer.parseInt("011", 8) is 9.
---------------------------------------------------------------------------------------------------------------
5.
float f = 10; //correct
float f = 10.0 //incorrect, it must be float f = 10.0f
floating point numbers are by default double. If you want to assing double to float the explicit cast is required, e.g.: float f = (float)10.0
---------------------------------------------------------------------------------------------------------------
6. Local variables must be initialized before use, look at the below example:
void method(){
String s;
if (s == null) { //compile time error
s = "s";
}
}
---------------------------------------------------------------------------------------------------------------
7. It is not allowed to compare (using ==) primitive and null;
e.g.
int i = 3;
if (i == null) { /compile-time error
...
}
środa, 7 września 2011
FLOW CONTROL
switch
1.
legal types, that can be used in switch: byte, short, char, int, enum, Byte, Short, Character, Integer. In case of wrappers - unboxing works.
e.g.
Integer i = 4;
switch(i) {}
2.
Notice the following example:
final int y = 3;
switch (2) {
case y - 1:
System.out.println("OK");
}
It is compiling, unless the y is not final.
3.
case expressions must be final!
static int x = 4;
switch(4) {
case x: {}
}
is incorrect. x should be final int x = 4;/final static int x = 4;
4.
byte b = 3;
switch(b) {
case 128 :{} //incorrect - possible loss of precission
}
byte b = 3;
final int i = 128;
switch(b) {
case i :{} //incorrect - possible loss of precission - if i <=127 && i >=-128 compilation is ok
}
char c = 3;
final int i = Integer.MAX_VALUE;
switch(c) {
case i :{} //incorrect
}
the case values must be within the range of switch type!
2.
Notice the following example:
final int y = 3;
switch (2) {
case y - 1:
System.out.println("OK");
}
It is compiling, unless the y is not final.
3.
case expressions must be final!
static int x = 4;
switch(4) {
case x: {}
}
is incorrect. x should be final int x = 4;/final static int x = 4;
4.
byte b = 3;
switch(b) {
case 128 :{} //incorrect - possible loss of precission
}
byte b = 3;
final int i = 128;
switch(b) {
case i :{} //incorrect - possible loss of precission - if i <=127 && i >=-128 compilation is ok
}
char c = 3;
final int i = Integer.MAX_VALUE;
switch(c) {
case i :{} //incorrect
}
the case values must be within the range of switch type!
poniedziałek, 5 września 2011
FUNDAMENTALS
1.
final int y = 9;
int i = y++;
not compile, cannot change y value.
------------------------------------------------------------------------------------------------------------
2.
float i = 3;
byte b =3;
b+=i;
compile, += performs autocast
float i = 3;
byte b = 3;
b = b+i;
will not compile
------------------------------------------------------------------------------------------------------------
3.
int [] tab = new int['a'];
will compile, chars are implicitly promoted to ints
------------------------------------------------------------------------------------------------------------
4.
private int get() {
return Integer.MAX_VALUE+1; //correct, also for long, float and double
}
private short get() {
return Short.MAX_VALUE+1; //incorrect
}
private byte get() {
return Byte.MAX_VALUE+1; //incorrect
}
final int y = 9;
int i = y++;
not compile, cannot change y value.
------------------------------------------------------------------------------------------------------------
2.
float i = 3;
byte b =3;
b+=i;
compile, += performs autocast
float i = 3;
byte b = 3;
b = b+i;
will not compile
------------------------------------------------------------------------------------------------------------
3.
int [] tab = new int['a'];
will compile, chars are implicitly promoted to ints
------------------------------------------------------------------------------------------------------------
4.
private int get() {
return Integer.MAX_VALUE+1; //correct, also for long, float and double
}
private short get() {
return Short.MAX_VALUE+1; //incorrect
}
private byte get() {
return Byte.MAX_VALUE+1; //incorrect
}
niedziela, 4 września 2011
INTERFACES
· An interface must be declared with the keyword interface
· Interfaces are by default abstract
i.e.:
public interface Comparable
is the same as:
public abstract interface Comparable
· All methods are by default public abstract although it does not have to be mentioned. So, notice the following declarations are equal:
public void method();
public abstract void method();
Because interface methods are abstract, they cannot be marked final, strictfp, native and private.
e.g. private void method() is incorrect.
· All variables are public static final by default
pubic int x = 3;
is the same as:
public final static int x = 3;
Because variables are final you have to declare and initialize them, you can’t have in the
interface something like this:
pubic int x;
ENUMS
- Enum when defined in the java file (outer version):
can be only: public, default or strictfp
cannot be: static, final, private, protected and abstract - Enum defined within other class (inner version):
can be: public, protected, default, private, strictfp and static!!!
cannot be final, abstract - Inner enums are by default static!
class A {
private enum E {}
}
is the same as:
class A {
private static enum E{}
}
- Enums cannot be defined inside methods (there is no inner local method enum like e.g. inner local method class, by the way you cannot declare interfaces within method too)
-----------------------------------------------------------------------------------------------------------
enum ENUM {XX};
System.out.println("XX" == ENUM.XX.name()); //prints true!
Subskrybuj:
Posty (Atom)