JAVA

Features of Java
Platform Independence
The Write-Once-Run-Anywhere ideal has not been achieved (tuning for different platforms usually required), but closer than with other languages.
Object Oriented
Object oriented throughout - no coding outside of class definitions, including main().
An extensive class library available in the core language packages.
Compiler/Interpreter Combo
Code is compiled to bytecodes that are interpreted by a Java virtual machines (JVM) .
This provides portability to any machine for which a virtual machine has been written.
The two steps of compilation and interpretation allow for extensive code checking and improved security.
Robust
Exception handling built-in, strong type checking (that is, all data must be declared an explicit type), local variables must be initialized.
Several dangerous features of C & C++ eliminated:
No memory pointers 
No preprocessor
Array index limit checking
Automatic Memory Management
Automatic garbage collection - memory management handled by JVM.
Security
No memory pointers
Programs runs inside the virtual machine sandbox.
Array index limit checking
Dynamic Binding
The linking of data and methods to where they are located, is done at run-time.
New classes can be loaded while a program is running. Linking is done on the fly.
Even if libraries are recompiled, there is no need to recompile code that uses classes in those libraries.
Good Performance
Interpretation of bytecodes slowed performance in early versions, but advanced virtual machines with adaptive and just-in-time compilation and other techniques now typically provide performance up to 50% to 100% the speed of C++ programs.
Threading
Lightweight processes, called threads, can easily be spun off to perform multiprocessing.
Can take advantage of multiprocessors where available
Great for multimedia displays.
Built-in Networking
Java was designed with networking in mind and comes with many classes to develop sophisticated Internet communications. 
Range of primitive data types in Java 

Type
Size
Range
byte
8 bits
-128 .. 127
short
16 bits
-32,768 .. 32,767
int
32 bits
-2,147,483,648 .. 2,147,483,647
long
64 bits
-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
float
32 bits
3.40282347 x 1038, 1.40239846 x 10-45
double
64 bits
1.7976931348623157 x 10308, 4.9406564584124654 x 10-324



Java String

String is one of the widely used java classes. It is special in java as it has some special characteristics than a usual java class.
Java String is a immutable object. For an immutable object you cannot modify any of its attribute’s values. Once you have created a java String object it cannot be modified to some other object or a different String.  References to a java String instance are mutable. There are multiple ways to make an object immutable.
Why java String is immutable?
Main reason behind it is for better performance. Creating a copy of existing java String is easier as there is no need to create a new instance but can be easily created by pointing to already existing String. This saves valuable primary memory. Using String as a key for Hash table guarantees that there will be no need for re hashing because of object change. Using java String in multithreaded environment is safe by itself and we need not take any precautionary measures.
Java String Instantiation

JVM maintains a memory pool for String. When you create a String, first this memory pool is scanned. If the instance already exists then this new instance is mapped to the already existing instance. If not, a new java String instance is created in the memory pool.
This approach of creating a java String instance is in sync with the immutable property. When you use ‘new’ to instantiate a String, you will force JVM to store this new instance is fresh memory location thus bypassing the memory map scan. Inside a String class what you have got is a char array which holds the characters in the String you create.
Following are some of the ways to instantiate a java String :
String s1 = "iByteCode";
How this works?
  • JVM checks the String constant pool first and if the string does not exist, it creates a new String object “iByteCode” and a reference is maintained in the pool. The variable ‘s1 also refers the same object.
  • This statement creates one String object “iByteCode”.
String s = new String("iByteCode");
How this works?
In this case, because we used ‘new’ keyword a String object is created in the heap memory even if an equal string object already exists in the pool and ‘s’ will refer to the newly created one.
Java String Comparison
Do not use == operator to compare java String. It compares only the object references and not its contents. If you say, “if references are same, then the value must be same” – this doesn’t cover, “even if references are not same, the content can be same”. Therefore == operator does not 100% guarantee the equality of java String. Consider,
String stringArray1 = new String(“apple”, “orange”);



“apple” == stringArray1[0]; gives FALSE.
  • You claim that, using == operator with intern will give right result but it is not necessary.
  • For equality comparison in java String, simplest and easiest way is to go with equals() method.
“apple”.equals(stringArray1[0]); gives TRUE.
equals() method is part of Object class. Java String class overrides it and provides implementation for equality comparison. String’s equals() implemetation uses three step process to compare two java String:
1.     Compare references (if both String references are same return true else continue)
2.     Compare length (if both String length are not same return false else continue)
3.     Compare character by character sequentially.
Use equalsIgnoreCase(String) to compare String irrespective of case (Case insensitivity).
“javaString”.equalsIgnoreCase(“JAVASTRING”); returns TRUE.

Java Access Modifiers

Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:
·         Default - Visible to the package. No modifiers are needed.
·         Private - Visible to the class only
·         Public - Visible to the world.
·         Protected - Visible to the package and all subclasses.


Access Modifier
Same Class
Same Package
Subclass
Other Packages
Public
Y
Y
Y
Y
Protected
Y
Y
Y
N
Default
Y
Y
N
N
Private
Y
N
N
N






No comments:

Post a Comment