You are on page 1of 8

JAVA BASICS 2

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

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 {
} Access specifiers:
//Compile : javac d . *.java
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
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

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

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);

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

public class CheckString {

public static void main(String[] args) {

String str = "This is a city";

StringBuffer sbf = new StringBuffer("This is a city");

System.out.println("sbf = "+sbf);

CheckString cs = new CheckString();

cs.updateMe(str);

System.out.println("str = "+str);

cs.updateMe(sbf);
System.out.println("now sbf = "+sbf);

public void updateMe(StringBuffer sbf) {

sbf.append(" - Bangalore");

public void updateMe(String str) {

str = str.concat( " - Bangalore");

System.out.println("str in updateMe = "+str);

String passwordPattern =

"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$;

Regular Expression Description

^ start of line

(?=.*[0-9]) At least one digit

(?=.*[a-z]) At least one lower case alphabet

(?=.*[A-Z]) At least one upper case alphabet

(?=.*[@#$%^&+=]) At least one special character in the bracket

(?=\\S+$) No white space allowed


.{8,} At least 8 characters

$ end of line

String pswd= "albertLeo@6";

Pattern pattern = Pattern.compile(passwordPattern);

Matcher matcher = pattern.matcher(pswd);

System.out.println(matcher.matches());

String emailPattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-


Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

Regular Expression Description

^ start of line

[_A-Za-z0-9-\\+]+ must start with string in the bracket AND must contain one or
more of the string

(\\.[_A-Za-z0-9-]+)* follow by a period '.' AND string in the bracket one or more AND is
optional

@[A-Za-z0-9-]+ must contain a '@' followed by string in the bracket one or more

(\\.[A-Za-z0-9]+)* follow by a period '.' AND string in the bracket one or more AND is
optional
(\\.[A-Za-z]{2,}) follow by a period '.' AND string in the bracket at least 2 characters

$ end of line

For More Information Learn Java Training in Bangalore at Zenrays Technologies

At +91 9916482106.

You might also like