140 likes | 172 Views
Zenrays is a Best Training Institute, which focus to give finest Java training in Bangalore covers from basic level to advance. We are the only one who offers you a free java Training video tutorial for candidates to understand the concept easily. We are an evolving training center which assurances to enlighten the aspirant’s software career through our extraordinary courses. Contact at 9916482106. Call for Java Real time live project http://zenrays.com/java-j2ee-training<br>
E N D
Java Basics 2 A practical tutorial
Package • A class can be placed in a package • This facilitates organizing the classes • Each module can have a number of classes • All these can be placed in one package
Importing classes import com.abc.transport.Car; // need to import classes in other packages public class Travel{ public static void main (String[] args) { Car c = new Car(); c.setType("Audi"); System.out.println("My car is : "+ c.getType()); } } package com.abc.transport; // class Car is placed in package public class Car extends Vehicle { } //Compile : javac –d . *.java
Access specifiers • All private members can be accessed by the same class only • All protected members can be accessed by same, child class and classes in same package • All default members can be accessed by same and classes in same package • All public members can be accessed by all classes in any package
Printing an object • To print an object, • System.out.println(obj); • Will display on console • com.abc.transport.Car@110b053 • In order to print something useful, we have to override toString() method which is defined in Object class • public String toString() { • return “Car is ”+type; • } • Now the output will be • Car is Audi
Comparing two objects • The method • .equals(Object obj) • compares current object with another object • The output is always false as the default implementation in Object class is comparison of references • To change the implementation override the method • public boolean equals(Object obj) { • return this.type == ((Car)obj).getType (); • } • Now the return will be true if type is same in both objects
final variable • public class Employee { • private final String companyName = “ABC Ltd”; • private final String location; • public Employee () { • this.location = “Bangalore”; • } • public Employee (String location) { • this.location = location; • } • } • A final instance variable should be initialized during declaration OR in the constructor. • We can use parameterized constructor to initialize from outside. • new Employee(“Mumbai”);
static field public class StaticTest { public static String str = "Bangalore"; public static void main (String [] args) { StaticTest st1 = new StaticTest(); System.out.println("st1.str :" + st1.str); StaticTest st2 = new StaticTest(); st2.str = "Delhi"; System.out.println("now st1.str :" + st1.str); } } Output is : st1.str :Bangalore now st1.str :Delhi All static fields are shared between objects
String class public class CheckString { public static void main(String[] args) { String str = "This is a city"; StringBuffersbf = new StringBuffer("This is a city"); System.out.println("sbf = "+sbf); CheckStringcs = new CheckString(); cs.updateMe(str); System.out.println("str = "+str); cs.updateMe(sbf); System.out.println("now sbf = "+sbf); } public void updateMe(StringBuffersbf) { sbf.append(" - Bangalore"); } public void updateMe(String str) { str = str.concat( " - Bangalore"); System.out.println("str in updateMe = "+str); }
String class • The output demonstrates that String variable is not modified by the method • This is because String class is immutable • It is a constant • So String is passed by value into the method • If modifications are required, StringBuffer or StringBuilder has to be used
String Pool String s1 = "abc"; String s2 = "abc"; System.out.println("s1= "+s1+", s2= "+s2); System.out.println("s1==s2 : "+(s1==s2)); String s3 = new String("xyz"); String s4 = new String("xyz"); System.out.println("s3==s4 : "+(s3==s4)); • java uses the value from the String pool for s2 as the value is same as s1 • s1==s2 will reveal that both are referring to same object. • This is an example of Java 'FlyweightPattern‘ : object in memory is used if available • new String() will create different object and not from pool
Regular Expressions Sequence of characters that forms a search pattern String ipAddress = "12.34.125.5"; // or args[0] String delimiters = "\\."; String[] tokensVal = ipAddress.split(delimiters); for(String str : tokensVal) System.out.println(str); String idPattern = "^(\\d{3}-?\\d{2}-?\\d{4})$"; String id = "111-22-3333"; // or args[0] Pattern pattern = Pattern.compile(idPattern); Matcher matcher = pattern.matcher(id); System.out.println(matcher.matches()); // id.matches(idPattern); // String supports regex
Password Validation String passwordPattern = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$“; String pswd= "albertLeo@6"; Pattern pattern = Pattern.compile(passwordPattern); Matcher matcher = pattern.matcher(pswd); System.out.println(matcher.matches());
Email Validation String emailPattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";