• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help regarding java7 feature

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Foreman
Posts: 275
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Foreman
Posts: 275
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob Thank you very much.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Foreman
Posts: 275
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
reply
    Bookmark Topic Watch Topic
  • New Topic