This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes help regarding java7 feature Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help regarding java7 feature" Watch "help regarding java7 feature" New topic
Author

help regarding java7 feature

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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


Tell the difficulties that i am difficult.
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

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.


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Thanks Rob.
If I get it right now I can cast any object to a primitive.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Rob Thank you very much.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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

Joined: Jul 29, 2008
Posts: 275

Rob Spoor wrote: Otherwise the compiler won't complain but you'll get a ClassCastException.

Of course.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help regarding java7 feature