Posts

Showing posts from December, 2015

The substring() Method in JDK 6 and JDK 7

Image
The substring() Method in JDK 6 and JDK 7 The substring(int beginIndex, int endIndex) method in JDK 6 and JDK 7 are different. Knowing the difference can help you better use them. For simplicity reasons, in the following substring() represent the substring(int beginIndex, int endIndex) method. 1. What substring() does? The substring(int beginIndex, int endIndex) method returns a string that starts with beginIndex and ends with endIndex-1. String x = "abcdef" ; x = x. substring ( 1 , 3 ) ; System . out . println ( x ) ; Output: bc 2. What happens when substring() is called? You may know that because x is immutable, when x is assigned with the result of x.substring(1,3), it points to a totally new string like the following: However, this diagram is not exactly right or it represents what really happens in the heap. What really happens when substring() is called is different between JDK 6 and JDK 7. 3. substring() in JDK 6 String is...

What is string immutability?

Image
What is string immutability? Here are a set of diagrams to illustrate Java String's  immutability . 1. Declare a string String s = "abcd" ; s stores the reference of the string object. The arrow below should be interpreted as "store reference of". 2. Assign one string variable to another string variable String s2 = s ; s2 stores the same reference value, since it is the same string object. 3. Concat string s = s. concat ( "ef" ) ; s now stores the reference of newly created string object. Summary Once a string is created in  memory (heap), it can not be changed. We should note that all methods of String do not change the string itself, but rather return a new String. If we need a string that can be modified, we will need StringBuffer or StringBuilder. Otherwise, there would be a lot of time wasted for Garbage Collection, since each time a new String is created.  Here  is an example of StringBuilder u...
Image
Image
Image
Object Oriented Programming (OOP) Java is a computer programming language that is concurrent, class-based and object-oriented. The advantages of object oriented software development are shown below: Modular development of code, which leads to easy maintenance and modification. Reusability of code. Improved reliability and flexibility of code. Increased understanding of code. Object-oriented programming contains many significant features, such as encapsulation, inheritance, polymorphism and abstraction. We analyze each feature separately in the following sections. Encapsulation Encapsulation provides objects with the ability to hide their internal characteristics and behavior. Each object provides a number of methods, which can be accessed by other objects and change its internal data. In Java, there are three access modifiers: public, private and protected. Each modifier imposes different access rights to other classes, either in the same or in external p...
Image

Basic Rules Need to Follow When Writing Constructors In Java

Basic Rules Need to Follow When Writing Constructors In Java Name of the constructor must be same as that of a class name. If you give another name it will give compile time error. If you give another name, it is neither a method because of no return type, nor constructor because name is different from class name. ? class A {      A()      {          // Constructor of Class A      }      A1()      {          // Compile time error, It is neither a constructor nor a method      } } Constructors must not have a return type. If you keep return type for the constructor, it will be treated as another method.But compiler gives a warning saying that this method has a constructor name. That means, it is legal to have method name same as constructor name or same as class name but it is not recommended. ? class A { ...

Memory Management In Java – Stack And Heap

Whenever you trigger a java command, it divides allocated memory into two parts – Stack and Heap. Stack is used only for execution purpose. Heap is used for storage purpose. consider the following program and we will see how it uses stack and heap memory through diagram. ? class StackAndHeapMemory {      static void methodOne()      {           System.out.println("From Method One");           methodTwo();      }      static void methodTwo()      {           System.out.println("From Method Two");      }      public static void main(String[] args)      {           System.out.println("Main Method Started");           methodOne();           System.out.println("Main Method Ended");      } ...