Tuesday, 18 April 2017

What is the difference between equals() method and == operator



== operator:

  •  == operator used to compare objects references.
  • used to compare data of primitive data types
  • if  we are comparing two objects using "==" operator if reference of both are same then it will returns true otherwise returns false.
  • obj1==obj2;
  • If we are comparing primitive data type variables then it compares data of those two variables
  • Ex: int a=12; int b=12; if(a==b) returns true

Monday, 17 April 2017

​ Substring in Java

​​
 Substring in Java

A part of string is called substring. In other words, substring is a subset of another string. In case of substring startIndex is inclusive and endIndex is exclusive.

Note: Index starts from 0.

You can get substring from the given string object by one of the two methods:

  1. public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive).

  2. public String substring(int startIndex, int endIndex): This method returns new String object containing the substring of the given string from specified startIndex to endIndex.

In case of string:

  • startIndex: inclusive

  • endIndex: exclusive

Let's understand the startIndex and endIndex by the code given below.

  1. String s="hello";  

  2. System.out.println(s.substring(0,2));//he  

In the above substring, 0 points to h but 2 points to e (because end index is exclusive).

Example of java substring

  1. public class TestSubstring{  

  2.  public static void main(String args[]){  

  3.    String s="SouravGanguly";  

  4.    System.out.println(s.substring(6));//Ganguly

  5.    System.out.println(s.substring(0,6));//Sourav

  6.  }  

  7. ​​

Wednesday, 15 March 2017

Access Modifiers in java

There are 4 types of java access modifiers:

  1. private (exclusively within the class)
  2. default (within in the package)
  3. protected (within package && outside package through inheritance)
  4. public (within any package)




Example of protected access modifier

In this example, we have created the two packages pack and mypack. The A class of pack package is public, so can be accessed from outside the package. But msg method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.

  1. //save by A.java  
  2. package pack;  
  3. public class A{  
  4. protected void msg(){System.out.println("Hello");}  
  5. }  
  1. //save by B.java  
  2. package mypack;  
  3. import pack.*;  
  4.   
  5. class B extends A{  
  6.   public static void main(String args[]){  
  7.    B obj = new B();  
  8.    obj.msg();  
  9.   }  
  10. }  
Output:Hello


Sunday, 19 February 2017

Java static variable

1) Java static variable

If you declare any variable as static, it is known static variable.

  • The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
  • The static variable gets memory only once in class area at the time of class loading.

Advantage of static variable

It makes your program memory efficient (i.e it saves memory).


  1. class Counter2{  
  2. static int count=0;//will get memory only once and retain its value  
  3.   
  4. Counter2(){  
  5. count++;  
  6. System.out.println(count);  
  7. }  
  8.   
  9. public static void main(String args[]){  
  10.   
  11. Counter2 c1=new Counter2();  
  12. Counter2 c2=new Counter2();  
  13. Counter2 c3=new Counter2();  
  14.   
  15.  }