a peek into my MIND

March 3, 2014

1. Keywords

Filed under: Java7Spec — Bharat Kondeti @ 6:01 am

Every language has some reserved words that cannot be used as identifiers called Keywords. Java has 57 such keywords. To get complete list go here – Java Keywords.

Few interesting things:

const and goto are reserved keywords even though they are not used by the language.

– theory around volatile is bit complex. First we need to know that threads can cache data in processor cache memory for performance reasons rather than reading from / writing to main memory every time. In a single-core / multi-core processor, if multiple threads are working on the same variable with each thread using a different cache, they might not see the changes by other threads and will result in race conditions. To avoid this, if a variable is declared as volatile then threads will NOT cache that variable and is always read from / updated to the main memory.

There is one more concept called crossing the memory barrier. This means that when a memory barrier is crossed, JVM syncs all cached copies of variables to main memory and invalidates any cache that is marked as dirty in main memory. This is important because when a volatile variable is accessed, all the other variables that are cached are synced to main memory first. From Java 5 onwards, accessing a volatile variable will result in crossing a memory barrier.

Fixing the Java Memory Model – Part 1

Fixing the Java Memory Model – Part 2

– there is a keyword called strictfp as one of the reserved words. For performance reasons JVM can offload floating point calculations to the platform its running on. Since different platforms use different precisions for floating points, we would get different results when calculating floating points on these platforms. When this keyword is applied to a class, all the calculations inside the class will use strict floating point math by java runtime, resulting in same value on all the platforms. This keyword can be applied to a class, interface or a non abstract method.



Leave a Comment »

No comments yet.

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: