What is strictfp keyword ?

strictfp is a keyword in the Java programming language that restricts floating-point calculations to ensure portability. The strictfp command was introduced into Java with the Java virtual machine (JVM) version 1.2 and is available for use on all currently updated Java VMs.

The IEEE standard IEEE 754 specifies a standard method for both floating-point calculations and storage of floating-point values in either single (32-bit, used in Java floats) or double (64-bit, used in Java doubles) precision.

Prior to JVM 1.2, floating-point calculations were strict; that is, all intermediate floating-point results were represented as IEEE single or double precisions only. As a consequence, errors of calculation (round-off errors), overflows and underflows, would occur with greater frequency than in architectures which did intermediate calculations in greater precision. The arithmetic issues were a real problem on early Java VMs, and many other solutions besides the use of this instruction were proposed.

Since JVM 1.2, intermediate computations are not limited to the standard 32 bit and 64 bit precisions. On platforms that can handle other representations e.g. 80-bit double extended on x86 or x86-64 platforms, those representations can be used, helping to prevent round-off errors and overflows, thereby increasing precision.

strictfp ensures that we get exactly the same results from our floating point calculations on every platform. If you do not use strictfp, the JVM implementation is free to use extra precision where available.


What is System.out in Java ?

System is a final class from java.lang package. It contains pre-defined methods and fields, which provides facilities like standard input, output, etc.

out is a static final field (ie, variable) in System class which is of the type PrintStream (a built-in class, contains methods to print the different data values). So out is the reference of PrintStream class and a static member of System class.

What is Java static import ?

The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:

import static java.lang.Math.PI;


import static java.lang.Math.*;

Once the static members have been imported, they may be used without qualification:

double r = cos(PI * theta);

The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.

So when should you use static import? Very sparingly! Only use it when you’d otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

For example,

package roytuts.com.staticimport;
import static java.lang.System.out;
public class StaticImport {
    public static void main(String[] args) {

When to use String and StringBuffer ?

String is used to manipulate character strings that cannot be changed (read-only and immutable). If your string is not going to change use a String class because a String object is immutable.

StringBuffer is used to represent characters that can be modified. If your string can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you can have thread-safety.

Performance wise, StringBuffer is faster when performing concatenations. This is because when you concatenate a String, you are creating a new object (internally) every time since String is immutable.

What is difference between StringBuffer and StringBuilder ?

The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.

If your string can change (logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough.

If your string can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you have thread-safety.

What is wrapper class in java ?

The primitive data types are not objects; so they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data type into an object in Java language. For example, up to JDK 1.4, the data structure accepts only objects to store. A data type needs to be converted into an object and then added to a Collection. For this conversion, the concept of wrapper class came into an existence. As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type.

For example,

int x = 50;
Integer i = new Integer(x);

The int data type x is converted into an object to a variable i using Integer class. The i object can be used in Java programming wherever x is required an object.

The following code can be used to unwrap (getting int back from Integer object) the object i.

int y = i.intValue();
System.out.println(y); // prints 50

intValue() is a method of Integer class that returns an int value.

What is Java classpath ?

Classpath is an environment variable of system. The setting of this variable is used to provide the root of any package hierarchy to java compiler.

Classpath is a parameter—set either on the command-line, or through an environment variable — that tells the Java Virtual Machine or the Java compiler where to look for user-defined classes and packages.

Can a class in Java be private ?

Private outer class would be useless as nothing can access it. So we cannot declare top level class as private; only public, abstract and final are permitted. we can declare inner classes as private. If we declare any method and variable in private class then we can access those method and variable within that scope directly.

Is null a keyword in java ?

null is a literal, in the same sense that false, 10, and ‘\n’ are literals. It is not a “keyword”, technically, but it is a character string that is treated specially by the compiler if the compiler encounters it in a Java source file. So, you cannot name a variable “null”. The lexical analyzer will decide that it is not an identifier. null is the value of reference variable.