300x250 AD TOP

Thursday, September 25, 2014

Tagged under: , ,

Autoboxing and Unboxing in Java



Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. If the conversion goes the other way, this is called unboxing


Autoboxing

Character ch = 'a';
List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(i); //it creates an Integer object from i and adds the object to li.
 ******************
List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(Integer.valueOf(i)); // compiler converts the previous code to the following at runtime
unboxing
Integer i = new Integer(-8);

        // Unboxing through method invocation

        int absVal = absoluteValue(i);



List<Double> ld = new ArrayList<>();

        ld.add(3.1416);    // autoboxed through method invocation.

        // Unboxing through assignment

        double pi = ld.get(0);

Primitive type
Wrapper class
boolean
Boolean
byte
Byte
char
Character
float
Float
int
Integer
long
Long
short
Short
double
Double

Benefits of Autoboxing / Unboxing

  • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  • We don't have to perform explicit typecasting
  • It helps prevent errors, but may lead to unexpected results sometimes. Hence must be used with care.

Monday, September 22, 2014

Tagged under: , , , , , ,

JDK, JRE and JVM

 


Java Development Kit (JDK)

Java Development Kit contains tools needed to develop the Java programs, and JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), Applet viewer, etc…

Compiler converts java code into byte code. Java application launcher opens a JRE, loads the class, and invokes its main method.

You need JDK, if at all you want to write your own programs, and to compile them. For running java programs, JRE is sufficient. JRE is targeted for execution of Java. JDK is mainly targeted for java development.

Java Runtime Environment (JRE)


Java Runtime Environment contains JVM, class libraries (like util, math, lang, awt, swing etc), and other supporting files.

Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system.

Java Virtual Machine (JVM)

Java Virtual Machine executes the program and generates output. To execute any code, JVM utilizes different components.   


JVM is divided into several components like the stack, the garbage-collected heap, the registers and the method area.


The Stack

·    Stack in JVM stores various method arguments as well as the local variables of any method.

Method Area
This is the area where byte codes reside. The program counter points to some byte in the method area. It always keeps tracks of the current instruction which is being executed (interpreted).

Garbage-collected Heap
The Garbage-collected Heap is where the objects in Java programs are stored. Whenever we allocate an object using new operator, the heap comes into picture and memory is allocated from there
Remember that the local object reference resides on Stack but the actual object resides in Heap only. Also, arrays in Java are objects, hence they also resides in Garbage-collected Heap.



Just In Time Compiler (JIT)

JIT is a module inside the JVM which helps in compiling certain parts of byte code into the machine code for higher performance. Note that only certain parts of byte code will be compiled to the machine code, the other parts are usually interpreted and executed.

Questions



What is the use of bin and lib in JDK?
- Bin contains all tools such as javac, appletviewer, awt tool, etc., whereas lib contains API and all packages.

Saturday, September 20, 2014

Tagged under: , , , , ,

String, StringBuilder and StringBuffer


String
  • Strings are objects like other objects.
  • They can create instances and they can have constructors and methods.
  • String is final class, so they can't be extended and overridden.
  • Strings are immutable objects.
  • They can be assigned to String variables.
  • They can be used as parameters in methods and constructors.


Ways of create Strings
First way: String s = "apple";
  •     In this situation String object is 'apple'. 's' is the reference variable to refer 'apple'. 
  •        Created one object and one reference variable "apple" will be placed at the heap area(pool) and "s" refers it.
Second way: String s = new String ("apple");
  •         In this way it creates two objects. Because we have used new keyword.
  •     But both are not placed at the pool. "apple" will be placed at the pool while another object(new String) stay in the normal memory. 
String Literal Pool 
  •    String literal pool is a section of heap in the memory. This is pool of Strings which is maintained by JVM(Java Virtual Machine) to store Strings.
  •      Once you create a String, JVM checks it already exists in the pool.
o   If it exist, a reference to the instance in the pool returns.
o   If it not exists a new String object will be created
 Advantages of immutability 
  • It helps to reduce memory usage(Allowing identical values to be combined together and referenced from various locations in your codes)
  • It simplifies multi-threaded programming(Once you initialized, the value cannot be changed)
  • Safe to work(There is no problem with alternatives)  
  • It simplifies the design and implementation(Re-usability of a String )
Disadvantages of immutability 
  •        Less efficient (It creates a heavy garbage collection)






String
StringBuilder
StringBuffer
immutable
mutable
mutable
Synchronized
Not synchronized
Synchronized
Thread safe
Not thread safe
Thread safe
-
Faster than string buffer
Slower than string builder
Java.lang.String
java.lang.StringBuilder
java.lang.StringBuffer
Use more memory
use a memory in efficient way
use a memory in efficient way
charAt(), concat(), equalsIgnoreCase(), length(), replace(), substring(), toLowerCase(), toString(), toUpperCase(), trim()
append(),replace(), insert(), delete(), reverse()
Same methods in string builder