Monday, August 20, 2018

How to use wildcard as the type of local variable in Java?

It is said in documentation, that

...wildcard, represents an unknown type. The wildcard can be used ... as the type of a ... local variable

From my point of view, this means I can write something like

int a;
? b;

but I can't.

Is it really possible?

Solved

That's an appallingly worded statement, and I'm surprised to see it in the documentation. You can't use a wildcard as the type of a variable, but you can use a wildcard in the type of a variable.

? a;       // no
List b; // yes

This is the documentation for generics. Java is strongly typed, and will not accept a wilcard as a variable type.

Wilcard will only be accepted in generic types like:

List x = new ArrayList<>() or List x = new ArrayList<>()


Nope, not possible. Java is strongly typed, the ? is used in a variety of cases (mostly to do with generics) when you can't know the type ahead of time.

I would suggest you read the rest of the documentation you posted, it has some excellent examples as to how to use ?.


You're confusing what purpose the unbounded wildcard has. In general, it implies that you either do not know or do not care about the type when dealing with various things, such as collections.

For example, a List simpleList is a list that contains an unknown type.

In general, ? extends Object, so if you ever really think you need ?, you'd want to use Object instead.


No it is not possible, the Java Tutorial reefers that you can define a local variable using a wildcard like this:

List  localVariable;

but not like this:

? localVariable;

A wildcard is used exclusivity in Java only in Generic brackets, replacing a Type Parameter.


Sunday, August 19, 2018

Javascript Canvas Drawing moves out of canvas after translate()

I am having a problem with randomising rectangle locations on the screen. I have a 5 x 5 grid set up with in html and they are all formatted properly. I am then drawing bars into this grid which also works fine. The problem is that when I want to randomly jitter these objects they sometimes move off the canvas (to the left, or to the top only) apparently. I am trying to reposition the canvas via jQuery with no success.

Also, I have the feeling my canvas sizes are wrong but if I set the sizes to 100% in the CSS section, the bars are suddenly very small and I have no idea why.

Here is the interesting piece of the code. the problem is the "jitter" part

function drawStimulus(size, color, orientation, location){  

// size for each box, refers to the first box in the first row
// box size should be around 40 on a 22inch screen in this jsfiddle
var box_size = $("#testbox").height();

var stimulus_size = box_size * size; // size is either 1 (large) or 2/3    
var size_diff = box_size-stimulus_size;
var c = document.getElementById(location);

c.width = box_size;
c.height = box_size;

// if a perfect grid is not wanted, jitter bars randomly
// by a maximum of 1/3 of the box size in any direction
if(jitter){
  var hjitter = rand(-box_size/3, box_size/3);
  var vjitter = rand(-box_size/3, box_size/3);
  c.style.left = String(c.style.left+hjitter)+"px";
  c.style.top = String(c.style.top+vjitter)+"px";  
}

var ctx = c.getContext("2d");
// rotate around center
ctx.translate(box_size/2, box_size/2);
ctx.rotate(orientation*Math.PI / 180);
ctx.translate(-box_size/2, -box_size/2);

// draw bars
ctx.beginPath();
ctx.strokeStyle = color;
ctx.moveTo(c.offsetLeft+size_diff/2,c.offsetTop+box_size/2);
ctx.lineTo(c.offsetLeft+stimulus_size+size_diff/2,c.offsetTop+box_size/2);
ctx.lineWidth = size*10;
ctx.closePath();
ctx.stroke();
}

Here is also a jsfiddle with the full code: http://jsfiddle.net/msauter/pdrwwm7x/

Saturday, August 18, 2018

How to set generic type at runtime in java

I saw some questions like this but different from my case.I want to set generated class at build time as generic type at runtime. here is code.

  //employee class is generated at build time 
  void function(Class employee)
  {  
      // I want to set class as generic type like this 
     List emps= new ArrayList(employee);
   }

how to do this?

Solved

You can't.

Generics are all mapped to java.lang.Object on compilation by a process called type erasure.

You can only build things that are checked at compile time: consider using Class notation.