Thursday, May 9, 2019

Core Java Concepts - String



Java is a very powerful high-level programming language, Java has a lot of super features. String is a data type in java.


What is String ?
Firstly, String is a data type in java training institute in Chennai as you already know, it has a series of character in (other words)
The number of character in a word is a string. For eg: Say, Good Bye ie. say has 3 characters of string and goodbye is the 7 character of string.


String is an unchangeable object in Java that means it’s a constant object, so you can’t change once it’s fixed.


How to create a String ?
There are only two ways to create a String, one is String literal and another one is by using new keyword.


What is the purpose of String ?
You can’t use character all the time while defining a new object or new method. It also creates a lot of confusion to developers, so avoid these types of error, String is created and its also a family of data type, Core java training in Chennai


String Literal:
String literal is nothing but you are assigning the String to String instance
For eg:
String s1 = “Hi, Good Morning”;
String s2 = “Hi, Good Morning”;


String literal is not a good type to determine to String instance because of the involvement of new you can’t java training institute in Chennai able to create an object. So to avoid these types of errors we are going to use


String using New Keyword:
It's the right approach to define a string.
For Eg:
String s1 = new (“Hi,Good Morning”);
String s2 = new (“Hi,Good Morning”);


String Methods in Java:
1.char charAt(int index)
2.boolean equals(Object obj)
3.boolean equalsIgnoreCase(String string)
4.int compareTo(String string)
5.int compareToIgnoreCase(String string)
6.boolean startsWith(String prefix, int offset)
7.boolean startsWith(String prefix)
8.boolean endsWith(String suffix)
9.int hashCode()
10.int indexOf(int ch)
11.int indexOf(int ch, int fromIndex)
12.int lastIndexOf(int ch)
13.int lastIndexOf(int ch, int fromIndex)
14.int indexOf(String str)
15.int lastindexOf(String str)
16.String substring(int beginIndex)
17.String substring(int beginIndex, int endIndex)
18.String concat(String str)
19.String replace(char oldChar, char newChar)
20.boolean contains(CharSequence s)
21.String toUpperCase(Locale locale)
22.String toUpperCase()
23.public String intern()
24.public boolean isEmpty().
25.public static String join()   and so on

Tuesday, April 30, 2019

Access Modifiers in Java


In Java Programming, we have Four Access Modifiers, they are:
1.Default
2.Private
3.Public
4.Protected

Default Access Modifier:


If we don’t mention any access modifier means it is a default access modifier, this modifier is can’t be accessed by any other package, and it can be accessed within the package only. The classes which present in the program Core Java Training in Chennai only access this modifier, The outside package access is restricted to this default modifier.Likewise, method and member can be accessed within the class only

Example of Default Access Modifier:


package vbnpackage;

public class Girl {
  /* Since we didn't mention any access modifier here, it would
   * be considered as default.
   */
  int add(int c, int d){
return c+d;
  }
}

Test1.java
package poipackage;

/* We are importing the vbnpackage
* but still we will get error because the
* class we are trying to use has default access
* modifier.
*/
import vbnpackage.*;
public class Test1 {
  public static void main(String args[]){
Girl obj = new Girl();
       /* It will throw error because we are trying to access
        * the default method in another package
        */
obj.add(10, 21);
  }
}

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method addTwoNumbers(int, int) from the type Add is not visible
at poipackage.Test1.main(Test1.java:12)

Private Access Modifier:


Private Access Modifier is similar to Default Access Modifier, The scope of this private modifier is limited to the class only. The name itself Java Training Institute in Chennai we can understand this private access modifier, “Private” the data members and packages, methods are only accessed within the class. It cannot create the object to constructor from outside of the class.

Example of Private Access Modifier:

class DEF{  
  private double num1 = 100;
  private int squ(int a){
return a*a;
  }
}  
public class Exp{
  public static void main(String args[]){  
DEF obj = new DEF();  
System.out.println(obj.num1);
System.out.println(obj.squ(10));
  }
}

Output:
Compile - time error

Protected Access Modifier

Protected Access Modifier Java Training Institute in Chennai is quite different, Protected data member and method are only accessed by the class of same package and subclasses present in any package.

Example of Protected Access Modifier:

package asdpackage;
public class Addition {

  protected int add1(int l, int k){
return l+k;
  }
}

Test2.java
package ghtpackage;
import asdpackage.*;
class Test2 extends Addition{
  public static void main(String args[]){
Test2 obj = new Test2();
System.out.println(obj.add1(11, 22));
  }
}

Output:
33

Public Access modifier

Public access modifier can be accessed within and outside the classes , it allow all the package and data member to access the class.Core Java Training in Chennai

Example of Public Access Modifier:

package zxcpackage;

public class Addition1 {

  public int add2(int t, int y){
return t+y;
  }
}

Test3.java
package dsapackage;
import zxcpackage.*;
class Test3{
  public static void main(String args[]){
     Addition1 obj = new Addition1();
     System.out.println(obj.add2(100, 1));
  }
}

Output:
101