Answer for question is no.
Gc behaves nearly same for both cases.
Garbage Collector has a unpredictable behavior. But
Any Object which is no longer referred or is no longer in use is eligible for garbage collection.
Case 1 : Main objective of anonymous object is for instant use (one time use). So after line "new Student().setName("john");" , your anonymous object is not in use so it will be GC.
case 2 : Student student = new Student();
student.setName("john");
After this line student reference is no longer referred so it will be GC.
There are few chances that in case 2 student reference may be leaked but GC is smart enough to handle this.
Now in case 1 if you want object for one time use then go for anonymous object as Objects are created in heap memory and GC sweep heap memory. Stack memory are managed in such way that memory used by stack is reclaimed automatically.
You can referred this link for more.