0

so in this code as the commments indicate two confusing cases appear.where putting generics before constructor name and after it gives different results.

class moke<T> {
    T ob;
     moke(T ob){
      this.ob=ob;
    }
    T ret(){
        return ob;
      }
    }
    class ramirez{
       public static void main(String args[]){
        moke<Integer> one= new moke<Integer>(9);// it's ok
        moke<String> two=new moke<Integer>("ASDASD"); //  error appears here which is ok
        moke<String> three= new <Integer>moke("knmkm"); //no error here WHY??
        moke<String> four=new <String>moke(9);  //No error hereWHY??


     }}

so what is the difference between <Type>constructor() and constructor<Type>()

podrimqakuf
  • 17
  • 1
  • 7
  • 1
    Possible duplicate of [Syntax for creating a two-dimensional array](http://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array) – luk2302 Jan 20 '17 at 21:26
  • 1
    One question per post please (e.g. for the reason to close one as duplicate and answer the second one or even close both separately) – luk2302 Jan 20 '17 at 21:29
  • Possible duplicate of [Why does giving explicit type arguments to a non-generic method or constructor compile?](http://stackoverflow.com/questions/22174022/why-does-giving-explicit-type-arguments-to-a-non-generic-method-or-constructor-c) – Calculator Jan 20 '17 at 22:08

1 Answers1

0

Answer to first question:

// here {1,2,3} is an array initializer you may use to define the array
int[] arr = {1,2,3}; 

// this does not work, because this is an assignment expression and not a definition of the array
arr = {1,2,3}; 

To make this clearer, just imagine arr is an array with a (possibly different) size determined at runtime. How would this assignment work?

For the second question I have no answer yet. Looks strange. But please create a new post for each question on stackoverflow.

UniversE
  • 2,419
  • 17
  • 24