learningbydeveloping.com

 

Learning by developing

...because practice teaches you

PDF Version: scjp T-1

Variable identifiers – Can start with: Currency letter, connecting characters(Underscore “_”), but NOT with numbers. Note: cannot start with #.

Private variables and private methods:Can only be accessed from inside the class.

Final method: Cannot be overrided(if it is not private). It assures the reference variable so that it cannot be referred to another object BUT it doesn’t prevent the object’s state to change and so attributes from that object can be modified/changed.

Classes: There can be only ONE public class per code file and the file name should be the same as the public class(if there’s no public class, the file can have any name)..

Final class: Cannot be extended or inherited(E.g.: String class).

Abstract class:An abstract class cannot be instanciated(But can be compiled and executed). They don’t have to have all the methods abstract, could have some of the implemented and some abstract as well. So even if they implement an interface, they do not need to implement the methods(at that point).

Note: one class can be abstract and final,but it means that the class won’t be usable since it won’t be possible to inherit and so implement the missing methods(abstract requires this) because it’s “final”.

Default(package) class: Can only be accessed from inside its package(cannot be accessed/extended from outside the package ). Note: A package contains classes(which first one MUST be public or compiler error) and comments can appear anywhere in a file(even before the package).

Interfaces: They can neither have static methods nor final, strictfp(float point strict floag-double) or native(thus they can only have public/abstract modifiers). They can extend 1 or more interfaces. In addition an interface could be abstract but it is redundant. An interface can have constant variables defined(if the value changes, compiling error) and any variable declared in an interface is automatically declared as “public static final int x” even if you declared it with “int x”.Its methods finish with “;” and not with “{}”.

Note: If we have “Interface A{}” and then inside another method-class “A a = new A(){blah blah}”, this is correct, since this will be an anonymous class and not an instance of the interface “A”.

Protected methods: Accessible just from inside the class itself and subclasses(inherits from that class) even of those subclasses are in different packages. So a protected class cannot be accessed from other packages, but its members can be inherited(and accessed) by subclasses that are located in different packages(so A can extend B(public in another package) which has a protected attribute X and so A, since it is a subclass of B, can access ‘X’from a different package, but not any other class).

Default methods: The same as the protected methods but can only be accessed inside the same package.

Native methods: implemented in platform-dependent code. E.g.: ‘C’code.

Strictpf methods: Reserved word that can modify clases/methods(not variables) and which means that all the methods acomplish the IEEE 754 floating point standard.

Note: In methods with variable argument lists(var “args” like “main”), the variable arguments MUST be placed at the end of the list. In the case of 2 possible methods to choose by 1 invocation, the methods with var-args would be selected in the last case. E.g. Void doStuff(int… x)//expects ‘0…many’ints, void doStuff(char a, int…x) //expects 1 char + 1-many int.

IMPORTANT: You can not use two var-args in one method:void blah(char…a, int… x) is NOT legal.

If you put method(A[]… a) and method(Object a), the method with ‘Object A’will take the array first,since it is more concrete and an array is an object.

Note: Widening(If you have a method with args: “Integer I”(wrapper) and another with “long i”,the compiler would chose “long i” because it’s “wider”) ← this means Widening >Boxing

This means:

Widening(“long i”) >Boxing(”Integer i”) >Var-args(”int… i”)

Note: You cannot wide from one wrapper to another, so Integer IS NOT A Long .

Access to class members
Visibility Public Protected Default Private
Same class Yes Yes Yes Yes
Any class in same package Yes Yes Yes No
Subclass in same package Yes Yes Yes No
Subclass outside the package Yes Yes, through inheritance No No
Any non-subclass outside the package Yes No No No

 

Ranges – numeric primitives
Type Bits Bytes Min-

Range

Max-

Range

byte 8 1 2^7 2^7-1
short 16 2 -2^15 2^15-1
int 32 4 -2^31 -2^31-1
long 64 8 -2^63 2^63-1
float 32 4 N/A N/A
double 64 8 N/A N/A

 

 

Constructors: Can be private/public/nothing, but they DON’T have return value(not even ‘void’). NOTE: private constructor() does not allow to create a new() object.

Comparisson operators(overloaded/not) with wrappers:

Local variables Variables (non-local) Methods
Final Final

public

protected

private

static

transient

volatile

Final

public

protected

private

static

 

abstract

synchronized

strictfp

native

Legal overridden method access

  • A default method may be overridden by a default, protected, or public method.

  • A protected method may be overridden by a protected or public method.

  • A public method may be overridden only by a public method.

Legal overridden method access (Order)

Can be less restrictive: default ->protected ->public (less restrictive)

Exceptions: When checking overriden methods, you cannot throw wider exceptions or add new exceptions(None–>Exception), but can throw subclasses of the exceptions thrown by the overriden method in the new one.

Note: you can ADD runtime exceptions. But if there is already one RTE, you can only set a subclass or remove it(not wider exception).

Extends: You can only extend ONE class, not two.

Implementing two interfaces: DOUSE comas!

—————————-

Correct: class F implements i1, i2 { }

Not correct: class B implements i1 implements i2 { }

Extending and implementing: Do NOT use comas!

Correct: class A extends c2 implements i1 { }

Not correct: class D extends c1, implements i2 { }

A,C exist in Package(A)

B extends A exist in Package(A)

D,E exist in Package(B)

F extends A exist in Package(B)

IMPORTANT: B can extend A, only if A is NOT private. If A is private, it cannot be extended. Only public classes can be accessed from other packages(even privates cannot be accessed)

So:

A,B,C can access all the members of A(methods/attributes).

F extends A can access, through the other package, the members of A through inheritance(note it cannot inherit if A is not private/default).

Array: An array is an object by itself(it does not have to be an array of objects even if an array represents multiple instances of one class). The objects might contain, as usual, themselves or any subclass of itself(downcasting).Ways of declaring arrays:

int[][][] arC, int key[].

Note: int [] x [5] <– incorrect, you cannot give the size if you don’t instanciate(new).

Initializing an array:

int x[] = new int[5];

x[0] = 1; x[1]=2; …

int x[] = new int[]{1, xValue, 3, 46}; //xValue = int variable

int[][] x = new int[5][]; //we can leave the second array part without initialize

Note: Multidimensional arrays CAN be inconsistant, let’s say:

int x[][] = {{1}, {1,2},{1,2,3}};

Instance variables: Non-static variables inside the class so that blongs to the object.

Transient variables: They are not serialized. It can only be applied to instance variables.

Volatile variables: Can change per threads. It can only be applied to instance variables.

Enums: Enums define predefined values(which you can iterate with the default method “.values()” <– IMPORTANT METHOD!!!), which can not be private/protected and which can have the semicolon ‘;’ in their names. Enums implement by default serializable and comparable and can implement any other interface. Enums cannot be declared in methods, but in classes. Enums can be declared as:

Important: When an enum is created, all the enums are created at the same time(so you would get the same instance if you get its same name several times). This means the constructor is called for every enum value when created the first one.

public enum Color {

WHITE, BLACK, RED, YELLOW, BLUE; //; is optional

}

public enum Color {

WHITE(21), BLACK(22), RED(23), YELLOW(24), BLUE(25);

private int code;

private Color(int c) {

code = c;

}

public int getCode() {

return code;

}

So we could declare a variable like:

Color c = Color.WHITE;

c.getCode(); //Value = 21

public enum Colorimplements Runnable {

WHITE, BLACK, RED, YELLOW, BLUE;

public void run() {

System.out.println(“name()=”+ name() +

“, toString()=”+ toString());

}

}

An enum can implement any interface(like runnable).E.g.: Using runnable

for(Colorc : Color.values()) {

c.run();

}

or

for(Runnable r : Color.values()) {

r.run();

}

Declaring a method abstract and overriding in the enum:

public enum Operation {

PLUS { double eval(double x, double y) { return x + y; } },

MINUS { double eval(double x, double y) { return x – y; } },

TIMES { double eval(double x, double y) { return x * y; } },

DIVIDE { double eval(double x, double y) { return x / y; } };

abstract double eval(double x, double y);

}

An “enum coffeeSize{BIG, HUGE{ //Definition of a method for HUGE type which overrides the same method for the other. E.g.: BIG/OVERW has one identifier but HUGE has another.}, OVERW};” can be accessed/referenced like CoffeeSize cs = CoffeeSize.BIG

They can be used in a class that contains enums. E.g.: 1. Coffee dunk1 = new Coffee();

2. dunk1.size = CoffeeSize.BIG;

Method: static “values()‘: returns the array of enum values(Strings).

Note: Enums cannot be instanciated using new() operator.

Note2: they can have consutrctors and variables(which, if one is assigned to other variables of the enum type, and compared with “==” would have the same reference).

Note3: they cannot be declared within a method.

Note4: Enum can be instanciated without being static and being used inside an static method.

Note: To access a method of a Enum(which is by default static), you need to access one of its members. E.g.: int h0 = Phantasy.racesToChoose.HALFLING.getWeight ();

public class PhantasyWorld {

public static void main(String[] args) {

// insert code here

}

}

class Phantasy {

enum racesToChoose {

HALFLING(38), ELVES(79), DWARVES(55);

int height;

racesToChoose(int h) { weight = h; }

int getWeight() { return weight; }

}

}

JavaBeans standards:

Attributes can have methods: getAttribute/isAttribute(boolean), setAttribute.

To add a listener or remove: addXListener/removeXListener(should finish with “Listener” and get a listener of the same type).

Variable values.

Short,char,float: 16 bits

int: 24 bits

long, double: 32 bits

char values:[ 0 – 2^16]

numbers: [-2^(N-1) – 2^(N-1)-1 ]

Shadowing(local variables with same name as class-global variables): From inside a method, we can declare int x = this.x; and “this.x” would refer to the global variable, while when saying just “x” will refer to the local variable.

Final variables: Once the variable is initialized it cannot be modified(a primitive will never change while an object can change its status(with setAttribute methods…) but not the reference to the object the variable its pointing to).

IMPORTANT: Main method(or any other STATIC) method CANNOT call a NON-STATIC method. Take into account that a static method is a class method, so there might be that there is no other object(which is the way to access a non-static method) and so it’s not possible to call it.


**********************************************

The books I have used are these ones, which I strongly recommend. You can support this blog buying them through this links:

 

and

 

For OCJP version 7, I would recommend:

 

 

Categories: Java

Leave a Reply