• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help regarding java7 feature

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Feature additions for Java 7 include:[63]

1)JVM support for dynamic languages, following the prototyping work currently done on the Multi Language Virtual Machine
2)Compressed 64-bit pointers[64] Available in Java 6 with -XX:+UseCompressedOops
3)Small language changes (grouped under a project named Coin)[65]:
3.1)The ability to cast from an Object type to a primitive type directly
3.2)Strings in switch[66]
3.3)Automatic resource management in try-statement[67]
3.4)Improved type inference for generic instance creation[68]
3.5)Simplified varargs method declaration[69]
3.6)Binary integer literals[70]
3.7)Allowing underscores in numeric literals[71]
3.8)Catching multiple exception types and rethrowing exceptions with improved type checking[72]


What is "3.1)The ability to cast from an Object type to a primitive type directly".? It will be very helpful to explain this feature.Thanks in advance.

Source:http://en.wikipedia.org/wiki/Java_version_history#Java_SE_7_.28July_28.2C_2011.29
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried below on Java 6 and it worked fine-



and the feature you mention is missing from Oracle's list of enhancements.
A link to which is present on the same wiki page you pointed to.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was already allowed since Java 5.0.

The following is an extension that shows this feature:
A Java 6 compiler will not allow line 12, whereas a Java 7 compiler will.
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.
If I get it right now I can cast any object to a primitive.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Thank you very much.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aniruddh Joshi wrote:Thanks Rob.
If I get it right now I can cast any object to a primitive.

Only if the object is a primitive wrapper of the right type (Integer + int, Long + long, etc). Otherwise the compiler won't complain but you'll get a ClassCastException. That's because line 12 is actually a cast to Integer, then auto unboxing:
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: Otherwise the compiler won't complain but you'll get a ClassCastException.

Of course.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic