a peek into my MIND

April 2, 2014

2.Literals

Filed under: Java7Spec — Bharat Kondeti @ 12:07 am

<< PREVIOUS

A literal is a representation of a value of primitive type, string type or a literal type. A primitive literal can be a Integer literal / Float literal / Boolean literal / Character literal

– Integer literal can be decimal, hexadecimal, octal and binary
– Integer literal is of type long if it is suffixed with an ASCII letter L or l
– Underscores can be used as separators between integers
– Hexadecimal numerical starts with 0x or 0X followed by ASCII hexadecimal digits (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,A,B,C,D,E,F)
– Octa numerical starts with 0 followed by ASCII digits 0 through 7
– Binary numerical starts with 0b or 0B followed by ASCII digits 0 or 1

// Decimal with underscore
 int decimal = 10_000_000;

// Hexa decimal
 int hexadecimal = 0X989_680;

// Octa decimal
 int octadecimal = 046113200;

// Binary
 int binary = 0B100110001001011010000000;

– Float literal can be decimal and hexadecimal
– Float literal is of type float if it is suffixed with an ASCII letter F or f
– By default, a Float literal is of type double and optionally it can be suffixed with an ASCII letter D or d

– Boolean literal can be true or false

– Character literal is represented as a character or a escape sequence enclosed in a ASCII single quotes
– Character literal can only represent UTF-16 code’s whose values are in between ‘\u0000’ to ‘\uffff’

– A String literal is a reference to an instance of String class
– A string literal always refers to the same instance of the String class
– String literals defined with in the same class / defined with in different classes of the same package / defined with in different classes of different packages all reference to the same String object instance

public class Sample {
    public static void main(String[] args) {
        String ss1 = "samplestring";
        String ss2 = "samplestring";

        System.out.println( ss1==ss2); //true
        System.out.println( ss1=="samplestring"); //true
        System.out.println( Sample2.ss=="samplestring"); //true
    }
}
class Sample2 {
    public static String ss = "samplestring";
}

– String literals computed by constant expressions are computed at compile time and thus refer to same String instance

String ss = "samplestring";
System.out.println(ss=="sample" + "string"); //true

– String literals computed at runtime by concatenation are newly created and thus reference to different String instance

String ss = "samplestring";
String sample = "sample";
String str = "string";

System.out.println(ss == sample + "string"); //false
System.out.println(ss == sample + str); //false

– intern() method on the computed string will refer to the same String object reference to a literal, if that literal instance is already created

String ss = "samplestring";
String sample = "sample";
String str = "string";

System.out.println(ss == (sample + "string").intern()); //true
System.out.println(ss == (sample + str).intern()); //true

– A null type has a value of null reference that is represented by Null literal

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.

>> NEXT

0. Java 7 Specification And A Goal

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

Since it is never too late to have goals, I decided to have one, which is to read Java 7 Specification and blog about things that I did not know or thought were interesting. Also my hope is writing about these would help me to remember and get better understanding of some concepts.

Plan is to finish reading the specification in next 6 months… Hope I succeed….

Create a free website or blog at WordPress.com.