Jul 28

Especially for certification it is necessary to distinguish if a warning, a CompilerError or a RuntimeError will appear. Therefore here a short comprehension:

CompilerErrors appear within others while explizit assignment of not allowed values to generics

warnings appear at mixture of (non-)generics in order to give the programmer the possibility to fix the code if necessary.

RuntimeError appear during runtime of the program, eg in case if processing should be continued with a unexpected type

Tagged with:
Jul 25

we know now that everything which is in context of mixing (non-)generics will be running as long as it is (although with warning) compilable. Under this circumstances also not really allowed object types can be added to the collection, but how does it work in the further context of the program? Although the object can be added it is still a String and not an Integer. Therefore the problems are foreseeable:

List <Integer> a= new ArrayList<Integer>();

a.add(1);
a.add(2);
a.add(3);

Insert e = new Insert();
e.add(a);
// adding String without problems
e.add(a); // adding another String without problems

a.remove(4);
// deletion of a a String is also no problem

for (int x= 0; x<a.size(); x++)

System.out.print(a.get(x)+” “); // output is also not a problem

int z=a.get(3); // assignment is a problem since a String can not be assigned to an int (ClassCastException during runtime)

Tagged with:
Jul 22

We already learned that during compilation a mixture of (non-)generic Collections is working – also for originally not allowed data-types. The next might be now: Is it running without problems?

The answer is yes.

Every generic possibility is checked during compilation. In case a compiler sees some problems, it is giving a warning. During runtime a generic collections is threated as a non-generic collection – JVM does not see any difference.

To make the point: Everything which can be compiled (with warning) can also run.

Tagged with:
Jul 19

As already explained mixing (non-)generics Collections is not a problem at all as long as we are dealing the same object-type, but what happens when we are dealing with different objects while mixing (non-)generic collections?

Let’s take our last example and change method

void add (List a){

a.add(5);

}

to

void add (List a){

a.add(“5″);

}

Now instead of the allowed Integer-Value a String is added. After all we have learned it should not work, what do you think?

Surprisingly the Compiler is making an exception; compilation and run are working without problems, only a warning is given.

So why is it so? The reason lies in the history of generics; since they came only with version 5 the code written in previous versions needed to be integrated. Therefore a mixture was made possible and tagged with a warning in order to give the programmer the possibility to decide if and how he wants to change the code.

Tagged with:
Jul 16

Let’s start with an quite simple example of mixing generics with non-generics collections. It is not a problem as long as the data types are equal:

 

class Employee{

public static void main (String … args){

List <Integer> a = new ArrayList <Integer>();

a.add(1);

a.add(2);

a.add(3);


Insert e = new Insert();

e.add(a);

for (int x= 0; x<a.size();x++)

System.out.print(a.get(x)+” “);

}

}

class Insert {

void add (List a){

a.add(5);

}

}

Although method Insert does not know that it is working on an generic collection it is adding the value without problems. Only a warning is returned that (non-)generics are mixed.

Tagged with:
Jul 13

First of all we should define what are (non-)generics. Let’s take for example ArrayList; until java5 we could declare ArrayList only in the following way:

ArrayList a = new ArrayList();

respectively the polymorphic way:

List a = new ArrayList();

In this list simply everything can be inserted what is known as an object, eg. String, Integer, Date, and so on. A comparison is quite difficult, sometimes even impossible. In case a return of a String was necessary it needed to be casted with

String b = (String) a.get(0);

since a cast has it’s boundaries it could still give an runtime-error

In order to ensure that as in our example only a particular object-value is inserted and also returned generics are created:

ArrayList <String> a = new ArrayList <String>();

respectively the polymorphic way:

List <String> a = new ArrayList<String>();

as a result from this

a.add(new Date());

would result at generics in an CompilerError, but run successful at non-generics.

Tagged with:
Jul 10

As already explained PriorityQueue can have other priorities than the natural order. Therefore (as for other Collections) Comparable or Comparator is used:

class ObjSort implements Comparator <Integer>{

public int compare(Integer a,Integer b)

return (b.compareTo(a));

}

This will be used while implementing PriorityQueue:

PriorityQueue <Integer> a = new PriorityQueue <Integer>(100, new ObjSort());

In case we are assigning the following values:

a.offer(3);
a.offer(7);
a.offer(1);
a.offer(5);
a.offer(9);

the output from:

int b=a.size();

for (int i =0; i<b; i++)

System.out.println(a.peek() +” ” +a.size()); //Output of the highest Element without deletion

is:

9 5
9 5
9 5
9 5
9 5

and the output from

for (int i =0; i<b; i++)

System.out.println(a.poll() +” ” +a.size()); // Output and deletion of the highest element

is:

9 4
7 3
5 2
3 1
1 0


Tagged with:
Jul 07

Let’s have a look what happens in case another than the proof-value of an instance (and therefore the instance itself) is changed:

 

import java.util.Map;

import java. util.HashMap;

class Employee{

Employee(int PersNr, String department) {

this.PersNr=PersNr;

this.department=department;

}

public int PersNr;

public String department;

public static void main(String[] args) {

Map < Employee, Integer> x = new HashMap < Employee, Integer>();


Employee a = new  Employee (1, “department A”);

Employee b = new Employee(2, “department B”);

Employee  c = new  Employee (3, “department C”);


x.put(a,a. PersNr);

x.put(b, b.PersNr);

x.put(c, c.PersNr);


a.department=”new”;


System.out.println(x.get(a));

System.out.println(x.get(b));

System.out.println(x.get(c));

}

public int hashCode(){ return PersNr*10; }

public boolean equals( Employee c){

if (c.PersNr==this.PersNr){ return true; }

else{ return false; }

}

}

Here the result of System.out.println(x.get(a)); remains 1; since hashCode() and equals(Obj) implemented another value for proof and return. This means the only value we really have to take care of is the one for comparison. In case of change this value will turn to null. In case another value than the comparison value is changed the compiler does not care.

Tagged with:
Jul 04

I already explained some methods of map:

a.put(“Meier”, 12345); // Hinzufügen des Keys “Meier” mit dem Wert 12345
a.get(“Meier”); // returning the corresponding value to “Meier” -> 12345
a.size(); // map-size; here: 1
a.remove(“Meier”); // deletion of the corresponding value to “Meier”

Today I will give some hints in relation to maps. Let’s have a look on the following example:

import java.util.Map;
import java.util.HashMap;

class Employee{

Employee(int PersNr) {

this.PersNr=PersNr;

}

public int PersNr;

public static void main(String[] args) {

Map <Employee, Integer> x = new HashMap <Employee, Integer>();

Employee a = new Employee (1);

Employee b = new Employee (2);

Employee c = new Employee (3);


x.put(a,1);

x.put(b, b.PersNr);

x.put(c, c.PersNr);

a.PersNr=5;


System.out.println(x.get(a));

System.out.println(x.get(b));

System.out.println(x.get(c));

}

public int hashCode(){

return PersNr*10;

}

public boolean equals(Employee c){

if (c.PersNr==this.PersNr){

return true;

}

else{

return false;

}

}

}

So it seems everything is as already known; hashCode() and equals(Obj) are overwritten, only the subsequent change of a.PersNr is uncommon. We are confusing the JVM a little bit since the key (on whose base we are comparing) has changed. Since this key is not known to the map null will be returned.

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs